CWIS Developer Documentation

- Home
- Main Page
- 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 # ---- 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
104 public function HasPriv($Privilege, $Privileges = NULL)
105 {
106 if ($Privilege instanceof PrivilegeSet)
107 {
108 $UserPrivs = $this->Privileges();
109 return $UserPrivs->IsGreaterThan($Privilege);
110 }
111 else
112 {
113 return call_user_func_array( "parent::HasPriv", func_get_args() );
114 }
115 }
116
127 public static function EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
128 {
129 # extract "From" address from supplied headers if available
130 if (strlen($AdditionalHeaders))
131 {
132 $HeaderLines = explode("\n", $AdditionalHeaders);
133 $Headers = array();
134 foreach ($HeaderLines as $Line)
135 {
136 $HeaderLine = trim($Line);
137 if (preg_match("/^from:/i", $Line))
138 {
139 $From = preg_replace("/^from:/i", "", $Line);
140 }
141 else
142 {
143 $Headers[] = $HeaderLine;
144 }
145 }
146 }
147
148 # send message
149 $Msg = new Email();
150 if (isset($From)) { $Msg->From($From); }
151 $Msg->To($To);
152 $Msg->Subject($Subject);
153 $Msg->AddHeaders($Headers);
154 $Msg->Body($Message);
155 $Result = $Msg->Send();
156
157 # report success to caller
158 return $Result;
159 }
160
165 public static function GetCustomUserFields()
166 {
167 static $CustomFields;
168
169 if (!isset($CustomFields))
170 {
171 $CustomFields = array();
172 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
173
174 foreach ($Schema->GetFields() as $Field)
175 {
176 # they're custom if not owned by CWIS
177 if ($Field->Owner() != "CWISCore")
178 {
179 $CustomFields[$Field->Id()] = $Field;
180 }
181 }
182 }
183
184 return $CustomFields;
185 }
186
191 public static function GetDefaultUserFields()
192 {
193 static $DefaultFields;
194
195 if (!isset($DefaultFields))
196 {
197 $DefaultFields = array();
198 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
199
200 foreach ($Schema->GetFields() as $Field)
201 {
202 # they're default if owned by CWIS
203 if ($Field->Owner() != "CWISCore")
204 {
205 $DefaultFields[$Field->Id()] = $Field;
206 }
207 }
208 }
209
210 return $DefaultFields;
211 }
212
213 # ---- OVERRIDDEN METHODS ------------------------------------------------
214
220 public function Delete()
221 {
222 # delete the associated user resource if set
223 if (isset($this->Resource))
224 {
225 $this->Resource->Delete();
226 $this->Result = U_OKAY;
227 }
228
229 return parent::Delete();
230 }
231
238 public function Get($FieldName)
239 {
240 # provide backwards-compatibility for data migrated to users fields as
241 # of CWIS 3.0.0
242 if (in_array($FieldName, self::$MigratedUserFields))
243 {
244 # return NULL if the resource object isn't set
245 if (!$this->IsResourceObjectSet())
246 {
247 return NULL;
248 }
249
250 # return the value from the associated resource
251 return $this->Resource->Get($FieldName);
252 }
253
254 # otherwise, get it from the APUsers table
255 return parent::Get($FieldName);
256 }
257
264 public function Set($FieldName, $NewValue)
265 {
266 # provide backwards-compatibility for data migrated to users fields as
267 # of CWIS 3.0.0
268 if (in_array($FieldName, self::$MigratedUserFields))
269 {
270 # set the value only if the resource object is set
271 if ($this->IsResourceObjectSet())
272 {
273 $this->Resource->Set($FieldName, $NewValue);
274 $this->Result = U_OKAY;
275 }
276 }
277
278 # transform boolean values to 1 or 0 because that's what the User
279 # class expects
280 if (is_bool($NewValue))
281 {
282 $NewValue = $NewValue ? 1 : 0;
283 }
284
285 # update the APUsers table
286 return parent::Set($FieldName, $NewValue);
287 }
288
289 # ---- PRIVATE INTERFACE -------------------------------------------------
290
295 protected $Resource;
296
301 protected static $MigratedUserFields = array(
302 "RealName", "WebSite", "AddressLineOne", "AddressLineTwo", "City",
303 "State", "ZipCode", "Country");
304
311 protected function FetchAssociatedResource($UserId)
312 {
313 try
314 {
315 $Factory = new ResourceFactory(MetadataSchema::SCHEMAID_USER);
316 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
317 }
318
319 # couldn't get the factory or schema, which probably means CWIS hasn't
320 # been installed yet
321 catch (Exception $Exception)
322 {
323 return U_ERROR;
324 }
325
326 # the UserId field doesn't exist, which probably means CWIS hasn't been
327 # installed yet
328 if (!$Schema->FieldExists("UserId"))
329 {
330 return U_ERROR;
331 }
332
333 # get matching resources, which should only be one
334 $Field = $Schema->GetFieldByName("UserId");
335 $ResourceIds = $Factory->GetItemIds("`".$Field->DBFieldName()
336 ."` = '".intval($UserId)."'");
337 $ResourceIdCount = count($ResourceIds);
338
339 # no resource found
340 if ($ResourceIdCount < 1)
341 {
342 return U_NOSUCHUSER;
343 }
344
345 # too many resources found
346 if ($ResourceIdCount > 1)
347 {
348 return U_ERROR;
349 }
350
351 # construct the associated resource and return it
352 return new Resource(array_shift($ResourceIds));
353 }
354
359 protected function IsResourceObjectSet()
360 {
361 # there must be a user ID, which is what the User class assumes, and the
362 # resource must be set
363 return isset($this->UserId) && isset($this->Resource);
364 }
365
366 }
Resource\Set
Set($FieldNameOrObject, $NewValue)
Set value using field name or field object.
Definition: Resource.php:965
CWUser\Set
Set($FieldName, $NewValue)
Set a value for the specified field.
Definition: CWUser.php:264
MetadataSchema
Metadata schema (in effect a Factory class for MetadataField).
Definition: MetadataSchema.php:13
CWUser\$Resource
$Resource
The user resource associated with the user or NULL if the user isn&#39;t logged in.
Definition: CWUser.php:295
CWUser\Privileges
Privileges(PrivilegeSet $NewValue=NULL)
Get/set user privileges as a set.
Definition: CWUser.php:57
CWUser\ResourceId
ResourceId()
Get the ID of the user resource associated with the user.
Definition: CWUser.php:88
CWUser\Delete
Delete()
Delete the user and its associated user resource.
Definition: CWUser.php:220
Resource\Id
Id()
Retrieve numerical resource ID.
Definition: Resource.php:252
CWUser\FetchAssociatedResource
FetchAssociatedResource($UserId)
Fetch the associated user resource based off of a user ID.
Definition: CWUser.php:311
CWUser\IsResourceObjectSet
IsResourceObjectSet()
Determine if the resource object for this object is set.
Definition: CWUser.php:359
CWUser\HasPriv
HasPriv($Privilege, $Privileges=NULL)
Definition: CWUser.php:104
CWUser\Get
Get($FieldName)
Get a value from the specified field.
Definition: CWUser.php:238
U_NOSUCHUSER
const U_NOSUCHUSER
Definition: Axis--User.php:22
MetadataSchema\SCHEMAID_USER
const SCHEMAID_USER
Definition: MetadataSchema.php:53
User
Definition: Axis--User.php:43
PrivilegeSet
Set of privileges used to access resource information or other parts of the system.
Definition: PrivilegeSet.php:16
Email
Electronic mail message.
Definition: Email.php:14
User\GetPrivList
GetPrivList()
Definition: Axis--User.php:916
PHP
PHP
Definition: OAIClient.php:39
CWUser\__construct
__construct($UserInfo=NULL)
Load user data from the given user info or from the session if available.
Definition: CWUser.php:21
U_ERROR
const U_ERROR
Definition: Axis--User.php:20
CWUser\EmailWrapper
static EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
Adapter method to bridge between AxisPHP User class and ScoutLib Email class.
Definition: CWUser.php:127
User\SetEmailFunction
static SetEmailFunction($NewValue)
Set email function to use instead of mail().
Definition: Axis--User.php:178
Resource\Get
Get($FieldNameOrObject, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
Definition: Resource.php:344
User\$Result
$Result
Definition: Axis--User.php:963
CWUser\$MigratedUserFields
static $MigratedUserFields
Fields that were previously part of the APUsers table that have been migrated to the Resources table ...
Definition: CWUser.php:301
Resource
Represents a &quot;resource&quot; in CWIS.
Definition: Resource.php:13
CWUser\GetDefaultUserFields
static GetDefaultUserFields()
Get the default user fields.
Definition: CWUser.php:191
User\Id
Id()
Definition: Axis--User.php:189
User\$UserId
$UserId
Definition: Axis--User.php:962
CWUser\GetCustomUserFields
static GetCustomUserFields()
Get all custom user fields.
Definition: CWUser.php:165
U_OKAY
const U_OKAY
Definition: Axis--User.php:19
User\SetPrivList
SetPrivList($NewPrivileges)
Definition: Axis--User.php:927
CWUser\GetResource
GetResource()
Get the associated user resource for this user.
Definition: CWUser.php:98
ResourceFactory
Factory for Resource objects.
Definition: ResourceFactory.php:13
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:139