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  # 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 
59  public function Privileges(PrivilegeSet $NewValue = NULL)
60  {
61  if ($NewValue !== NULL)
62  {
63  throw new Exception(
64  "Attempt to set user privileges with CWUser::Privileges(), "
65  ."which is no longer supported");
66  }
67 
68  return new PrivilegeSetCompatibilityShim($this);
69  }
70 
76  public function ResourceId()
77  {
78  return $this->IsResourceObjectSet() ? $this->Resource->Id() : NULL;
79  }
80 
86  public function GetResource()
87  {
88  return $this->IsResourceObjectSet() ? $this->Resource : NULL;
89  }
90 
91 
104  public function HasPriv($Privilege, $Privileges = NULL)
105  {
106  if ($Privilege instanceof PrivilegeSet)
107  {
108  if ($Privileges instanceof Resource)
109  {
110  return $Privilege->MeetsRequirements($this, $Privileges);
111  }
112  else
113  {
114  return $Privilege->MeetsRequirements($this);
115  }
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  if (self::$UserIdFieldId === NULL)
320  {
321  try
322  {
324  }
325 
326  # couldn't get the user schema, which probably means CWIS hasn't
327  # been installed yet
328  catch (Exception $Exception)
329  {
330  return U_ERROR;
331  }
332 
333  # the UserId field doesn't exist, which probably means CWIS hasn't been
334  # installed yet
335  if (!$Schema->FieldExists("UserId"))
336  {
337  return U_ERROR;
338  }
339 
340  # get matching resources, which should only be one
341  $Field = $Schema->GetFieldByName("UserId");
342 
343  self::$UserIdFieldId = intval($Field->Id());
344  }
345 
346  $this->DB->Query(
347  "SELECT ResourceId FROM ResourceUserInts WHERE ".
348  "FieldId=".self::$UserIdFieldId.
349  " AND UserId=".intval($UserId) );
350  $ResourceIds = $this->DB->FetchColumn("ResourceId");
351  $ResourceIdCount = count($ResourceIds);
352 
353  # no resource found
354  if ($ResourceIdCount < 1)
355  {
356  return U_NOSUCHUSER;
357  }
358 
359  # too many resources found
360  if ($ResourceIdCount > 1)
361  {
362  return U_ERROR;
363  }
364 
365  # construct the associated resource and return it
366  return new Resource(array_shift($ResourceIds));
367  }
368 
373  protected function IsResourceObjectSet()
374  {
375  # there must be a user ID, which is what the User class assumes, and the
376  # resource must be set
377  return isset($this->UserId) && isset($this->Resource);
378  }
379 
380 
381  # ---- MAINTAINED FOR BACKWARD COMPATIBILITY IN INTERFACES (BEGIN)
382 
383  # ---- user interface preference mnemonics
384  # color avoidance flags
385  const UIPREF_AVOID_RED = 1;
393 
394  # content display options
398 
399  # content view options
403 
404  # audio description options
408 
409  # caption type options
413 
414  // @codingStandardsIgnoreStart
415 
416  # user interface / accessibility preferences
417  function PrefFontSize($NewValue = DB_NOVALUE)
418  { return 0; }
419 
420  function PrefFontTypeFace($NewValue = DB_NOVALUE)
421  { return 0; }
422 
423  function PrefFontColor($NewValue = DB_NOVALUE)
424  { return 0; }
425 
426  function PrefBackgroundColor($NewValue = DB_NOVALUE)
427  { return 0; }
428 
429  function PrefColorAvoidanceFlags($NewValue = DB_NOVALUE)
430  { return 0; }
431 
432  function PrefContentDensity($NewValue = DB_NOVALUE)
433  { return 0; }
434 
435  function PrefContentView($NewValue = DB_NOVALUE)
436  { return 0; }
437 
438  function PrefAudioDescriptionLevel($NewValue = DB_NOVALUE)
439  { return 0; }
440 
442  { return 0; }
443 
445  { return 0; }
446 
448  { return 0; }
449 
451  { return 0; }
452 
453  function PrefSignLanguage($NewValue = DB_NOVALUE)
454  { return 0; }
455 
456  function PrefCaptionType($NewValue = DB_NOVALUE)
457  { return 0; }
458 
459  function PrefCaptionRate($NewValue = DB_NOVALUE)
460  { return 0; }
461 
462  // @codingStandardsIgnoreEnd
463  # ---- MAINTAINED FOR BACKWARD COMPATIBILITY IN INTERFACES (END)
464 
465  private static $UserIdFieldId = NULL;
466 }
PrefFontTypeFace($NewValue=DB_NOVALUE)
Definition: CWUser.php:420
PrefUseGraphicAlternatives($NewValue=DB_NOVALUE)
Definition: CWUser.php:450
const UIPREF_AVOID_ORANGE
Definition: CWUser.php:389
PrefCaptionType($NewValue=DB_NOVALUE)
Definition: CWUser.php:456
Set($FieldName, $NewValue)
Set a value for the specified field.
Definition: CWUser.php:270
const UIPREF_AVOID_BLUEYELLOW
Definition: CWUser.php:387
Metadata schema (in effect a Factory class for MetadataField).
const UIPREF_CONTENTVIEW_TEXTINTENSIVE
Definition: CWUser.php:401
const UIPREF_AVOID_REDBLACK
Definition: CWUser.php:390
PrefImageDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:447
$Resource
The user resource associated with the user or NULL if the user isn&#39;t logged in.
Definition: CWUser.php:301
Privileges(PrivilegeSet $NewValue=NULL)
THIS FUNCTION HAS BEEN DEPRECATED This provides compatibility for interfaces written to use a version...
Definition: CWUser.php:59
ResourceId()
Get the ID of the user resource associated with the user.
Definition: CWUser.php:76
const UIPREF_AVOID_REDGREEN
Definition: CWUser.php:386
Delete()
Delete the user and its associated user resource.
Definition: CWUser.php:226
const UIPREF_AVOID_GREENYELLOW
Definition: CWUser.php:388
const UIPREF_AUDIODESCRIPTION_NONE
Definition: CWUser.php:405
const UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL
Definition: CWUser.php:412
PrefCaptionRate($NewValue=DB_NOVALUE)
Definition: CWUser.php:459
PrefBackgroundColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:426
Id()
Retrieve numerical resource ID.
Definition: Resource.php:291
PrefSignLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:453
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:373
const U_ERROR
Definition: User.php:19
const UIPREF_AVOID_USEMAXMONOCHR
Definition: CWUser.php:392
const UIPREF_CONTENTDENSITY_NOPREFERENCE
Definition: CWUser.php:395
HasPriv($Privilege, $Privileges=NULL)
Determine if a user has a given privilege, or satisfies the conditions specified by a given privilege...
Definition: CWUser.php:104
const UIPREF_AUDIODESCRIPTION_STANDARD
Definition: CWUser.php:406
Get($FieldName)
Get a value from the specified field.
Definition: CWUser.php:244
PrefAudioDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:441
const UIPREF_CONTENTDENSITY_DETAILED
Definition: CWUser.php:396
const UIPREF_CONTENTVIEW_NOPREFERENCE
Definition: CWUser.php:400
Definition: User.php:48
PrefContentView($NewValue=DB_NOVALUE)
Definition: CWUser.php:435
Set of privileges used to access resource information or other parts of the system.
PrefContentDensity($NewValue=DB_NOVALUE)
Definition: CWUser.php:432
PrefFontColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:423
PrefColorAvoidanceFlags($NewValue=DB_NOVALUE)
Definition: CWUser.php:429
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:1675
const UIPREF_CAPTIONTYPE_VERBATIM
Definition: CWUser.php:411
PrefVisualDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:444
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: User.php:210
const U_OKAY
Definition: User.php:18
const UIPREF_CONTENTVIEW_IMAGEINTENSIVE
Definition: CWUser.php:402
const UIPREF_AVOID_RED
Definition: CWUser.php:385
PrefAudioDescriptionLevel($NewValue=DB_NOVALUE)
Definition: CWUser.php:438
$Result
Definition: User.php:1076
Compatibility layer allowing interfaces built against the privilege system from CWIS 3...
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
const UIPREF_AUDIODESCRIPTION_EXPANDED
Definition: CWUser.php:407
const U_NOSUCHUSER
Definition: User.php:21
const UIPREF_CAPTIONTYPE_NONE
Definition: CWUser.php:410
$UserId
Definition: User.php:1075
static GetCustomUserFields()
Get all custom user fields.
Definition: CWUser.php:171
Set($Field, $NewValue, $Reset=FALSE)
Set value using field name or field object.
Definition: Resource.php:1141
const UIPREF_AVOID_PURPLEGREY
Definition: CWUser.php:391
const UIPREF_CONTENTDENSITY_OVERVIEW
Definition: CWUser.php:397
GetResource()
Get the associated user resource for this user.
Definition: CWUser.php:86
CWIS-specific user class.
Definition: CWUser.php:13
Delete()
Remove resource (and accompanying associations) from database and delete any associated files...
Definition: Resource.php:142
PrefFontSize($NewValue=DB_NOVALUE)
Definition: CWUser.php:417