CWIS Developer Documentation
CWUser.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: CWUser.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
13 class CWUser extends User {
14 
15  # ---- PUBLIC INTERFACE --------------------------------------------------
16 
21  public function __construct($UserInfo=NULL)
22  {
23  static $EmailWrapperSet = FALSE;
24  if (!$EmailWrapperSet)
25  {
26  User::SetEmailFunction(array("CWUser", "EmailWrapper"));
27  $EmailWrapperSet = TRUE;
28  }
29 
30  # call the parent constructor
31  parent::User($UserInfo);
32 
33  # try to fetch the associated resource if the user was found
34  if ($this->Result === U_OKAY)
35  {
36  $Resource = $this->FetchAssociatedResource($this->UserId);
37 
38  # the associated resource was successfully found
39  if ($Resource instanceof Resource)
40  {
41  $this->Resource = $Resource;
42  }
43 
44  # there was a problem finding the resource
45  else
46  {
47  $this->Result = $Resource;
48  }
49  }
50  }
51 
57  public function Privileges(PrivilegeSet $NewValue = NULL)
58  {
59  # if new set of privileges supplied
60  if ($NewValue !== NULL)
61  {
62  # retrieve privilege list from set
63  $PrivList = $NewValue->GetPrivilegeList();
64 
65  # set new privilege list for user
66  $this->SetPrivList($PrivList);
67  }
68 
69  # retrieve privilege list for user
70  $Privs = $this->GetPrivList();
71 
72  # convert privilege list to privilege set
73  $Set = new PrivilegeSet();
74  foreach ($Privs as $Priv) { $Set->AddPrivilege($Priv); }
75 
76  # set user associated with privilege set
77  $Set->AssociatedUserId($this->Id());
78 
79  # return privilege set to caller
80  return $Set;
81  }
82 
88  public function ResourceId()
89  {
90  return $this->IsResourceObjectSet() ? $this->Resource->Id() : NULL;
91  }
92 
98  public function GetResource()
99  {
100  return $this->IsResourceObjectSet() ? $this->Resource : NULL;
101  }
102 
103 
107  public function HasPriv($Privilege, $Privileges = NULL)
108  {
109  if ($Privilege instanceof PrivilegeSet)
110  {
111  $UserPrivs = $this->Privileges();
112  if ($Privileges instanceof Resource)
113  return $UserPrivs->IsGreaterThan($Privilege, $Privileges);
114  else
115  return $UserPrivs->IsGreaterThan($Privilege);
116  }
117  else
118  {
119  return call_user_func_array( "parent::HasPriv", func_get_args() );
120  }
121  }
122 
133  public static function EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
134  {
135  # extract "From" address from supplied headers if available
136  if (strlen($AdditionalHeaders))
137  {
138  $HeaderLines = explode("\n", $AdditionalHeaders);
139  $Headers = array();
140  foreach ($HeaderLines as $Line)
141  {
142  $HeaderLine = trim($Line);
143  if (preg_match("/^from:/i", $Line))
144  {
145  $From = preg_replace("/^from:/i", "", $Line);
146  }
147  else
148  {
149  $Headers[] = $HeaderLine;
150  }
151  }
152  }
153 
154  # send message
155  $Msg = new Email();
156  if (isset($From)) { $Msg->From($From); }
157  $Msg->To($To);
158  $Msg->Subject($Subject);
159  $Msg->AddHeaders($Headers);
160  $Msg->Body($Message);
161  $Result = $Msg->Send();
162 
163  # report success to caller
164  return $Result;
165  }
166 
171  public static function GetCustomUserFields()
172  {
173  static $CustomFields;
174 
175  if (!isset($CustomFields))
176  {
177  $CustomFields = array();
179 
180  foreach ($Schema->GetFields() as $Field)
181  {
182  # they're custom if not owned by CWIS
183  if ($Field->Owner() != "CWISCore")
184  {
185  $CustomFields[$Field->Id()] = $Field;
186  }
187  }
188  }
189 
190  return $CustomFields;
191  }
192 
197  public static function GetDefaultUserFields()
198  {
199  static $DefaultFields;
200 
201  if (!isset($DefaultFields))
202  {
203  $DefaultFields = array();
205 
206  foreach ($Schema->GetFields() as $Field)
207  {
208  # they're default if owned by CWIS
209  if ($Field->Owner() == "CWISCore")
210  {
211  $DefaultFields[$Field->Id()] = $Field;
212  }
213  }
214  }
215 
216  return $DefaultFields;
217  }
218 
219  # ---- OVERRIDDEN METHODS ------------------------------------------------
220 
226  public function Delete()
227  {
228  # delete the associated user resource if set
229  if (isset($this->Resource))
230  {
231  $this->Resource->Delete();
232  $this->Result = U_OKAY;
233  }
234 
235  return parent::Delete();
236  }
237 
244  public function Get($FieldName)
245  {
246  # provide backwards-compatibility for data migrated to users fields as
247  # of CWIS 3.0.0
248  if (in_array($FieldName, self::$MigratedUserFields))
249  {
250  # return NULL if the resource object isn't set
251  if (!$this->IsResourceObjectSet())
252  {
253  return NULL;
254  }
255 
256  # return the value from the associated resource
257  return $this->Resource->Get($FieldName);
258  }
259 
260  # otherwise, get it from the APUsers table
261  return parent::Get($FieldName);
262  }
263 
270  public function Set($FieldName, $NewValue)
271  {
272  # provide backwards-compatibility for data migrated to users fields as
273  # of CWIS 3.0.0
274  if (in_array($FieldName, self::$MigratedUserFields))
275  {
276  # set the value only if the resource object is set
277  if ($this->IsResourceObjectSet())
278  {
279  $this->Resource->Set($FieldName, $NewValue);
280  $this->Result = U_OKAY;
281  }
282  }
283 
284  # transform boolean values to 1 or 0 because that's what the User
285  # class expects
286  if (is_bool($NewValue))
287  {
288  $NewValue = $NewValue ? 1 : 0;
289  }
290 
291  # update the APUsers table
292  return parent::Set($FieldName, $NewValue);
293  }
294 
295  # ---- PRIVATE INTERFACE -------------------------------------------------
296 
301  protected $Resource;
302 
307  protected static $MigratedUserFields = array(
308  "RealName", "WebSite", "AddressLineOne", "AddressLineTwo", "City",
309  "State", "ZipCode", "Country");
310 
317  protected function FetchAssociatedResource($UserId)
318  {
319  try
320  {
323  }
324 
325  # couldn't get the factory or schema, which probably means CWIS hasn't
326  # been installed yet
327  catch (Exception $Exception)
328  {
329  return U_ERROR;
330  }
331 
332  # the UserId field doesn't exist, which probably means CWIS hasn't been
333  # installed yet
334  if (!$Schema->FieldExists("UserId"))
335  {
336  return U_ERROR;
337  }
338 
339  # get matching resources, which should only be one
340  $Field = $Schema->GetFieldByName("UserId");
341  $ResourceIds = $Factory->GetItemIds("`".$Field->DBFieldName()
342  ."` = '".intval($UserId)."'");
343  $ResourceIdCount = count($ResourceIds);
344 
345  # no resource found
346  if ($ResourceIdCount < 1)
347  {
348  return U_NOSUCHUSER;
349  }
350 
351  # too many resources found
352  if ($ResourceIdCount > 1)
353  {
354  return U_ERROR;
355  }
356 
357  # construct the associated resource and return it
358  return new Resource(array_shift($ResourceIds));
359  }
360 
365  protected function IsResourceObjectSet()
366  {
367  # there must be a user ID, which is what the User class assumes, and the
368  # resource must be set
369  return isset($this->UserId) && isset($this->Resource);
370  }
371 
372 }
Set($FieldName, $NewValue)
Set a value for the specified field.
Definition: CWUser.php:270
Metadata schema (in effect a Factory class for MetadataField).
$Resource
The user resource associated with the user or NULL if the user isn't logged in.
Definition: CWUser.php:301
Privileges(PrivilegeSet $NewValue=NULL)
Get/set user privileges as a set.
Definition: CWUser.php:57
ResourceId()
Get the ID of the user resource associated with the user.
Definition: CWUser.php:88
Delete()
Delete the user and its associated user resource.
Definition: CWUser.php:226
Set($FieldNameOrObject, $NewValue, $Reset=FALSE)
Set value using field name or field object.
Definition: Resource.php:972
Id()
Retrieve numerical resource ID.
Definition: Resource.php:256
FetchAssociatedResource($UserId)
Fetch the associated user resource based off of a user ID.
Definition: CWUser.php:317
IsResourceObjectSet()
Determine if the resource object for this object is set.
Definition: CWUser.php:365
HasPriv($Privilege, $Privileges=NULL)
Determine if a user has a speficifed prviliege.
Definition: CWUser.php:107
Get($FieldName)
Get a value from the specified field.
Definition: CWUser.php:244
const U_NOSUCHUSER
Definition: Axis--User.php:22
Set of privileges used to access resource information or other parts of the system.
Electronic mail message.
Definition: Email.php:14
GetPrivList()
Definition: Axis--User.php:916
PHP
Definition: OAIClient.php:39
__construct($UserInfo=NULL)
Load user data from the given user info or from the session if available.
Definition: CWUser.php:21
const U_ERROR
Definition: Axis--User.php:20
static EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
Adapter method to bridge between AxisPHP User class and ScoutLib Email class.
Definition: CWUser.php:133
static SetEmailFunction($NewValue)
Set email function to use instead of mail().
Definition: Axis--User.php:178
Get($FieldNameOrObject, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
Definition: Resource.php:348
static $MigratedUserFields
Fields that were previously part of the APUsers table that have been migrated to the Resources table ...
Definition: CWUser.php:307
Represents a "resource" in CWIS.
Definition: Resource.php:13
static GetDefaultUserFields()
Get the default user fields.
Definition: CWUser.php:197
static GetCustomUserFields()
Get all custom user fields.
Definition: CWUser.php:171
const U_OKAY
Definition: Axis--User.php:19
SetPrivList($NewPrivileges)
Definition: Axis--User.php:927
GetResource()
Get the associated user resource for this user.
Definition: CWUser.php:98
Factory for Resource objects.
CWIS-specific user class.
Definition: CWUser.php:13
Delete()
Remove resource (and accompanying associations) from database and delete any associated files...
Definition: Resource.php:143