Scalr_Account_User::getDefaultEnvironment PHP Method

getDefaultEnvironment() public method

In case the identifier is not specified it takes default Environment from the list of the available for current User.
public getDefaultEnvironment ( integer $envId ) : Scalr_Environment
$envId integer optional The identifier of the Environment
return Scalr_Environment Returns Environment object
    public function getDefaultEnvironment($envId = 0)
    {
        if ($envId || ($envId = (int) $this->getSetting(Scalr_Account_User::SETTING_UI_ENVIRONMENT))) {
            try {
                $environment = Scalr_Environment::init()->loadById($envId);
                $this->getPermissions()->validate($environment);
            } catch (Exception $e) {
                $environment = null;
            }
        }
        if (empty($environment)) {
            $envs = $this->getEnvironments(null, true);
            if (count($envs)) {
                $envId = $envs[0]['id'];
                $environment = Scalr_Environment::init()->loadById($envId);
            } else {
                throw new Scalr_Exception_Core('You don\'t have access to any environment.');
            }
        }
        $this->getPermissions()->validate($environment);
        return $environment;
    }

Usage Example

Esempio n. 1
0
 public function xLoginAsAction()
 {
     if ($this->getParam('accountId')) {
         $account = new Scalr_Account();
         $account->loadById($this->getParam('accountId'));
         $user = $account->getOwner();
     } else {
         $user = new Scalr_Account_User();
         $user->loadById($this->getParam('userId'));
     }
     if ($user->status != User::STATUS_ACTIVE) {
         throw new Exception('User account has been deactivated. You cannot login into it.');
     }
     Scalr_Session::create($user->getId(), $this->user->getId());
     try {
         $envId = $this->getEnvironmentId(true) ?: $user->getDefaultEnvironment()->id;
     } catch (Exception $e) {
         $envId = null;
     }
     $this->auditLog("user.auth.login", $user, $envId, $this->request->getRemoteAddr(), $this->user->getId());
     $this->response->success();
 }