public function xSaveAction() { $this->request->defineParams(array('password' => array('type' => 'string', 'rawValue' => true))); $sendResetLink = $newUser = false; $user = Scalr_Account_User::init(); $validator = new Validator(); $email = $this->getParam('email'); if (($isEmailValid = $validator->validateEmail($email)) !== true) { throw new Scalr_Exception_Core($isEmailValid); } if ($this->user->canManageAcl() || $this->user->isTeamOwner()) { $password = $this->getParam('password'); if (empty($password)) { $password = CryptoTool::sault(10); $sendResetLink = true; } if (($isPasswordValid = $validator->validatePassword($password)) !== true) { throw new Scalr_Exception_Core($isPasswordValid); } if ($this->getParam('id')) { $user->loadById((int) $this->getParam('id')); if (!$this->user->canEditUser($user)) { throw new Scalr_Exception_InsufficientPermissions(); } $user->updateEmail($email); } else { $this->user->getAccount()->validateLimit(Scalr_Limits::ACCOUNT_USERS, 1); $user->create($email, $this->user->getAccountId()); $user->type = Scalr_Account_User::TYPE_TEAM_USER; $newUser = true; } if ($password != '******') { $user->updatePassword($password); } if (in_array($this->getParam('status'), array(Scalr_Account_User::STATUS_ACTIVE, Scalr_Account_User::STATUS_INACTIVE)) && !$user->isAccountOwner()) { $user->status = $this->getParam('status'); } $user->fullname = $this->getParam('fullname'); $user->comments = $this->getParam('comments'); $user->save(); if ($this->getParam('enableApi')) { $keys = Scalr::GenerateAPIKeys(); $user->setSetting(Scalr_Account_User::SETTING_API_ENABLED, true); $user->setSetting(Scalr_Account_User::SETTING_API_ACCESS_KEY, $keys['id']); $user->setSetting(Scalr_Account_User::SETTING_API_SECRET_KEY, $keys['key']); } if ($newUser) { if ($sendResetLink) { try { $hash = $this->getCrypto()->sault(10); $user->setSetting(Scalr_Account::SETTING_OWNER_PWD_RESET_HASH, $hash); $clientinfo = array('email' => $user->getEmail(), 'fullname' => $user->fullname); // Send reset password E-mail $res = $this->getContainer()->mailer->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/user_account_confirm.eml', array("{{fullname}}" => $clientinfo['fullname'], "{{pwd_link}}" => "https://{$_SERVER['HTTP_HOST']}/#/guest/updatePassword/?hash={$hash}"), $clientinfo['email'], $clientinfo['fullname']); } catch (Exception $e) { } } } $this->response->data(array('user' => array('id' => $user->getId(), 'email' => $user->getEmail(), 'fullname' => $user->fullname))); $this->response->success('User successfully saved'); } else { throw new Scalr_Exception_InsufficientPermissions(); } }
public function xSaveAction() { $this->request->defineParams(array('teams' => array('type' => 'json'), 'action', 'password' => array('type' => 'string', 'rawValue' => true), 'currentPassword' => array('type' => 'string', 'rawValue' => true))); $newUser = $existingPasswordChanged = $sendResetLink = false; $user = Scalr_Account_User::init(); $validator = new Validator(); $id = (int) $this->getParam('id'); if ($id) { $user->loadById($id); } else { if ($this->getContainer()->config->get('scalr.auth_mode') == 'ldap') { throw new Exception("Adding new users is not supported with LDAP user management"); } $newUser = true; } if ($this->getContainer()->config->get('scalr.auth_mode') != 'ldap') { $email = $this->getParam('email'); if (($isEmailValid = $validator->validateEmail($email)) !== true) { throw new Scalr_Exception_Core($isEmailValid); } $password = $this->getParam('password'); if (empty($password) && ($this->request->hasParam('password') || $newUser)) { if ($user->id == $this->user->id) { $this->response->data(['errors' => ['password' => 'You cannot reset password for yourself']]); $this->response->failure(); return; } $password = Scalr::GenerateSecurePassword($user->isAccountAdmin() || $user->isAccountOwner() ? User::PASSWORD_ADMIN_LENGTH : User::PASSWORD_USER_LENGTH); $sendResetLink = true; } else { if ($this->request->hasParam('password') && ($isPasswordValid = $validator->validatePassword($password, $user->isAccountAdmin() || $user->isAccountOwner() ? ['admin'] : [])) !== true) { $this->response->data(['errors' => ['password' => $isPasswordValid]]); $this->response->failure(); return; } if (!empty($password)) { $existingPasswordChanged = true; } } if (!$newUser && ($sendResetLink || $existingPasswordChanged) && $this->request->hasParam('password') && !$this->user->checkPassword($this->getParam('currentPassword'), false)) { $this->response->data(['errors' => ['currentPassword' => 'Invalid password']]); $this->response->failure(); return; } if ($id) { if (!$this->user->canEditUser($user)) { throw new Scalr_Exception_InsufficientPermissions(); } $user->updateEmail($email); } else { $this->user->getAccount()->validateLimit(Scalr_Limits::ACCOUNT_USERS, 1); $user->type = Scalr_Account_User::TYPE_TEAM_USER; $user->create($email, $this->user->getAccountId()); } if (!empty($password)) { $user->updatePassword($password); } } if ($user->getId() != $this->user->getId() && in_array($this->getParam('status'), array(Scalr_Account_User::STATUS_ACTIVE, Scalr_Account_User::STATUS_INACTIVE))) { $user->status = $this->getParam('status'); } if (!$user->isAccountOwner()) { if ($this->getParam('isAccountAdmin')) { if ($this->user->isAccountOwner()) { $user->type = $this->getParam('isAccountSuperAdmin') ? Scalr_Account_User::TYPE_ACCOUNT_SUPER_ADMIN : Scalr_Account_User::TYPE_ACCOUNT_ADMIN; } else { if ($this->user->isAccountAdmin() && $user->type != Scalr_Account_User::TYPE_ACCOUNT_SUPER_ADMIN) { $user->type = Scalr_Account_User::TYPE_ACCOUNT_ADMIN; } } } else { $user->type = Scalr_Account_User::TYPE_TEAM_USER; } } $user->fullname = $this->getParam('fullname'); $user->comments = $this->getParam('comments'); $user->save(); if (!empty($password)) { $user->setSetting(Scalr_Account::SETTING_OWNER_PWD_RESET_HASH, ""); } $user->setAclRoles($this->getParam('teams')); if ($this->getParam('enableApi')) { $keys = Scalr::GenerateAPIKeys(); $user->setSetting(Scalr_Account_User::SETTING_API_ENABLED, true); $user->setSetting(Scalr_Account_User::SETTING_API_ACCESS_KEY, $keys['id']); $user->setSetting(Scalr_Account_User::SETTING_API_SECRET_KEY, $keys['key']); } $creatorName = $this->user->fullname; if (empty($creatorName)) { $creatorName = $this->user->isAccountOwner() ? 'Account owner' : ($this->user->isAccountAdmin() ? 'Account admin' : 'Team user'); } if ($newUser) { try { $clientinfo = array('fullname' => $user->fullname, 'firstname' => $user->fullname, 'email' => $user->getEmail(), 'password' => $password); $url = Scalr::config('scalr.endpoint.scheme') . "://" . Scalr::config('scalr.endpoint.host'); $res = $this->getContainer()->mailer->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/referral.eml.php', array("creatorName" => $creatorName, "clientFirstname" => $clientinfo['firstname'], "email" => $clientinfo['email'], "password" => $clientinfo['password'], "siteUrl" => $url, "wikiUrl" => \Scalr::config('scalr.ui.wiki_url'), "supportUrl" => \Scalr::config('scalr.ui.support_url'), "isUrl" => preg_match('/^http(s?):\\/\\//i', \Scalr::config('scalr.ui.support_url'))), $user->getEmail()); } catch (Exception $e) { } } elseif ($sendResetLink) { try { $hash = $this->getCrypto()->sault(10); $url = Scalr::config('scalr.endpoint.scheme') . "://" . Scalr::config('scalr.endpoint.host'); $user->setSetting(Scalr_Account::SETTING_OWNER_PWD_RESET_HASH, $hash); $clientinfo = array('email' => $user->getEmail(), 'fullname' => $user->fullname); $res = $this->getContainer()->mailer->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/user_account_confirm.eml', array("{{fullname}}" => $clientinfo['fullname'], "{{pwd_link}}" => "{$url}/?resetPasswordHash={$hash}"), $clientinfo['email'], $clientinfo['fullname']); } catch (Exception $e) { } } else { if ($existingPasswordChanged) { // Send notification E-mail $this->getContainer()->mailer->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/password_change_notification.eml', array('{{fullname}}' => $user->fullname ? $user->fullname : $user->getEmail()), $user->getEmail(), $user->fullname); } } $userTeams = array(); $troles = $this->getContainer()->acl->getUserRoleIdsByTeam($user->id, array_map(create_function('$v', 'return $v["id"];'), $user->getTeams()), $user->getAccountId()); foreach ($troles as $teamId => $roles) { $userTeams[$teamId] = array('roles' => $roles); } $data = ['user' => $user->getUserInfo(), 'teams' => $userTeams]; if ($existingPasswordChanged && $user->getId() == $this->user->getId()) { Scalr_Session::create($this->user->getId()); $data['specialToken'] = Scalr_Session::getInstance()->getToken(); } $this->response->data($data); $this->response->success('User successfully saved'); }