Example #1
0
 public function viewAction()
 {
     $ccs = false;
     $unassignedCcs = false;
     if ($this->getContainer()->analytics->enabled) {
         $ccs = [];
         foreach (AccountCostCenterEntity::findByAccountId($this->user->getAccountId()) as $accountCc) {
             /* @var $accountCs \Scalr\Stats\CostAnalytics\Entity\AccountCostCenterEntity */
             $ccEntity = CostCentreEntity::findPk($accountCc->ccId);
             /* @var $ccEntity \Scalr\Stats\CostAnalytics\Entity\CostCentreEntity */
             if ($ccEntity->archived) {
                 continue;
             }
             $ccs[$accountCc->ccId] = get_object_vars($ccEntity);
         }
         $unassignedCcs = [];
         foreach ($this->user->getEnvironments() as $row) {
             $env = Scalr_Environment::init()->loadById($row['id']);
             $ccEntity = CostCentreEntity::findPk($env->getPlatformConfigValue(Scalr_Environment::SETTING_CC_ID));
             /* @var $ccEntity \Scalr\Stats\CostAnalytics\Entity\CostCentreEntity */
             if ($ccEntity && !isset($ccs[$ccEntity->ccId])) {
                 $unassignedCcs[$row['id']] = ['ccId' => $ccEntity->ccId, 'name' => $ccEntity->name];
             }
         }
     }
     $this->response->page('ui/account2/environments/view.js', array('ccs' => array_values($ccs), 'unassignedCcs' => $unassignedCcs), array('ui/account2/dataconfig.js'), array('ui/account2/environments/view.css'), array('account.environments', 'account.teams'));
 }
Example #2
0
 public function xSaveAction()
 {
     $this->request->defineParams(array('id' => array('type' => 'int'), 'name' => array('type' => 'string'), 'ownerEmail' => array('type' => 'string'), 'ownerPassword' => array('type' => 'string', 'rawValue' => true), 'comments' => array('type' => 'string'), 'ccs' => array('type' => 'json')));
     $account = Scalr_Account::init();
     $validator = new Validator();
     $id = (int) $this->getParam('id');
     $name = $this->getParam('name');
     $ownerEmail = $this->getParam('ownerEmail');
     $ownerPassword = $this->getParam('ownerPassword');
     $validator->validate($name, "name", Validator::NOEMPTY, [], "Name is required");
     $validator->validate($id, "id", Validator::INTEGERNUM);
     if ($id) {
         $account->loadById($id);
     } else {
         $account->status = Scalr_Account::STATUS_ACTIVE;
         if ($this->getContainer()->config->get('scalr.auth_mode') == 'scalr') {
             $validator->validate($ownerEmail, "ownerEmail", Validator::EMAIL);
             $validator->validate($ownerPassword, "ownerPassword", Validator::PASSWORD, ["admin"]);
         } elseif ($this->getContainer()->config->get('scalr.auth_mode') == 'ldap') {
             $validator->validate($ownerEmail, "ownerEmail", Validator::NOEMPTY, [], "Email is required");
         }
     }
     if (!$validator->isValid($this->response)) {
         return;
     }
     $this->db->BeginTrans();
     try {
         $account->name = $name;
         $account->comments = $this->getParam('comments');
         $account->save();
         $account->initializeAcl();
         $account->setLimits(array(Scalr_Limits::ACCOUNT_ENVIRONMENTS => $this->getParam('limitEnv'), Scalr_Limits::ACCOUNT_FARMS => $this->getParam('limitFarms'), Scalr_Limits::ACCOUNT_SERVERS => $this->getParam('limitServers'), Scalr_Limits::ACCOUNT_USERS => $this->getParam('limitUsers')));
         if (!$id) {
             $user = $account->createUser($ownerEmail, $ownerPassword, Scalr_Account_User::TYPE_ACCOUNT_OWNER);
             if ($this->getContainer()->analytics->enabled) {
                 //Default Cost Center should be assigned
                 $cc = $this->getContainer()->analytics->ccs->get($this->getContainer()->analytics->usage->autoCostCentre());
                 //Assigns account with Cost Center
                 $accountCcEntity = new AccountCostCenterEntity($account->id, $cc->ccId);
                 $accountCcEntity->save();
             }
             $account->createEnvironment("default");
         }
         if ($this->getContainer()->config->get('scalr.auth_mode') == 'ldap' && $id) {
             if ($ownerEmail != $account->getOwner()->getEmail()) {
                 $prev = $account->getOwner();
                 $prev->type = Scalr_Account_User::TYPE_TEAM_USER;
                 $prev->save();
                 $user = new Scalr_Account_User();
                 if ($user->loadByEmail($ownerEmail, $account->id)) {
                     $user->type = Scalr_Account_User::TYPE_ACCOUNT_OWNER;
                     $user->save();
                 } else {
                     $account->createUser($ownerEmail, $ownerPassword, Scalr_Account_User::TYPE_ACCOUNT_OWNER);
                 }
             }
         }
         if ($this->getContainer()->analytics->enabled) {
             if (!Scalr::isHostedScalr()) {
                 //save ccs
                 $ccs = (array) $this->getParam('ccs');
                 foreach (AccountCostCenterEntity::findByAccountId($account->id) as $accountCcsEntity) {
                     $index = array_search($accountCcsEntity->ccId, $ccs);
                     if ($index === false) {
                         $accountCcsEntity->delete();
                     } else {
                         unset($ccs[$index]);
                     }
                 }
                 foreach ($ccs as $ccId) {
                     $accountCcsEntity = new AccountCostCenterEntity($account->id, $ccId);
                     $accountCcsEntity->save();
                 }
             }
         }
     } catch (Exception $e) {
         $this->db->RollbackTrans();
         throw $e;
     }
     $this->db->CommitTrans();
     $this->response->data(array('accountId' => $account->id));
 }
Example #3
0
 /**
  * Gets the list of the Cost Centers which correspond to Account
  *
  * @return  \Scalr\Model\Collections\ArrayCollection  Returns collection of the entities
  */
 public function getCostCenters()
 {
     $ccs = new ArrayCollection();
     foreach (AccountCostCenterEntity::findByAccountId($this->id) as $accountCc) {
         $cc = CostCentreEntity::findPk($accountCc->ccId);
         if (!$cc instanceof CostCentreEntity) {
             continue;
         }
         $ccs->append($cc);
     }
     return $ccs;
 }