CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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();
178 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
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();
204 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
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 {
323 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
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;
386 const UIPREF_AVOID_REDGREEN = 2;
387 const UIPREF_AVOID_BLUEYELLOW = 4;
388 const UIPREF_AVOID_GREENYELLOW = 8;
389 const UIPREF_AVOID_ORANGE = 16;
390 const UIPREF_AVOID_REDBLACK = 32;
391 const UIPREF_AVOID_PURPLEGREY = 64;
392 const UIPREF_AVOID_USEMAXMONOCHR = 128;
393
394 # content display options
395 const UIPREF_CONTENTDENSITY_NOPREFERENCE = 0;
396 const UIPREF_CONTENTDENSITY_DETAILED = 1;
397 const UIPREF_CONTENTDENSITY_OVERVIEW = 2;
398
399 # content view options
400 const UIPREF_CONTENTVIEW_NOPREFERENCE = 0;
401 const UIPREF_CONTENTVIEW_TEXTINTENSIVE = 1;
402 const UIPREF_CONTENTVIEW_IMAGEINTENSIVE = 2;
403
404 # audio description options
405 const UIPREF_AUDIODESCRIPTION_NONE = 0;
406 const UIPREF_AUDIODESCRIPTION_STANDARD = 1;
407 const UIPREF_AUDIODESCRIPTION_EXPANDED = 2;
408
409 # caption type options
410 const UIPREF_CAPTIONTYPE_NONE = 0;
411 const UIPREF_CAPTIONTYPE_VERBATIM = 1;
412 const UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL = 2;
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
441 function PrefAudioDescriptionLanguage($NewValue = DB_NOVALUE)
442 { return 0; }
443
444 function PrefVisualDescriptionLanguage($NewValue = DB_NOVALUE)
445 { return 0; }
446
447 function PrefImageDescriptionLanguage($NewValue = DB_NOVALUE)
448 { return 0; }
449
450 function PrefUseGraphicAlternatives($NewValue = DB_NOVALUE)
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 }
CWUser\PrefFontTypeFace
PrefFontTypeFace($NewValue=DB_NOVALUE)
Definition: CWUser.php:420
CWUser\PrefUseGraphicAlternatives
PrefUseGraphicAlternatives($NewValue=DB_NOVALUE)
Definition: CWUser.php:450
CWUser\UIPREF_AVOID_ORANGE
const UIPREF_AVOID_ORANGE
Definition: CWUser.php:389
CWUser\PrefCaptionType
PrefCaptionType($NewValue=DB_NOVALUE)
Definition: CWUser.php:456
CWUser\Set
Set($FieldName, $NewValue)
Set a value for the specified field.
Definition: CWUser.php:270
CWUser\UIPREF_AVOID_BLUEYELLOW
const UIPREF_AVOID_BLUEYELLOW
Definition: CWUser.php:387
MetadataSchema
Metadata schema (in effect a Factory class for MetadataField).
Definition: MetadataSchema.php:13
CWUser\UIPREF_CONTENTVIEW_TEXTINTENSIVE
const UIPREF_CONTENTVIEW_TEXTINTENSIVE
Definition: CWUser.php:401
CWUser\UIPREF_AVOID_REDBLACK
const UIPREF_AVOID_REDBLACK
Definition: CWUser.php:390
CWUser\PrefImageDescriptionLanguage
PrefImageDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:447
CWUser\$Resource
$Resource
The user resource associated with the user or NULL if the user isn&#39;t logged in.
Definition: CWUser.php:301
CWUser\Privileges
Privileges(PrivilegeSet $NewValue=NULL)
THIS FUNCTION HAS BEEN DEPRECATED This provides compatibility for interfaces written to use a version...
Definition: CWUser.php:59
CWUser\ResourceId
ResourceId()
Get the ID of the user resource associated with the user.
Definition: CWUser.php:76
CWUser\UIPREF_AVOID_REDGREEN
const UIPREF_AVOID_REDGREEN
Definition: CWUser.php:386
CWUser\Delete
Delete()
Delete the user and its associated user resource.
Definition: CWUser.php:226
CWUser\UIPREF_AVOID_GREENYELLOW
const UIPREF_AVOID_GREENYELLOW
Definition: CWUser.php:388
CWUser\UIPREF_AUDIODESCRIPTION_NONE
const UIPREF_AUDIODESCRIPTION_NONE
Definition: CWUser.php:405
CWUser\UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL
const UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL
Definition: CWUser.php:412
CWUser\PrefCaptionRate
PrefCaptionRate($NewValue=DB_NOVALUE)
Definition: CWUser.php:459
CWUser\PrefBackgroundColor
PrefBackgroundColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:426
Resource\Id
Id()
Retrieve numerical resource ID.
Definition: Resource.php:291
CWUser\PrefSignLanguage
PrefSignLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:453
CWUser\FetchAssociatedResource
FetchAssociatedResource($UserId)
Fetch the associated user resource based off of a user ID.
Definition: CWUser.php:317
CWUser\IsResourceObjectSet
IsResourceObjectSet()
Determine if the resource object for this object is set.
Definition: CWUser.php:373
U_ERROR
const U_ERROR
Definition: User.php:19
CWUser\UIPREF_AVOID_USEMAXMONOCHR
const UIPREF_AVOID_USEMAXMONOCHR
Definition: CWUser.php:392
CWUser\UIPREF_CONTENTDENSITY_NOPREFERENCE
const UIPREF_CONTENTDENSITY_NOPREFERENCE
Definition: CWUser.php:395
CWUser\HasPriv
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
CWUser\UIPREF_AUDIODESCRIPTION_STANDARD
const UIPREF_AUDIODESCRIPTION_STANDARD
Definition: CWUser.php:406
CWUser\Get
Get($FieldName)
Get a value from the specified field.
Definition: CWUser.php:244
CWUser\PrefAudioDescriptionLanguage
PrefAudioDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:441
MetadataSchema\SCHEMAID_USER
const SCHEMAID_USER
Definition: MetadataSchema.php:55
CWUser\UIPREF_CONTENTDENSITY_DETAILED
const UIPREF_CONTENTDENSITY_DETAILED
Definition: CWUser.php:396
CWUser\UIPREF_CONTENTVIEW_NOPREFERENCE
const UIPREF_CONTENTVIEW_NOPREFERENCE
Definition: CWUser.php:400
User
Definition: User.php:48
CWUser\PrefContentView
PrefContentView($NewValue=DB_NOVALUE)
Definition: CWUser.php:435
PrivilegeSet
Set of privileges used to access resource information or other parts of the system.
Definition: PrivilegeSet.php:16
CWUser\PrefContentDensity
PrefContentDensity($NewValue=DB_NOVALUE)
Definition: CWUser.php:432
CWUser\PrefFontColor
PrefFontColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:423
CWUser\PrefColorAvoidanceFlags
PrefColorAvoidanceFlags($NewValue=DB_NOVALUE)
Definition: CWUser.php:429
Email
Electronic mail message.
Definition: Email.php:14
Resource\Get
Get($Field, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
Definition: Resource.php:415
PHP
PHP
Definition: RSSClient--Test.php:12
CWUser\__construct
__construct($UserInfo=NULL)
Load user data from the given user info or from the session if available.
Definition: CWUser.php:22
DB_NOVALUE
const DB_NOVALUE
Definition: Database.php:1675
CWUser\UIPREF_CAPTIONTYPE_VERBATIM
const UIPREF_CAPTIONTYPE_VERBATIM
Definition: CWUser.php:411
CWUser\PrefVisualDescriptionLanguage
PrefVisualDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:444
CWUser\EmailWrapper
static EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
Adapter method to bridge between AxisPHP User class and ScoutLib Email class.
Definition: CWUser.php:133
User\SetEmailFunction
static SetEmailFunction($NewValue)
Set email function to use instead of mail().
Definition: User.php:210
U_OKAY
const U_OKAY
Definition: User.php:18
CWUser\UIPREF_CONTENTVIEW_IMAGEINTENSIVE
const UIPREF_CONTENTVIEW_IMAGEINTENSIVE
Definition: CWUser.php:402
CWUser\UIPREF_AVOID_RED
const UIPREF_AVOID_RED
Definition: CWUser.php:385
CWUser\PrefAudioDescriptionLevel
PrefAudioDescriptionLevel($NewValue=DB_NOVALUE)
Definition: CWUser.php:438
User\$Result
$Result
Definition: User.php:1076
PrivilegeSetCompatibilityShim
Compatibility layer allowing interfaces built against the privilege system from CWIS 3...
Definition: PrivilegeSetCompatibilityShim.php:15
CWUser\$MigratedUserFields
static $MigratedUserFields
Fields that were previously part of the APUsers table that have been migrated to the Resources table ...
Definition: CWUser.php:307
Resource
Represents a "resource" in CWIS.
Definition: Resource.php:13
CWUser\GetDefaultUserFields
static GetDefaultUserFields()
Get the default user fields.
Definition: CWUser.php:197
CWUser\UIPREF_AUDIODESCRIPTION_EXPANDED
const UIPREF_AUDIODESCRIPTION_EXPANDED
Definition: CWUser.php:407
U_NOSUCHUSER
const U_NOSUCHUSER
Definition: User.php:21
CWUser\UIPREF_CAPTIONTYPE_NONE
const UIPREF_CAPTIONTYPE_NONE
Definition: CWUser.php:410
User\$UserId
$UserId
Definition: User.php:1075
CWUser\GetCustomUserFields
static GetCustomUserFields()
Get all custom user fields.
Definition: CWUser.php:171
Resource\Set
Set($Field, $NewValue, $Reset=FALSE)
Set value using field name or field object.
Definition: Resource.php:1141
CWUser\UIPREF_AVOID_PURPLEGREY
const UIPREF_AVOID_PURPLEGREY
Definition: CWUser.php:391
CWUser\UIPREF_CONTENTDENSITY_OVERVIEW
const UIPREF_CONTENTDENSITY_OVERVIEW
Definition: CWUser.php:397
CWUser\GetResource
GetResource()
Get the associated user resource for this user.
Definition: CWUser.php:86
CWUser
CWIS-specific user class.
Definition: CWUser.php:13
Resource\Delete
Delete()
Remove resource (and accompanying associations) from database and delete any associated files...
Definition: Resource.php:142
CWUser\PrefFontSize
PrefFontSize($NewValue=DB_NOVALUE)
Definition: CWUser.php:417