CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

CWUserFactory.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: CWUserFactory.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 CWUserFactory extends UserFactory
14 {
15 # ---- PUBLIC INTERFACE --------------------------------------------------
16
20 public function __construct()
21 {
22 parent::__construct();
23 $this->ResourceFactory = new ResourceFactory(MetadataSchema::SCHEMAID_USER);
24 }
25
34 public function GetTopContributors($Limit = 5)
35 {
36 # assume no users will be found
37 $Users = array();
38
39 $Schema = new MetadataSchema();
40 $LastModField = $Schema->GetFieldByName("Last Modified By Id");
41
42 # fetch the top contributors
43 $this->DB->Query(
44 "SELECT UserId FROM ResourceUserInts "
45 ." WHERE FieldId = ".$LastModField->Id()
46 ." GROUP BY UserId"
47 ." ORDER BY COUNT(*) DESC"
48 ." LIMIT ".intval($Limit));
49 $UserIds = $this->DB->FetchColumn("UserId");
50
51 # for each user id found
52 foreach ($UserIds as $UserId)
53 {
54 $Users[$UserId] = new CWUser($UserId);
55 }
56
57 # return the newest users
58 return $Users;
59 }
60
69 public function GetMostRecentContributors($Limit = 5)
70 {
71 # assume no users will be found
72 $Users = array();
73
74 $Schema = new MetadataSchema();
75 $LastModField = $Schema->GetFieldByName("Last Modified By Id");
76
77 # fetch the top contributors
78 $this->DB->Query(
79 "SELECT UserId FROM ResourceUserInts RU, Resources R "
80 ." WHERE RU.FieldId = ".$LastModField->Id()
81 ." AND R.ResourceId = RU.ResourceId "
82 ." GROUP BY RU.UserId"
83 ." ORDER BY MAX(R.DateLastModified) DESC"
84 ." LIMIT ".intval($Limit));
85 $UserIds = $this->DB->FetchColumn("UserId");
86
87 # for each user id found
88 foreach ($UserIds as $UserId)
89 {
90 $Users[$UserId] = new CWUser($UserId);
91 }
92
93 # return the newest users
94 return $Users;
95 }
96
107 public function FindUsersThatMeetRequirements($Privset, $ResourceIds = array())
108 {
109 # if there are necessary privileges for this privilege set
110 $ReqPrivs = $Privset->GetPossibleNecessaryPrivileges();
111 if (count($ReqPrivs))
112 {
113 # start with only those users who have at least one of those privileges
114 $UserIds = array_keys($this->GetUsersWithPrivileges($ReqPrivs));
115 }
116 else
117 {
118 # start with all users
119 $UserIds = $this->GetUserIds();
120 }
121
122 # determine of individual resources will need to be checked
123 $NeedToCheckResources =
124 (count($ResourceIds) && count($Privset->FieldsWithUserComparisons()))
125 ? TRUE : FALSE ;
126
127 # build up a list of matching users
128 $UsersThatMeetReqs = array();
129
130 $MemLimit = $GLOBALS["AF"]->GetPhpMemoryLimit();
131 $ResourceCache = [];
132
133 # iterate over all the users
134 foreach ($UserIds as $UserId)
135 {
136 # load user
137 $User = new CWUser($UserId);
138
139 if ($NeedToCheckResources)
140 {
141 # iterate over all the resources
142 foreach ($ResourceIds as $ResourceId)
143 {
144 # if we're running low on memory, nuke the resource cache
145 if ($GLOBALS["AF"]->GetFreeMemory() / $MemLimit
146 < self::$LowMemoryThresh)
147 {
148 $ResourceCache = [];
149 }
150
151 # load resource
152 if (!isset($ResourceCache[$ResourceId]))
153 {
154 $ResourceCache[$ResourceId] = new Resource($ResourceId);
155 }
156
157 # if user meets requirements for set
158 if ($Privset->MeetsRequirements(
159 $User, $ResourceCache[$ResourceId]))
160 {
161 # add resource to user's list
162 $UsersThatMeetReqs[$UserId][] = $ResourceId;
163 }
164 }
165 }
166 else
167 {
168 # if user meets requirements for set
169 if ($Privset->MeetsRequirements($User))
170 {
171 # add user to list
172 $UsersThatMeetReqs[$UserId] = $ResourceIds;
173 }
174 }
175 }
176
177 # return IDs for users that meet requirements to caller
178 return $UsersThatMeetReqs;
179 }
180
186 public static function GenerateUniqueUsernameFromEmail($Email)
187 {
188 $TrialName = explode('@', $Email);
189 $TrialName = array_shift($TrialName);
190 $TrialName = preg_replace("/[^A-Za-z0-9]/", "", $TrialName);
191 $TrialName = strtolower($TrialName);
192
193 # if this email address is very short, we'll pad it with some random
194 # characters
195 if (strlen($TrialName) < 2)
196 {
197 $TrialName .= GetRandomCharacters(2, "/[^a-hj-np-z0-9]/");
198 }
199
200 # see if the specified name exists
201 $UFactory = new UserFactory();
202
203 $Name = self::AppendSuffix($TrialName, '');
204
205 while ($UFactory->UserNameExists($Name))
206 {
207 $Name = self::AppendSuffix(
208 $TrialName, GetRandomCharacters(2) );
209 }
210
211 return $Name;
212 }
213
220 public static function FilterNonViewableResourcesFromPerUserLists($ResourcesPerUser)
221 {
222 $Result = [];
223
224 $RFactories = [];
225 foreach ($ResourcesPerUser as $UserId => $ResourceList)
226 {
227 $User = new CWUser($UserId);
228 $UserResources = [];
229
230 $ResourcesPerSchema = ResourceFactory::BuildMultiSchemaResourceList(
231 $ResourceList);
232 foreach ($ResourcesPerSchema as $SchemaId => $ResourceIds)
233 {
234 if (!isset($RFactories[$SchemaId]))
235 {
236 $RFactories[$SchemaId] = new ResourceFactory($SchemaId);
237 }
238
239 $VisibleResources = $RFactories[$SchemaId]->FilterNonViewableResources(
240 $ResourceIds, $User);
241
242 if (count($VisibleResources))
243 {
244 $UserResources[$SchemaId] = $VisibleResources;
245 }
246 }
247
248 if (count($UserResources))
249 {
250 $Result[$UserId] = ResourceFactory::FlattenMultiSchemaResourceList(
251 $UserResources);
252 }
253 }
254
255 return $Result;
256 }
257
258
259 # ---- OVERRIDDEN METHODS ------------------------------------------------
260
273 public function CreateNewUser(
274 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain,
275 $IgnoreErrorCodes = NULL)
276 {
277 # add the user to the APUsers table
278 $User = parent::CreateNewUser(
279 $UserName,
280 $Password,
281 $PasswordAgain,
282 $EMail,
283 $EMailAgain,
284 $IgnoreErrorCodes);
285
286 # user account creation did not succeed, so return the error codes
287 if (!($User instanceof User))
288 {
289 return $User;
290 }
291
292 # create the user resource
293 $Resource = Resource::Create(MetadataSchema::SCHEMAID_USER);
294
295 # set the user ID for the resource
296 $Resource->Set("UserId", $User->Id());
297
298 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
299 $TimestampFields = $Schema->GetFields(MetadataSchema::MDFTYPE_TIMESTAMP);
300
301 # update timestamps as required
302 foreach ($TimestampFields as $Field)
303 {
304 if ($Field->UpdateMethod()
305 == MetadataField::UPDATEMETHOD_ONRECORDCREATE)
306 {
307 $Resource->Set($Field, "now");
308 }
309 }
310
311 # make the user resource permanent
312 $Resource->IsTempResource(FALSE);
313
314 # get the CWUser object for the user
315 $CWUser = new CWUser(intval($User->Id()));
316
317 # couldn't get the CWUser object
318 if ($CWUser->Status() != U_OKAY)
319 {
320 return array($CWUser->Status());
321 }
322
323 # set up initial UI setting
324 $CWUser->Set("ActiveUI",
325 $GLOBALS["G_SysConfig"]->DefaultActiveUI());
326
327 # set up initial privileges
328 foreach ($GLOBALS["G_SysConfig"]->DefaultUserPrivs() as $Privilege)
329 {
330 $CWUser->GivePriv($Privilege);
331 }
332
333 # return new user object to caller
334 return $CWUser;
335 }
336
337 # ---- PRIVATE INTERFACE -------------------------------------------------
338
348 private static function AppendSuffix($TrialName, $Suffix, $MaxLength=24)
349 {
350 if (strlen($TrialName.$Suffix)>$MaxLength)
351 {
352 $TrialName = substr(
353 $TrialName, 0, $MaxLength - strlen($Suffix));
354 }
355
356 return $TrialName.$Suffix;
357 }
358
362 protected $ResourceFactory;
363
364 private static $LowMemoryThresh = 0.25;
365 }
MetadataSchema\MDFTYPE_TIMESTAMP
const MDFTYPE_TIMESTAMP
Definition: MetadataSchema.php:25
CWUserFactory\$ResourceFactory
$ResourceFactory
The resource factory for user resources.
Definition: CWUserFactory.php:362
CWUserFactory\GetTopContributors
GetTopContributors($Limit=5)
Get a list of users sorted by how many resources they have added or edited, starting with those who h...
Definition: CWUserFactory.php:34
ResourceFactory\FlattenMultiSchemaResourceList
static FlattenMultiSchemaResourceList($ResourcesPerSchema)
Take an array keyed by SchemaId with elements giving arrays of ResourceIds and merge it to a flattene...
Definition: ResourceFactory.php:968
CWUserFactory\GetMostRecentContributors
GetMostRecentContributors($Limit=5)
Get the users sorted by when they last added or edited a resource starting with those who added/edite...
Definition: CWUserFactory.php:69
MetadataSchema
Metadata schema (in effect a Factory class for MetadataField).
Definition: MetadataSchema.php:13
CWUserFactory\FindUsersThatMeetRequirements
FindUsersThatMeetRequirements($Privset, $ResourceIds=array())
Find all users that meet the requirements for a specified privilege set.
Definition: CWUserFactory.php:107
ResourceFactory\BuildMultiSchemaResourceList
static BuildMultiSchemaResourceList($ResourceIds)
Take an array of ResourceIds and split it into an array keyed by SchemaId where the elements are arra...
Definition: ResourceFactory.php:987
UserFactory\GetUserIds
GetUserIds()
Get IDs for all users.
Definition: UserFactory.php:214
MetadataSchema\SCHEMAID_USER
const SCHEMAID_USER
Definition: MetadataSchema.php:55
User
Definition: User.php:48
UserFactory\GetUsersWithPrivileges
GetUsersWithPrivileges()
Return array of user names who have the specified privileges.
Definition: UserFactory.php:291
CWUserFactory
CWIS-specific user factory class.
Definition: CWUserFactory.php:13
CWUserFactory\__construct
__construct()
Construct the user factory object.
Definition: CWUserFactory.php:20
PHP
PHP
Definition: RSSClient--Test.php:12
UserFactory
Definition: UserFactory.php:18
CWUserFactory\CreateNewUser
CreateNewUser($UserName, $Password, $PasswordAgain, $EMail, $EMailAgain, $IgnoreErrorCodes=NULL)
Create a new user.
Definition: CWUserFactory.php:273
U_OKAY
const U_OKAY
Definition: User.php:18
CWUserFactory\FilterNonViewableResourcesFromPerUserLists
static FilterNonViewableResourcesFromPerUserLists($ResourcesPerUser)
Take a per-user list of resources and filter out the non-viewable ones.
Definition: CWUserFactory.php:220
Resource
Represents a "resource" in CWIS.
Definition: Resource.php:13
CWUserFactory\GenerateUniqueUsernameFromEmail
static GenerateUniqueUsernameFromEmail($Email)
Derive a unique username from an email address.
Definition: CWUserFactory.php:186
MetadataField\UPDATEMETHOD_ONRECORDCREATE
const UPDATEMETHOD_ONRECORDCREATE
Definition: MetadataField.php:19
Resource\Create
static Create($SchemaId)
Create a new resource.
Definition: Resource.php:48
ResourceFactory
Factory for Resource objects.
Definition: ResourceFactory.php:13
CWUser
CWIS-specific user class.
Definition: CWUser.php:13