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  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 
342  $DB = new Database();
343  $DB->Query(
344  "SELECT ResourceId FROM ResourceUserInts WHERE ".
345  "FieldId=".intval($Field->Id()).
346  " AND UserId=".intval($UserId) );
347  $ResourceIds = $DB->FetchColumn("ResourceId");
348  $ResourceIdCount = count($ResourceIds);
349 
350  # no resource found
351  if ($ResourceIdCount < 1)
352  {
353  return U_NOSUCHUSER;
354  }
355 
356  # too many resources found
357  if ($ResourceIdCount > 1)
358  {
359  return U_ERROR;
360  }
361 
362  # construct the associated resource and return it
363  return new Resource(array_shift($ResourceIds));
364  }
365 
370  protected function IsResourceObjectSet()
371  {
372  # there must be a user ID, which is what the User class assumes, and the
373  # resource must be set
374  return isset($this->UserId) && isset($this->Resource);
375  }
376 
377 
378  # ---- MAINTAINED FOR BACKWARD COMPATIBILITY IN INTERFACES (BEGIN)
379 
380  # ---- user interface preference mnemonics
381  # color avoidance flags
382  const UIPREF_AVOID_RED = 1;
390 
391  # content display options
395 
396  # content view options
400 
401  # audio description options
405 
406  # caption type options
410 
411  // @codingStandardsIgnoreStart
412 
413  # user interface / accessibility preferences
414  function PrefFontSize($NewValue = DB_NOVALUE)
415  { return 0; }
416 
417  function PrefFontTypeFace($NewValue = DB_NOVALUE)
418  { return 0; }
419 
420  function PrefFontColor($NewValue = DB_NOVALUE)
421  { return 0; }
422 
423  function PrefBackgroundColor($NewValue = DB_NOVALUE)
424  { return 0; }
425 
426  function PrefColorAvoidanceFlags($NewValue = DB_NOVALUE)
427  { return 0; }
428 
429  function PrefContentDensity($NewValue = DB_NOVALUE)
430  { return 0; }
431 
432  function PrefContentView($NewValue = DB_NOVALUE)
433  { return 0; }
434 
435  function PrefAudioDescriptionLevel($NewValue = DB_NOVALUE)
436  { return 0; }
437 
439  { return 0; }
440 
442  { return 0; }
443 
445  { return 0; }
446 
448  { return 0; }
449 
450  function PrefSignLanguage($NewValue = DB_NOVALUE)
451  { return 0; }
452 
453  function PrefCaptionType($NewValue = DB_NOVALUE)
454  { return 0; }
455 
456  function PrefCaptionRate($NewValue = DB_NOVALUE)
457  { return 0; }
458 
459  // @codingStandardsIgnoreEnd
460  # ---- MAINTAINED FOR BACKWARD COMPATIBILITY IN INTERFACES (END)
461 }
PrefFontTypeFace($NewValue=DB_NOVALUE)
Definition: CWUser.php:417
PrefUseGraphicAlternatives($NewValue=DB_NOVALUE)
Definition: CWUser.php:447
$DB
Definition: User.php:976
const UIPREF_AVOID_ORANGE
Definition: CWUser.php:386
PrefCaptionType($NewValue=DB_NOVALUE)
Definition: CWUser.php:453
Set($FieldName, $NewValue)
Set a value for the specified field.
Definition: CWUser.php:270
const UIPREF_AVOID_BLUEYELLOW
Definition: CWUser.php:384
Metadata schema (in effect a Factory class for MetadataField).
const UIPREF_CONTENTVIEW_TEXTINTENSIVE
Definition: CWUser.php:398
const UIPREF_AVOID_REDBLACK
Definition: CWUser.php:387
PrefImageDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:444
$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
SQL database abstraction object with smart query caching.
Definition: Database.php:22
const UIPREF_AVOID_REDGREEN
Definition: CWUser.php:383
Delete()
Delete the user and its associated user resource.
Definition: CWUser.php:226
const UIPREF_AVOID_GREENYELLOW
Definition: CWUser.php:385
const UIPREF_AUDIODESCRIPTION_NONE
Definition: CWUser.php:402
const UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL
Definition: CWUser.php:409
PrefCaptionRate($NewValue=DB_NOVALUE)
Definition: CWUser.php:456
PrefBackgroundColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:423
Id()
Retrieve numerical resource ID.
Definition: Resource.php:287
PrefSignLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:450
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:370
const U_ERROR
Definition: User.php:19
const UIPREF_AVOID_USEMAXMONOCHR
Definition: CWUser.php:389
const UIPREF_CONTENTDENSITY_NOPREFERENCE
Definition: CWUser.php:392
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:403
Get($FieldName)
Get a value from the specified field.
Definition: CWUser.php:244
PrefAudioDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:438
const UIPREF_CONTENTDENSITY_DETAILED
Definition: CWUser.php:393
const UIPREF_CONTENTVIEW_NOPREFERENCE
Definition: CWUser.php:397
Definition: User.php:41
PrefContentView($NewValue=DB_NOVALUE)
Definition: CWUser.php:432
Set of privileges used to access resource information or other parts of the system.
PrefContentDensity($NewValue=DB_NOVALUE)
Definition: CWUser.php:429
PrefFontColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:420
PrefColorAvoidanceFlags($NewValue=DB_NOVALUE)
Definition: CWUser.php:426
Electronic mail message.
Definition: Email.php:14
Get($Field, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
Definition: Resource.php:410
__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:1541
const UIPREF_CAPTIONTYPE_VERBATIM
Definition: CWUser.php:408
PrefVisualDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:441
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:177
const U_OKAY
Definition: User.php:18
const UIPREF_CONTENTVIEW_IMAGEINTENSIVE
Definition: CWUser.php:399
const UIPREF_AVOID_RED
Definition: CWUser.php:382
PrefAudioDescriptionLevel($NewValue=DB_NOVALUE)
Definition: CWUser.php:435
$Result
Definition: User.php:978
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:404
const U_NOSUCHUSER
Definition: User.php:21
const UIPREF_CAPTIONTYPE_NONE
Definition: CWUser.php:407
$UserId
Definition: User.php:977
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:1142
const UIPREF_AVOID_PURPLEGREY
Definition: CWUser.php:388
const UIPREF_CONTENTDENSITY_OVERVIEW
Definition: CWUser.php:394
GetResource()
Get the associated user resource for this user.
Definition: CWUser.php:86
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:138
PrefFontSize($NewValue=DB_NOVALUE)
Definition: CWUser.php:414