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  # ---- 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 
113  public static function EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
114  {
115  # extract "From" address from supplied headers if available
116  if (strlen($AdditionalHeaders))
117  {
118  $HeaderLines = explode("\n", $AdditionalHeaders);
119  $Headers = array();
120  foreach ($HeaderLines as $Line)
121  {
122  $HeaderLine = trim($Line);
123  if (preg_match("/^from:/i", $Line))
124  {
125  $From = preg_replace("/^from:/i", "", $Line);
126  }
127  else
128  {
129  $Headers[] = $HeaderLine;
130  }
131  }
132  }
133 
134  # send message
135  $Msg = new Email();
136  if (isset($From)) { $Msg->From($From); }
137  $Msg->To($To);
138  $Msg->Subject($Subject);
139  $Msg->AddHeaders($Headers);
140  $Msg->Body($Message);
141  $Result = $Msg->Send();
142 
143  # report success to caller
144  return $Result;
145  }
146 
151  public static function GetCustomUserFields()
152  {
153  static $CustomFields;
154 
155  if (!isset($CustomFields))
156  {
157  $CustomFields = array();
159 
160  foreach ($Schema->GetFields() as $Field)
161  {
162  # they're custom if not owned by CWIS
163  if ($Field->Owner() != "CWISCore")
164  {
165  $CustomFields[$Field->Id()] = $Field;
166  }
167  }
168  }
169 
170  return $CustomFields;
171  }
172 
177  public static function GetDefaultUserFields()
178  {
179  static $DefaultFields;
180 
181  if (!isset($DefaultFields))
182  {
183  $DefaultFields = array();
185 
186  foreach ($Schema->GetFields() as $Field)
187  {
188  # they're default if owned by CWIS
189  if ($Field->Owner() != "CWISCore")
190  {
191  $DefaultFields[$Field->Id()] = $Field;
192  }
193  }
194  }
195 
196  return $DefaultFields;
197  }
198 
199  # ---- OVERRIDDEN METHODS ------------------------------------------------
200 
206  public function Delete()
207  {
208  # delete the associated user resource if set
209  if (isset($this->Resource))
210  {
211  $this->Resource->Delete();
212  $this->Result = U_OKAY;
213  }
214 
215  return parent::Delete();
216  }
217 
224  public function Get($FieldName)
225  {
226  # provide backwards-compatibility for data migrated to users fields as
227  # of CWIS 3.0.0
228  if (in_array($FieldName, self::$MigratedUserFields))
229  {
230  # return NULL if the resource object isn't set
231  if (!$this->IsResourceObjectSet())
232  {
233  return NULL;
234  }
235 
236  # return the value from the associated resource
237  return $this->Resource->Get($FieldName);
238  }
239 
240  # otherwise, get it from the APUsers table
241  return parent::Get($FieldName);
242  }
243 
250  public function Set($FieldName, $NewValue)
251  {
252  # provide backwards-compatibility for data migrated to users fields as
253  # of CWIS 3.0.0
254  if (in_array($FieldName, self::$MigratedUserFields))
255  {
256  # set the value only if the resource object is set
257  if ($this->IsResourceObjectSet())
258  {
259  $this->Resource->Set($FieldName, $NewValue);
260  $this->Result = U_OKAY;
261  }
262  }
263 
264  # transform boolean values to 1 or 0 because that's what the User
265  # class expects
266  if (is_bool($NewValue))
267  {
268  $NewValue = $NewValue ? 1 : 0;
269  }
270 
271  # update the APUsers table
272  return parent::Set($FieldName, $NewValue);
273  }
274 
275  # ---- PRIVATE INTERFACE -------------------------------------------------
276 
281  protected $Resource;
282 
287  protected static $MigratedUserFields = array(
288  "RealName", "WebSite", "AddressLineOne", "AddressLineTwo", "City",
289  "State", "ZipCode", "Country");
290 
297  protected function FetchAssociatedResource($UserId)
298  {
299  try
300  {
303  }
304 
305  # couldn't get the factory or schema, which probably means CWIS hasn't
306  # been installed yet
307  catch (Exception $Exception)
308  {
309  return U_ERROR;
310  }
311 
312  # the UserId field doesn't exist, which probably means CWIS hasn't been
313  # installed yet
314  if (!$Schema->FieldExists("UserId"))
315  {
316  return U_ERROR;
317  }
318 
319  # get matching resources, which should only be one
320  $Field = $Schema->GetFieldByName("UserId");
321  $ResourceIds = $Factory->GetItemIds("`".$Field->DBFieldName()
322  ."` = '".intval($UserId)."'");
323  $ResourceIdCount = count($ResourceIds);
324 
325  # no resource found
326  if ($ResourceIdCount < 1)
327  {
328  return U_NOSUCHUSER;
329  }
330 
331  # too many resources found
332  if ($ResourceIdCount > 1)
333  {
334  return U_ERROR;
335  }
336 
337  # construct the associated resource and return it
338  return new Resource(array_shift($ResourceIds));
339  }
340 
345  protected function IsResourceObjectSet()
346  {
347  # there must be a user ID, which is what the User class assumes, and the
348  # resource must be set
349  return isset($this->UserId) && isset($this->Resource);
350  }
351 
352 }
Set($FieldNameOrObject, $NewValue)
Set value using field name or field object.
Definition: Resource.php:965
Set($FieldName, $NewValue)
Set a value for the specified field.
Definition: CWUser.php:250
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:281
Privileges(PrivilegeSet $NewValue=NULL)
Get/set user privileges as a set.
Definition: CWUser.php:57
ResourceId()
Get the ID of the user resource associated with the user.
Definition: CWUser.php:88
Delete()
Delete the user and its associated user resource.
Definition: CWUser.php:206
Id()
Retrieve numerical resource ID.
Definition: Resource.php:252
FetchAssociatedResource($UserId)
Fetch the associated user resource based off of a user ID.
Definition: CWUser.php:297
IsResourceObjectSet()
Determine if the resource object for this object is set.
Definition: CWUser.php:345
Get($FieldName)
Get a value from the specified field.
Definition: CWUser.php:224
const U_NOSUCHUSER
Definition: Axis--User.php:22
Set of privileges used to access resource information or other parts of the system.
Electronic mail message.
Definition: Email.php:14
GetPrivList()
Definition: Axis--User.php:873
PHP
Definition: OAIClient.php:39
__construct($UserInfo=NULL)
Load user data from the given user info or from the session if available.
Definition: CWUser.php:21
const U_ERROR
Definition: Axis--User.php:20
static EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
Adapter method to bridge between AxisPHP User class and ScoutLib Email class.
Definition: CWUser.php:113
static SetEmailFunction($NewValue)
Set email function to use instead of mail().
Definition: Axis--User.php:178
Get($FieldNameOrObject, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
Definition: Resource.php:344
static $MigratedUserFields
Fields that were previously part of the APUsers table that have been migrated to the Resources table ...
Definition: CWUser.php:287
Represents a &quot;resource&quot; in CWIS.
Definition: Resource.php:13
static GetDefaultUserFields()
Get the default user fields.
Definition: CWUser.php:177
static GetCustomUserFields()
Get all custom user fields.
Definition: CWUser.php:151
const U_OKAY
Definition: Axis--User.php:19
SetPrivList($NewPrivileges)
Definition: Axis--User.php:884
GetResource()
Get the associated user resource for this user.
Definition: CWUser.php:98
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:139