📄 user.class.php
字号:
/** * This function will generate new user password, set it and return it * * @param boolean $save Save object after the update * @return string */ function resetPassword($save = true) { $new_password = substr(sha1(uniqid(rand(), true)), rand(0, 25), 13); $this->setPassword($new_password); if ($save) { $this->save(); } // if return $new_password; } // resetPassword /** * Set password value * * @param string $value * @return boolean */ function setPassword($value) { do { $salt = substr(sha1(uniqid(rand(), true)), rand(0, 25), 13); $token = sha1($salt . $value); } while (Users::tokenExists($token)); $this->setToken($token); $this->setSalt($salt); $this->setTwister(StringTwister::getTwister()); } // setPassword /** * Return twisted token * * @param void * @return string */ function getTwistedToken() { return StringTwister::twistHash($this->getToken(), $this->getTwister()); } // getTwistedToken /** * Check if $check_password is valid user password * * @param string $check_password * @return boolean */ function isValidPassword($check_password) { return sha1($this->getSalt() . $check_password) == $this->getToken(); } // isValidPassword /** * Check if $twisted_token is valid for this user account * * @param string $twisted_token * @return boolean */ function isValidToken($twisted_token) { return StringTwister::untwistHash($twisted_token, $this->getTwister()) == $this->getToken(); } // isValidToken // --------------------------------------------------- // Permissions // --------------------------------------------------- /** * Can specific user add user to specific company * * @access public * @param User $user * @param Company $to Can user add user to this company * @return boolean */ function canAdd(User $user, Company $to) { if ($user->isAccountOwner()) { return true; } // if return $user->isAdministrator(); } // canAdd /** * Check if specific user can update this user account * * @access public * @param User $user * @return boolean */ function canEdit(User $user) { if ($user->getId() == $this->getId()) { return true; // account owner } // if if ($user->isAccountOwner()) { return true; } // if return $user->isAdministrator(); } // canEdit /** * Check if specific user can delete specific account * * @param User $user * @return boolean */ function canDelete(User $user) { if ($this->isAccountOwner()) { return false; // can't delete accountowner } // if if ($this->getId() == $user->getId()) { return false; // can't delete self } // if return $user->isAdministrator(); } // canDelete /** * Returns true if this user can see $user * * @param User $user * @return boolean */ function canSeeUser(User $user) { if ($this->isMemberOfOwnerCompany()) { return true; // see all } // if if ($user->getCompanyId() == $this->getCompanyId()) { return true; // see members of your own company } // if if ($user->isMemberOfOwnerCompany()) { return true; // see members of owner company } // if return false; } // canSeeUser /** * Returns true if this user can see $company. Members of owener company and * coworkers are visible without project check! Also, members of owner company * can see all clients without any prior check! * * @param Company $company * @return boolean */ function canSeeCompany(Company $company) { if ($this->isMemberOfOwnerCompany()) { return true; } // if if (isset($this->visible_companies[$company->getId()])) { return $this->visible_companies[$company->getId()]; } // if if ($company->isOwner()) { $this->visible_companies[$company->getId()] = true; return true; } // if if ($this->getCompanyId() == $company->getId()) { $this->visible_companies[$company->getId()] = true; return true; } // if // Lets companye projects for company of this user and for $company and // compare if we have projects where both companies work together $projects_1 = DB::executeAll("SELECT `project_id` FROM " . ProjectCompanies::instance()->getTableName(true) . " WHERE `company_id` = ?", $this->getCompanyId()); $projects_2 = DB::executeAll("SELECT `project_id` FROM " . ProjectCompanies::instance()->getTableName(true) . " WHERE `company_id` = ?", $company->getId()); if (!is_array($projects_1) || !is_array($projects_2)) { $this->visible_companies[$company->getId()] = false; return false; } // if foreach ($projects_1 as $project_id) { if (in_array($project_id, $projects_2)) { $this->visible_companies[$company->getId()] = true; return true; } // if } // foreach $this->visible_companies[$company->getId()] = false; return false; } // canSeeCompany /** * Check if specific user can update this profile * * @param User $user * @return boolean */ function canUpdateProfile(User $user) { if ($this->getId() == $user->getId()) { return true; } // if if ($user->isAdministrator()) { return true; } // if return false; } // canUpdateProfile /** * Check if this user can update this users permissions * * @param User $user * @return boolean */ function canUpdatePermissions(User $user) { if ($this->isAccountOwner()) { return false; // noone will touch this } // if return $user->isAdministrator(); } // canUpdatePermissions /** * Check if this user is company administration (used to check many other permissions). User must * be part of the company and have is_admin stamp set to true * * @access public * @param Company $company * @return boolean */ function isCompanyAdmin(Company $company) { return ($this->getCompanyId() == $company->getId()) && $this->getIsAdmin(); } // isCompanyAdmin /** * Return project permission for specific user if he is on project. In case of any error $default is returned * * @access public * @param Project $project * @param string $permission Permission name * @param boolean $default Default value * @return boolean */ function getProjectPermission(Project $project, $permission, $default = false) { static $valid_permissions = null; if (is_null($valid_permissions)) { $valid_permissions = ProjectUsers::getPermissionColumns(); } // if if (!in_array($permission, $valid_permissions)) { return $default; } // if $project_user = ProjectUsers::findById(array( 'project_id' => $project->getId(), 'user_id' => $this->getId() )); // findById if (!($project_user instanceof ProjectUser)) { return $default; } // if $getter = 'get' . Inflector::camelize($permission); return $project_user->$getter(); } // getProjectPermission // --------------------------------------------------- // URLs // --------------------------------------------------- /** * Return view account URL of this user * * @access public * @param void * @return string */ function getAccountUrl() { return get_url('account', 'index'); } // getAccountUrl /** * Show company card page * * @access public * @param void * @return null */ function getCardUrl() { return get_url('user', 'card', $this->getId()); } // getCardUrl /** * Return edit user URL * * @access public * @param void * @return string */ function getEditUrl() { return get_url('user', 'edit', $this->getId()); } // getEditUrl /** * Return delete user URL * * @access public * @param void * @return string */ function getDeleteUrl() { return get_url('user', 'delete', $this->getId()); } // getDeleteUrl /** * Return edit profile URL * * @param string $redirect_to URL where we need to redirect user when he updates profile * @return string */ function getEditProfileUrl($redirect_to = null) { $attributes = array('id' => $this->getId()); if (trim($redirect_to) <> '') { $attributes['redirect_to'] = str_replace('&', '&', trim($redirect_to)); } // if return get_url('account', 'edit_profile', $attributes); } // getEditProfileUrl /** * Edit users password * * @param string $redirect_to URL where we need to redirect user when he updates password * @return null */ function getEditPasswordUrl($redirect_to = null) { $attributes = array('id' => $this->getId()); if (trim($redirect_to) <> '') { $attributes['redirect_to'] = str_replace('&', '&', trim($redirect_to)); } // if return get_url('account', 'edit_password', $attributes); } // getEditPasswordUrl /** * Return update user permissions page URL * * @param string $redirect_to * @return string */ function getUpdatePermissionsUrl($redirect_to = null) { $attributes = array('id' => $this->getId()); if (trim($redirect_to) <> '') { $attributes['redirect_to'] = str_replace('&', '&', trim($redirect_to)); } // if return get_url('account', 'update_permissions', $attributes); } // getUpdatePermissionsUrl /** * Return update avatar URL * * @param string * @return string */ function getUpdateAvatarUrl($redirect_to = null) { $attributes = array('id' => $this->getId()); if (trim($redirect_to) <> '') { $attributes['redirect_to'] = str_replace('&', '&', trim($redirect_to)); } // if return get_url('account', 'edit_avatar', $attributes); } // getUpdateAvatarUrl /** * Return delete avatar URL * * @param void * @return string */ function getDeleteAvatarUrl($redirect_to = null) { $attributes = array('id' => $this->getId()); if (trim($redirect_to) <> '') { $attributes['redirect_to'] = str_replace('&', '&', trim($redirect_to)); } // if return get_url('account', 'delete_avatar', $attributes); } // getDeleteAvatarUrl /** * Return recent activities feed URL * * If $project is valid project instance URL will be limited for that project only, else it will be returned for * overal feed * * @param Project $project * @return string */ function getRecentActivitiesFeedUrl($project = null) { $params = array( 'id' => $this->getId(), 'token' => $this->getTwistedToken(), ); // array if ($project instanceof Project) { $params['project'] = $project->getId(); return get_url('feed', 'project_activities', $params, null, false); } else { return get_url('feed', 'recent_activities', $params, null, false); } // if } // getRecentActivitiesFeedUrl /** * Return iCalendar URL * * If $project is valid project instance calendar will be rendered just for that project, else it will be rendered * for all active projects this user is involved with * * @param Project $project * @return string */ function getICalendarUrl($project = null) { $params = array( 'id' => $this->getId(), 'token' => $this->getTwistedToken(), ); // array if ($project instanceof Project) { $params['project'] = $project->getId(); return get_url('feed', 'project_ical', $params, null, false); } else { return get_url('feed', 'user_ical', $params, null, false); } // if } // getICalendarUrl // --------------------------------------------------- // System functions // --------------------------------------------------- /** * Validate data before save * * @access public * @param array $errors * @return void */ function validate(&$errors) { // Validate username if present if ($this->validatePresenceOf('username')) { if (!$this->validateUniquenessOf('username')) { $errors[] = lang('username must be unique'); } } else { $errors[] = lang('username value required'); } // if if (!$this->validatePresenceOf('token')) { $errors[] = lang('password value required'); } // Validate email if present if ($this->validatePresenceOf('email')) { if (!$this->validateFormatOf('email', EMAIL_FORMAT)) { $errors[] = lang('invalid email address'); } if (!$this->validateUniquenessOf('email')) { $errors[] = lang('email address must be unique'); } } else { $errors[] = lang('email value is required'); } // if // Company ID if (!$this->validatePresenceOf('company_id')) { $errors[] = lang('company value required'); } } // validate /** * Delete this object * * @param void * @return boolean */ function delete() { if ($this->isAccountOwner()) { return false; } // if $this->deleteAvatar(); ProjectUsers::clearByUser($this); MessageSubscriptions::clearByUser($this); return parent::delete(); } // delete // --------------------------------------------------- // ApplicationDataObject implementation // --------------------------------------------------- /** * Return object name * * @access public * @param void * @return string */ function getObjectName() { return $this->getDisplayName(); } // getObjectName /** * Return object type name * * @param void * @return string */ function getObjectTypeName() { return lang('user'); } // getObjectTypeName /** * Return object URl * * @access public * @param void * @return string */ function getObjectUrl() { return $this->getCardUrl(); } // getObjectUrl } // User ?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -