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 
16  # ---- PUBLIC INTERFACE --------------------------------------------------
17 
22  public function __construct($UserInfo=NULL)
23  {
24  static $EmailWrapperSet = FALSE;
25  if (!$EmailWrapperSet)
26  {
27  User::SetEmailFunction(array("CWUser", "EmailWrapper"));
28  $EmailWrapperSet = TRUE;
29  }
30 
31  parent::__construct($UserInfo);
32 
33  if ($this->Result !== U_OKAY)
34  {
35  throw new Exception(
36  "Unable to load user information.");
37  }
38 
39  # try to fetch the associated resource if the user was found
40  if (!$this->IsAnonymous())
41  {
42  $Resource = $this->FetchAssociatedResource($this->UserId);
43 
44  # the associated resource was successfully found
45  if ($Resource instanceof Resource)
46  {
47  $this->Resource = $Resource;
48  }
49  # there was a problem finding the resource
50  else
51  {
52  throw new Exception(
53  "Unable to load corresponding resource for user.");
54  }
55  }
56  else
57  {
58  $this->Resource = NULL;
59  }
60  }
61 
70  public function Login($UserName, $Password, $IgnorePassword = FALSE)
71  {
72  parent::Login($UserName, $Password, $IgnorePassword);
73 
74  if ($this->Result == U_OKAY)
75  {
76  $Resource = $this->FetchAssociatedResource($this->UserId);
77 
78  # the associated resource was successfully found
79  if ($Resource instanceof Resource)
80  {
81  $this->Resource = $Resource;
82  }
83  # there was a problem finding the resource
84  else
85  {
86  throw new Exception(
87  "Unable to load corresponding resource for user.");
88  }
89  }
90 
91  return $this->Result;
92  }
93 
97  public function Logout()
98  {
99  parent::Logout();
100  $this->Resource = NULL;
101  }
102 
110  public function Privileges(PrivilegeSet $NewValue = NULL)
111  {
112  if ($NewValue !== NULL)
113  {
114  throw new Exception(
115  "Attempt to set user privileges with CWUser::Privileges(), "
116  ."which is no longer supported");
117  }
118 
119  return new PrivilegeSetCompatibilityShim($this);
120  }
121 
127  public function ResourceId()
128  {
129  return ($this->Resource !== NULL) ? $this->Resource->Id() : NULL;
130  }
131 
137  public function GetResource()
138  {
139  return ($this->Resource !== NULL) ? $this->Resource : NULL;
140  }
141 
154  public function HasPriv($Privilege, $Privileges = NULL)
155  {
156  if ($Privilege instanceof PrivilegeSet)
157  {
158  if ($Privileges instanceof Resource)
159  {
160  return $Privilege->MeetsRequirements($this, $Privileges);
161  }
162  else
163  {
164  return $Privilege->MeetsRequirements($this);
165  }
166  }
167  else
168  {
169  return call_user_func_array( "parent::HasPriv", func_get_args() );
170  }
171  }
172 
183  public static function EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
184  {
185  # extract "From" address from supplied headers if available
186  if (strlen($AdditionalHeaders))
187  {
188  $HeaderLines = explode("\n", $AdditionalHeaders);
189  $Headers = array();
190  foreach ($HeaderLines as $Line)
191  {
192  $HeaderLine = trim($Line);
193  if (preg_match("/^from:/i", $Line))
194  {
195  $From = preg_replace("/^from:/i", "", $Line);
196  }
197  else
198  {
199  $Headers[] = $HeaderLine;
200  }
201  }
202  }
203 
204  # send message
205  $Msg = new Email();
206  if (isset($From)) { $Msg->From($From); }
207  $Msg->To($To);
208  $Msg->Subject($Subject);
209  $Msg->AddHeaders($Headers);
210  $Msg->Body($Message);
211  $Result = $Msg->Send();
212 
213  # report success to caller
214  return $Result;
215  }
216 
221  public static function GetCustomUserFields()
222  {
223  static $CustomFields;
224 
225  if (!isset($CustomFields))
226  {
227  $CustomFields = array();
229 
230  foreach ($Schema->GetFields() as $Field)
231  {
232  # they're custom if not owned by CWIS
233  if ($Field->Owner() != "CWISCore")
234  {
235  $CustomFields[$Field->Id()] = $Field;
236  }
237  }
238  }
239 
240  return $CustomFields;
241  }
242 
247  public static function GetDefaultUserFields()
248  {
249  static $DefaultFields;
250 
251  if (!isset($DefaultFields))
252  {
253  $DefaultFields = array();
255 
256  foreach ($Schema->GetFields() as $Field)
257  {
258  # they're default if owned by CWIS
259  if ($Field->Owner() == "CWISCore")
260  {
261  $DefaultFields[$Field->Id()] = $Field;
262  }
263  }
264  }
265 
266  return $DefaultFields;
267  }
268 
269  # ---- OVERRIDDEN METHODS ------------------------------------------------
270 
276  public function Delete()
277  {
278  # delete the associated user resource if set
279  if (isset($this->Resource))
280  {
281  $this->Resource->Delete();
282  $this->Result = U_OKAY;
283  }
284 
285  return parent::Delete();
286  }
287 
294  public function Get($FieldName)
295  {
296  # all values are NULL for anonymous users
297  if ($this->IsAnonymous())
298  {
299  return NULL;
300  }
301 
302  if (in_array($FieldName, self::$FieldsOnlyInAPUsers))
303  {
304  return parent::Get($FieldName);
305  }
306  else
307  {
308  return $this->Resource->Get($FieldName);
309  }
310  }
311 
318  public function Set($Field, $NewValue)
319  {
320  if ($this->IsAnonymous())
321  {
322  throw new Exception(
323  "Attempt to set User field value when "
324  ."no user is logged in.");
325  }
326 
327  # make sure Field is a FieldName
328  if ($Field instanceof MetadataField)
329  {
330  $Field = $Field->Name();
331  }
332 
333  # if this field is not among those that should only exists in
334  # the APUsers table
335  if (!in_array($Field, self::$FieldsOnlyInAPUsers))
336  {
337  # set it in our corresponding resource
338  $this->Resource->Set($Field, $NewValue);
339  }
340 
341  # if the given field exists in the APUsers table, update that too
342  if ($this->DB->FieldExists("APUsers", $Field))
343  {
344  parent::Set($Field, $NewValue);
345  }
346  else
347  {
348  # indicate success for fields that don't have a column in APUsers
349  $this->Result = U_OKAY;
350  }
351 
352  return $this->Result;
353  }
354 
355  # ---- PRIVATE INTERFACE -------------------------------------------------
356 
361  protected $Resource = NULL;
362 
363  # list of fields that exist in APUsers that are not mirrored as MetadataFields
364  private static $FieldsOnlyInAPUsers = [
365  # fields necessary to for user identification
366  "UserId", "UserName", "EMail", "EMailNew",
367 
368  # fields necessary for authentication
369  "UserPassword", "RegistrationConfirmed",
370 
371  # fields that can't be in a schema because they are updated by User
372  "LastLoginDate", "LastActiveDate", "LastIPAddress", "LastLocation", "LoggedIn",
373 
374  # user preferences
375  "ActiveUI", "BrowsingFieldId", "RecordsPerPage", "SearchSelections",
376  ];
377 
384  protected function FetchAssociatedResource($UserId)
385  {
386  if (self::$UserIdFieldId === NULL)
387  {
388  # get the user schema
390 
391  # pull out the UserId field, which should only be one
392  $Field = $Schema->GetField("UserId");
393 
394  # and get its FieldId
395  self::$UserIdFieldId = intval($Field->Id());
396  }
397 
398  # find the matching Resources (should only be one)
399  $this->DB->Query(
400  "SELECT ResourceId FROM ResourceUserInts WHERE ".
401  "FieldId=".self::$UserIdFieldId.
402  " AND UserId=".intval($UserId) );
403  $ResourceIds = $this->DB->FetchColumn("ResourceId");
404  $ResourceIdCount = count($ResourceIds);
405 
406  # no resource found
407  if ($ResourceIdCount < 1)
408  {
409  return U_NOSUCHUSER;
410  }
411 
412  # too many resources found
413  if ($ResourceIdCount > 1)
414  {
415  throw new Exception(
416  "Multiple resources exist for a single user, "
417  ."which should be impossible");
418  }
419 
420  # construct the associated resource and return it
421  return new Resource(array_shift($ResourceIds));
422  }
423 
424 
425  # ---- MAINTAINED FOR BACKWARD COMPATIBILITY IN INTERFACES (BEGIN)
426 
427  # ---- user interface preference mnemonics
428  # color avoidance flags
429  const UIPREF_AVOID_RED = 1;
430  const UIPREF_AVOID_REDGREEN = 2;
431  const UIPREF_AVOID_BLUEYELLOW = 4;
432  const UIPREF_AVOID_GREENYELLOW = 8;
433  const UIPREF_AVOID_ORANGE = 16;
434  const UIPREF_AVOID_REDBLACK = 32;
435  const UIPREF_AVOID_PURPLEGREY = 64;
436  const UIPREF_AVOID_USEMAXMONOCHR = 128;
437 
438  # content display options
439  const UIPREF_CONTENTDENSITY_NOPREFERENCE = 0;
440  const UIPREF_CONTENTDENSITY_DETAILED = 1;
441  const UIPREF_CONTENTDENSITY_OVERVIEW = 2;
442 
443  # content view options
444  const UIPREF_CONTENTVIEW_NOPREFERENCE = 0;
445  const UIPREF_CONTENTVIEW_TEXTINTENSIVE = 1;
446  const UIPREF_CONTENTVIEW_IMAGEINTENSIVE = 2;
447 
448  # audio description options
449  const UIPREF_AUDIODESCRIPTION_NONE = 0;
450  const UIPREF_AUDIODESCRIPTION_STANDARD = 1;
451  const UIPREF_AUDIODESCRIPTION_EXPANDED = 2;
452 
453  # caption type options
454  const UIPREF_CAPTIONTYPE_NONE = 0;
455  const UIPREF_CAPTIONTYPE_VERBATIM = 1;
456  const UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL = 2;
457 
458  // @codingStandardsIgnoreStart
459 
460  # user interface / accessibility preferences
461  function PrefFontSize($NewValue = DB_NOVALUE)
462  { return 0; }
463 
464  function PrefFontTypeFace($NewValue = DB_NOVALUE)
465  { return 0; }
466 
467  function PrefFontColor($NewValue = DB_NOVALUE)
468  { return 0; }
469 
470  function PrefBackgroundColor($NewValue = DB_NOVALUE)
471  { return 0; }
472 
473  function PrefColorAvoidanceFlags($NewValue = DB_NOVALUE)
474  { return 0; }
475 
476  function PrefContentDensity($NewValue = DB_NOVALUE)
477  { return 0; }
478 
479  function PrefContentView($NewValue = DB_NOVALUE)
480  { return 0; }
481 
482  function PrefAudioDescriptionLevel($NewValue = DB_NOVALUE)
483  { return 0; }
484 
485  function PrefAudioDescriptionLanguage($NewValue = DB_NOVALUE)
486  { return 0; }
487 
488  function PrefVisualDescriptionLanguage($NewValue = DB_NOVALUE)
489  { return 0; }
490 
491  function PrefImageDescriptionLanguage($NewValue = DB_NOVALUE)
492  { return 0; }
493 
494  function PrefUseGraphicAlternatives($NewValue = DB_NOVALUE)
495  { return 0; }
496 
497  function PrefSignLanguage($NewValue = DB_NOVALUE)
498  { return 0; }
499 
500  function PrefCaptionType($NewValue = DB_NOVALUE)
501  { return 0; }
502 
503  function PrefCaptionRate($NewValue = DB_NOVALUE)
504  { return 0; }
505 
506  // @codingStandardsIgnoreEnd
507  # ---- MAINTAINED FOR BACKWARD COMPATIBILITY IN INTERFACES (END)
508 
509  private static $UserIdFieldId = NULL;
510 }
Metadata schema (in effect a Factory class for MetadataField).
$Resource
The user resource associated with the user or NULL if the user isn&#39;t logged in.
Definition: CWUser.php:361
Privileges(PrivilegeSet $NewValue=NULL)
THIS FUNCTION HAS BEEN DEPRECATED This provides compatibility for interfaces written to use a version...
Definition: CWUser.php:110
ResourceId()
Get the ID of the user resource associated with the user.
Definition: CWUser.php:127
Delete()
Delete the user and its associated user resource.
Definition: CWUser.php:276
Id()
Retrieve numerical resource ID.
Definition: Resource.php:291
HasPriv($Privilege, $Privileges=NULL)
Determine if a user has a given privilege, or satisfies the conditions specified by a given privilege...
Definition: CWUser.php:154
Get($FieldName)
Get a value from the specified field.
Definition: CWUser.php:294
Set($Field, $NewValue)
Set a value for the specified field.
Definition: CWUser.php:318
Definition: User.php:48
Set of privileges used to access resource information or other parts of the system.
Login($UserName, $Password, $IgnorePassword=FALSE)
Log the specified user in and associate the underlying Resource with this CWUser. ...
Definition: CWUser.php:70
Electronic mail message.
Definition: Email.php:14
Get($Field, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
Definition: Resource.php:415
__construct($UserInfo=NULL)
Load user data from the given user info or from the session if available.
Definition: CWUser.php:22
const DB_NOVALUE
Definition: Database.php:1738
IsAnonymous()
Report whether user is anonymous user.
Definition: User.php:500
static EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
Adapter method to bridge between User class and Email class.
Definition: CWUser.php:183
static SetEmailFunction($NewValue)
Set email function to use instead of mail().
Definition: User.php:208
const U_OKAY
Definition: User.php:18
$Result
Definition: User.php:1081
Compatibility layer allowing interfaces built against the privilege system from CWIS 3...
Object representing a locally-defined type of metadata field.
Represents a "resource" in CWIS.
Definition: Resource.php:13
static GetDefaultUserFields()
Get the default user fields.
Definition: CWUser.php:247
const U_NOSUCHUSER
Definition: User.php:21
$UserId
Definition: User.php:1080
static GetCustomUserFields()
Get all custom user fields.
Definition: CWUser.php:221
Set($Field, $NewValue, $Reset=FALSE)
Set value using field name or field object.
Definition: Resource.php:1143
GetResource()
Get the associated user resource for this user.
Definition: CWUser.php:137
CWIS-specific user class.
Definition: CWUser.php:13
Logout()
Log this user out and disassociate their underlying Resource from this CWUser.
Definition: CWUser.php:97
Delete()
Remove resource (and accompanying associations) from database and delete any associated files...
Definition: Resource.php:142