public function loadUserByUsername($username) { $identityCheck = UserCommands::checkIdentity($username, '', $this->website); if (!$identityCheck->usernameExists) { throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); } $user = new UserModelWithPassword(); $user->readByUserName($username); if (!$identityCheck->usernameExistsOnThisSite and $user->role != SystemRoles::SYSTEM_ADMIN) { throw new AccessDeniedException(sprintf('Username "%s" not available on "%s". Use "Create an Account".', $username, $this->website->domain)); } $roles = array('ROLE_' . $user->role); if ($user->siteRole and $user->siteRole->offsetExists($this->website->domain) and $user->siteRole[$this->website->domain] !== SiteRoles::NONE) { $roles[] = 'ROLE_SITE_' . $user->siteRole[$this->website->domain]; } return new User($user->username, $user->password, $roles, $user->active, true, true, true); }
/** * * @param string $validationKey * @param array $params * @param Website $website */ public static function updateFromRegistration($validationKey, $params, $website) { $user = new \Api\Model\UserModelWithPassword(); if ($user->readByProperty('validationKey', $validationKey)) { if ($user->validate()) { $params['id'] = $user->id->asString(); JsonDecoder::decode($user, $params); $user->setPassword($params['password']); $user->validate(); $user->role = SystemRoles::USER; $user->siteRole[$website->domain] = $website->userDefaultSiteRole; $user->active = true; return $user->write(); } else { throw new \Exception("Sorry, your registration link has expired."); } } }
/** * Register a new user * @param array $params * @param string $captcha_info * @param Website $website * @param DeliveryInterface $delivery * @throws \Exception * @return string $userId */ public static function register($params, $captcha_info, $website, DeliveryInterface $delivery = null) { if (strtolower($captcha_info['code']) != strtolower($params['captcha'])) { return false; // captcha does not match } $user = new UserModel(); JsonDecoder::decode($user, $params); UserCommands::assertUniqueIdentity($user, $params['username'], $params['email'], $website); $user->active = false; $user->role = SystemRoles::USER; $user->siteRole[$website->domain] = $website->userDefaultSiteRole; if (!$user->emailPending) { if (!$user->email) { throw new \Exception("Error: no email set for user signup."); } $user->emailPending = $user->email; $user->email = ''; } $userId = $user->write(); // Write the password $userPassword = new UserModelWithPassword($userId); $userPassword->setPassword($params['password']); $userPassword->write(); // if website has a default project then add them to that project $project = ProjectModel::getDefaultProject($website); if ($project) { $project->addUser($user->id->asString(), ProjectRoles::CONTRIBUTOR); $user->addProject($project->id->asString()); $project->write(); $user->write(); } Communicate::sendSignup($user, $website, $delivery); return $userId; }