3 # FILE: CWUserFactory.php 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/ 15 # ---- PUBLIC INTERFACE -------------------------------------------------- 22 parent::__construct();
36 # assume no users will be found 40 $LastModField = $Schema->GetFieldByName(
"Last Modified By Id");
42 # fetch the top contributors 44 "SELECT UserId FROM ResourceUserInts " 45 .
" WHERE FieldId = ".$LastModField->Id()
47 .
" ORDER BY COUNT(*) DESC" 48 .
" LIMIT ".intval($Limit));
49 $UserIds = $this->DB->FetchColumn(
"UserId");
51 # for each user id found 52 foreach ($UserIds as $UserId)
54 $Users[$UserId] =
new CWUser($UserId);
57 # return the newest users 71 # assume no users will be found 75 $LastModField = $Schema->GetFieldByName(
"Last Modified By Id");
77 # fetch the top contributors 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");
87 # for each user id found 88 foreach ($UserIds as $UserId)
90 $Users[$UserId] =
new CWUser($UserId);
93 # return the newest users 109 # if there are necessary privileges for this privilege set 110 $ReqPrivs = $Privset->GetPossibleNecessaryPrivileges();
111 if (count($ReqPrivs))
113 # start with only those users who have at least one of those privileges 118 # start with all users 122 # determine of individual resources will need to be checked 123 $NeedToCheckResources =
124 (count($ResourceIds) && count($Privset->FieldsWithUserComparisons()))
127 # build up a list of matching users 128 $UsersThatMeetReqs = array();
130 $MemLimit = $GLOBALS[
"AF"]->GetPhpMemoryLimit();
133 # iterate over all the users 134 foreach ($UserIds as $UserId)
137 $User =
new CWUser($UserId);
139 if ($NeedToCheckResources)
141 # iterate over all the resources 142 foreach ($ResourceIds as $ResourceId)
144 # if we're running low on memory, nuke the resource cache 145 if ($GLOBALS[
"AF"]->GetFreeMemory() / $MemLimit
146 < self::$LowMemoryThresh)
152 if (!isset($ResourceCache[$ResourceId]))
154 $ResourceCache[$ResourceId] =
new Resource($ResourceId);
157 # if user meets requirements for set 158 if ($Privset->MeetsRequirements(
159 $User, $ResourceCache[$ResourceId]))
161 # add resource to user's list 162 $UsersThatMeetReqs[$UserId][] = $ResourceId;
168 # if user meets requirements for set 169 if ($Privset->MeetsRequirements($User))
172 $UsersThatMeetReqs[$UserId] = $ResourceIds;
177 # return IDs for users that meet requirements to caller 178 return $UsersThatMeetReqs;
188 $TrialName = explode(
'@', $Email);
189 $TrialName = array_shift($TrialName);
190 $TrialName = preg_replace(
"/[^A-Za-z0-9]/",
"", $TrialName);
191 $TrialName = strtolower($TrialName);
193 # if this email address is very short, we'll pad it with some random 195 if (strlen($TrialName) < 2)
197 $TrialName .= GetRandomCharacters(2,
"/[^a-hj-np-z0-9]/");
200 # see if the specified name exists 203 $Name = self::AppendSuffix($TrialName,
'');
205 while ($UFactory->UserNameExists($Name))
207 $Name = self::AppendSuffix(
208 $TrialName, GetRandomCharacters(2) );
225 foreach ($ResourcesPerUser as $UserId => $ResourceList)
227 $User =
new CWUser($UserId);
232 foreach ($ResourcesPerSchema as $SchemaId => $ResourceIds)
234 if (!isset($RFactories[$SchemaId]))
239 $VisibleResources = $RFactories[$SchemaId]->FilterNonViewableResources(
240 $ResourceIds, $User);
242 if (count($VisibleResources))
244 $UserResources[$SchemaId] = $VisibleResources;
248 if (count($UserResources))
259 # ---- OVERRIDDEN METHODS ------------------------------------------------ 274 $UserName, $Password, $PasswordAgain, $EMail, $EMailAgain,
275 $IgnoreErrorCodes = NULL)
277 # add the user to the APUsers table 278 $User = parent::CreateNewUser(
286 # user account creation did not succeed, so return the error codes 287 if (!($User instanceof
User))
292 # create the user resource 295 # set the user ID for the resource 296 $Resource->Set(
"UserId", $User->Id());
301 # update timestamps as required 302 foreach ($TimestampFields as $Field)
304 if ($Field->UpdateMethod()
307 $Resource->Set($Field,
"now");
311 # make the user resource permanent 312 $Resource->IsTempResource(FALSE);
314 # get the CWUser object for the user 315 $CWUser =
new CWUser(intval($User->Id()));
317 # couldn't get the CWUser object 318 if ($CWUser->Status() !=
U_OKAY)
320 return array($CWUser->Status());
323 # set up initial UI setting 324 $CWUser->Set(
"ActiveUI",
325 $GLOBALS[
"G_SysConfig"]->DefaultActiveUI());
327 # set up initial privileges 328 foreach ($GLOBALS[
"G_SysConfig"]->DefaultUserPrivs() as $Privilege)
330 $CWUser->GivePriv($Privilege);
333 # return new user object to caller 337 # ---- PRIVATE INTERFACE ------------------------------------------------- 348 private static function AppendSuffix($TrialName, $Suffix, $MaxLength=24)
350 if (strlen($TrialName.$Suffix)>$MaxLength)
353 $TrialName, 0, $MaxLength - strlen($Suffix));
356 return $TrialName.$Suffix;
364 private static $LowMemoryThresh = 0.25;
$ResourceFactory
The resource factory for user resources.
GetTopContributors($Limit=5)
Get a list of users sorted by how many resources they have added or edited, starting with those who h...
static FlattenMultiSchemaResourceList($ResourcesPerSchema)
Take an array keyed by SchemaId with elements giving arrays of ResourceIds and merge it to a flattene...
GetMostRecentContributors($Limit=5)
Get the users sorted by when they last added or edited a resource starting with those who added/edite...
FindUsersThatMeetRequirements($Privset, $ResourceIds=array())
Find all users that meet the requirements for a specified privilege set.
static BuildMultiSchemaResourceList($ResourceIds)
Take an array of ResourceIds and split it into an array keyed by SchemaId where the elements are arra...
GetUserIds()
Get IDs for all users.
GetUsersWithPrivileges()
Return array of user names who have the specified privileges.
CWIS-specific user factory class.
__construct()
Construct the user factory object.
CreateNewUser($UserName, $Password, $PasswordAgain, $EMail, $EMailAgain, $IgnoreErrorCodes=NULL)
Create a new user.
static FilterNonViewableResourcesFromPerUserLists($ResourcesPerUser)
Take a per-user list of resources and filter out the non-viewable ones.
Represents a "resource" in CWIS.
static GenerateUniqueUsernameFromEmail($Email)
Derive a unique username from an email address.
static Create($SchemaId)
Create a new resource.
Factory for Resource objects.
CWIS-specific user class.