示例#1
0
文件: Farms.php 项目: scalr/scalr
 /**
  * @param int $farmId optional
  * @param int $roleId optional
  * @param string $scalrPageHash optional
  * @param string $scalrPageUiHash optional
  * @throws Scalr_Exception_InsufficientPermissions
  */
 public function xGetFarmAction($farmId = null, $roleId = null, $scalrPageHash = null, $scalrPageUiHash = null)
 {
     if ($scalrPageHash && $scalrPageHash != $this->calcFarmDesignerHash()) {
         $this->response->data(['scalrPageHashMismatch' => true]);
         return;
     }
     if ($scalrPageUiHash && $scalrPageUiHash != $this->response->pageUiHash()) {
         $this->response->data(['scalrPageUiHashMismatch' => true]);
         return;
     }
     $moduleParams = array('farmId' => $farmId, 'roleId' => $roleId, 'behaviors' => ROLE_BEHAVIORS::GetName(null, true));
     unset($moduleParams['behaviors'][ROLE_BEHAVIORS::CASSANDRA]);
     unset($moduleParams['behaviors'][ROLE_BEHAVIORS::CUSTOM]);
     unset($moduleParams['behaviors'][ROLE_BEHAVIORS::HAPROXY]);
     //platforms list
     $platforms = self::loadController('Platforms')->getEnabledPlatforms();
     if (empty($platforms)) {
         throw new Exception('Before building new farm you need to configure environment and setup cloud credentials');
     }
     $moduleParams['categories'] = self::loadController('Roles')->listRoleCategories(true, true);
     $moduleParams['farmVpcEc2Enabled'] = $this->getEnvironment()->isPlatformEnabled(SERVER_PLATFORMS::EC2);
     if ($moduleParams['farmVpcEc2Enabled']) {
         $moduleParams['farmVpcEc2Locations'] = self::loadController('Platforms')->getCloudLocations(SERVER_PLATFORMS::EC2, false);
     }
     if ($farmId) {
         $this->request->checkPermissions(DBFarm::LoadByID($farmId)->__getNewFarmObject(), Acl::PERM_FARMS_UPDATE);
         $c = self::loadController('Builder', 'Scalr_UI_Controller_Farms');
         $moduleParams['farm'] = $c->getFarm2($farmId);
     } else {
         $this->request->restrictAccess(Acl::RESOURCE_OWN_FARMS, Acl::PERM_FARMS_CREATE);
         // TODO: remove hack, do better
         $vars = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(), ScopeInterface::SCOPE_FARM);
         $moduleParams['farmVariables'] = $vars->getValues();
     }
     $moduleParams['tabs'] = array('vpcrouter', 'dbmsr', 'mongodb', 'mysql', 'scaling', 'network', 'cloudfoundry', 'rabbitmq', 'haproxy', 'proxy', 'scripting', 'ec2', 'openstack', 'gce', 'azure', 'security', 'devel', 'storage', 'variables', 'advanced', 'chef');
     //deprecated tabs
     if (\Scalr::config('scalr.ui.show_deprecated_features')) {
         $moduleParams['tabs'][] = 'ebs';
     }
     $conf = $this->getContainer()->config->get('scalr.load_statistics.connections.plotter');
     $moduleParams['tabParams'] = array('farmId' => $farmId, 'farmHash' => $moduleParams['farm'] ? $moduleParams['farm']['farm']['hash'] : '', 'accountId' => $this->environment->keychain(SERVER_PLATFORMS::EC2)->properties[Entity\CloudCredentialsProperty::AWS_ACCOUNT_ID], 'remoteAddress' => $this->request->getRemoteAddr(), 'monitoringHostUrl' => "{$conf['scheme']}://{$conf['host']}:{$conf['port']}", 'nginx' => array('server_section' => file_get_contents(APPPATH . '/templates/services/nginx/server_section.tpl'), 'server_section_ssl' => file_get_contents(APPPATH . '/templates/services/nginx/server_section_ssl.tpl')));
     $moduleParams['tabParams']['scalr.instances_connection_policy'] = \Scalr::config('scalr.instances_connection_policy');
     $moduleParams['tabParams']['scalr.scalarizr_update.default_repo'] = \Scalr::config('scalr.scalarizr_update.default_repo');
     if (Scalr::isHostedScalr()) {
         $moduleParams['tabParams']['scalr.scalarizr_update.repos'] = ['latest' => Utils::getScalarizrUpdateRepoTitle('latest')];
         if ($this->user->getAccount()->priority == 100) {
             $moduleParams['tabParams']['scalr.scalarizr_update.repos']['stable'] = Utils::getScalarizrUpdateRepoTitle('stable');
         }
     } else {
         $repos = array_keys(\Scalr::config('scalr.scalarizr_update.repos'));
         $moduleParams['tabParams']['scalr.scalarizr_update.repos'] = array_combine($repos, $repos);
     }
     $moduleParams['tabParams']['scalr.scalarizr_update.devel_repos'] = is_array(\Scalr::config('scalr.scalarizr_update.devel_repos')) ? array_keys(\Scalr::config('scalr.scalarizr_update.devel_repos')) : [];
     $moduleParams['tabParams']['scalr.aws.ec2.limits.security_groups_per_instance'] = \Scalr::config('scalr.aws.ec2.limits.security_groups_per_instance');
     $moduleParams['metrics'] = Entity\ScalingMetric::getList($this->getEnvironmentId());
     $moduleParams['timezones_list'] = Scalr_Util_DateTime::getTimezones();
     $moduleParams['timezone_default'] = $this->user->getSetting(Scalr_Account_User::SETTING_UI_TIMEZONE);
     if ($moduleParams['farm']['farm']['ownerEditable']) {
         $moduleParams['usersList'] = [];
         foreach (Entity\Account\User::findByAccountId($this->user->getAccountId()) as $user) {
             $moduleParams['usersList'][] = ['id' => $user->id, 'email' => $user->email];
         }
     }
     $defaultFarmRoleSecurityGroups = array('default');
     if (\Scalr::config('scalr.aws.security_group_name')) {
         $defaultFarmRoleSecurityGroups[] = \Scalr::config('scalr.aws.security_group_name');
     }
     $moduleParams['roleDefaultSettings'] = array('base.keep_scripting_logs_time' => \Scalr::config('scalr.system.scripting.default_instance_log_rotation_period'), 'security_groups.list' => json_encode($defaultFarmRoleSecurityGroups), 'base.abort_init_on_script_fail' => \Scalr::config('scalr.system.scripting.default_abort_init_on_script_fail') ? 1 : 0, 'base.disable_firewall_management' => \Scalr::config('scalr.system.default_disable_firewall_management') ? 1 : 0);
     //cost analytics
     if ($this->getContainer()->analytics->enabled && $this->getEnvironment()->getPlatformConfigValue(Scalr_Environment::SETTING_CC_ID)) {
         $farmCostData = $this->getFarmCostData($farmId);
         $moduleParams['analytics'] = $farmCostData['analytics'];
         if ($farmId) {
             $moduleParams['farm']['farm']['projectId'] = $farmCostData['projectId'];
             $moduleParams['analytics']['farmCostMetering'] = $farmCostData['farmCostMetering'];
         }
     }
     $moduleParams['farmLaunchPermission'] = $farmId ? $moduleParams['farm']['farm']['launchPermission'] : $this->request->isAllowed([Acl::RESOURCE_FARMS, Acl::RESOURCE_TEAM_FARMS, Acl::RESOURCE_OWN_FARMS], Acl::PERM_FARMS_LAUNCH_TERMINATE);
     $this->response->data($moduleParams);
 }
示例#2
0
文件: Users.php 项目: scalr/scalr
 /**
  * @param   JsonData    $ids
  * @param   string      $action
  * @param   int         $ownerId
  */
 public function xGroupActionHandlerAction(JsonData $ids, $action, $ownerId = null)
 {
     $processed = array();
     $errors = array();
     $needUpdateFarmOwner = false;
     $actionMsg = '';
     $ids = (array) $ids;
     if ($ownerId && !User::findOne([['id' => $ownerId], ['accountId' => $this->user->getAccountId()]])) {
         $ownerId = null;
     }
     foreach ($ids as $userId) {
         try {
             $user = Scalr_Account_User::init();
             $user->loadById($userId);
             switch ($action) {
                 case 'delete':
                     $actionMsg = 'removed';
                     if ($this->user->canRemoveUser($user)) {
                         $ownedFarms = Farm::findByOwnerId($user->id);
                         if ($ownedFarms->count() > 0) {
                             if ($ownerId) {
                                 /* @var $newOwner User */
                                 $newOwner = User::findPk($ownerId);
                                 /* @var $u User */
                                 $u = User::findPk($this->user->getId());
                                 foreach ($ownedFarms as $farm) {
                                     /* @var $farm Farm */
                                     FarmSetting::addOwnerHistory($farm, $newOwner, $u);
                                     $farm->ownerId = $ownerId;
                                     $farm->save();
                                 }
                             } else {
                                 $needUpdateFarmOwner = true;
                                 throw new Exception("You can't delete owner of the Farm");
                             }
                         }
                         $user->delete();
                         $processed[] = $user->getId();
                     } else {
                         throw new Exception('Insufficient permissions to remove user');
                     }
                     break;
                 case 'activate':
                     $actionMsg = 'activated';
                     if ($this->user->getId() !== $user->getId() && $this->user->canEditUser($user)) {
                         if ($user->status == Scalr_Account_User::STATUS_ACTIVE) {
                             throw new Scalr_Exception_Core('User(s) has already been activated');
                         }
                         $user->status = Scalr_Account_User::STATUS_ACTIVE;
                         $user->save();
                         $processed[] = $user->getId();
                     } else {
                         throw new Scalr_Exception_Core('Insufficient permissions to activate user');
                     }
                     break;
                 case 'deactivate':
                     $actionMsg = 'deactivated';
                     if ($this->user->getId() !== $user->getId() && $this->user->canEditUser($user)) {
                         if ($user->status == Scalr_Account_User::STATUS_INACTIVE) {
                             throw new Scalr_Exception_Core('User(s) has already been suspended');
                         }
                         $user->status = Scalr_Account_User::STATUS_INACTIVE;
                         $user->save();
                         $processed[] = $user->getId();
                     } else {
                         throw new Scalr_Exception_Core('Insufficient permissions to deactivate user');
                     }
                     break;
             }
         } catch (Exception $e) {
             $errors[] = $e->getMessage();
         }
     }
     $num = count($ids);
     if (count($processed) == $num) {
         $this->response->success("Selected user(s) successfully {$actionMsg}");
     } else {
         array_walk($errors, function (&$item) {
             $item = '- ' . $item;
         });
         $this->response->warning(sprintf("Successfully {$actionMsg} only %d from %d users. \nFollowing errors occurred:\n%s", count($processed), $num, join(array_unique($errors), "\n")));
     }
     $this->response->data(['processed' => $processed]);
     if ($needUpdateFarmOwner) {
         $users = [];
         foreach (User::findByAccountId($this->user->getAccountId()) as $user) {
             /* @var $user User */
             if (in_array($user->id, $ids)) {
                 continue;
             }
             $users[] = ['id' => $user->id, 'email' => $user->email];
         }
         $this->response->data(['needUpdateFarmOwner' => $needUpdateFarmOwner, 'usersList' => $users]);
     }
 }