save() public method

public save ( )
示例#1
0
文件: Roles.php 项目: recipe/scalr
 public function xSave2Action()
 {
     $this->request->restrictAccess(Acl::RESOURCE_FARMS_ROLES, Acl::PERM_FARMS_ROLES_MANAGE);
     $this->request->defineParams(array('roleId' => array('type' => 'int'), 'behaviors' => array('type' => 'array'), 'tags' => array('type' => 'array'), 'description', 'name', 'os', 'parameters' => array('type' => 'json'), 'removedImages' => array('type' => 'json'), 'images' => array('type' => 'json'), 'properties' => array('type' => 'json'), 'scripts' => array('type' => 'json'), 'chef' => array('type' => 'json')));
     $id = $this->getParam('roleId');
     if ($id == 0) {
         if ($this->user->isScalrAdmin()) {
             $origin = ROLE_TYPE::SHARED;
             $envId = 0;
             $clientId = 0;
         } else {
             $origin = ROLE_TYPE::CUSTOM;
             $envId = $this->environment->id;
             $clientId = $this->user->getAccountId();
         }
         $dbRole = new DBRole(0);
         $dbRole->generation = 2;
         $dbRole->origin = $origin;
         $dbRole->envId = $envId;
         $dbRole->clientId = $clientId;
         $dbRole->catId = $this->getParam('catId');
         $dbRole->name = $this->getParam('name');
         $dbRole->os = $this->getParam('os');
         $dbRole->osGeneration = $this->getParam('osGeneration');
         $dbRole->osFamily = $this->getParam('osFamily');
         $dbRole->osVersion = $this->getParam('osVersion');
         $dbRole->addedByEmail = $this->user->getEmail();
         $dbRole->addedByUserId = $this->user->getId();
         $dbRole->save();
         $dbRole->setBehaviors(array_values($this->getParam('behaviors')));
     } else {
         $dbRole = DBRole::loadById($id);
         if (!$this->user->isScalrAdmin()) {
             $this->user->getPermissions()->validate($dbRole);
         }
     }
     if ($dbRole->origin == ROLE_TYPE::CUSTOM) {
         $variables = new Scalr_Scripting_GlobalVariables($this->getEnvironmentId(), Scalr_Scripting_GlobalVariables::SCOPE_ROLE);
         $variables->setValues(json_decode($this->getParam('variables'), true), $dbRole->id);
     }
     $dbRole->description = $this->getParam('description');
     foreach ($this->getParam('removedImages') as $imageId) {
         $dbRole->removeImage($imageId);
     }
     foreach ($this->getParam('images') as $image) {
         $image = (array) $image;
         $dbRole->setImage($image['image_id'], $image['platform'], $image['location'], $image['szr_version'], $image['architecture']);
     }
     $dbRole->setScripts($this->getParam('scripts'));
     //todo
     //$dbRole->setChefSettings($this->getParam('chef'));
     if ($this->user->isScalrAdmin()) {
         $dbRole->setTags($this->getParam('tags'));
     }
     $dbRole->save();
     $this->response->success('Role saved');
 }
示例#2
0
 public function xSaveAction()
 {
     $this->request->restrictAccess(Acl::RESOURCE_FARMS_ROLES, Acl::PERM_FARMS_ROLES_MANAGE);
     $this->request->defineParams(array('roleId' => array('type' => 'int'), 'behaviors' => array('type' => 'json'), 'tags' => array('type' => 'json'), 'description', 'name', 'parameters' => array('type' => 'json'), 'removedImages' => array('type' => 'json'), 'images' => array('type' => 'json'), 'properties' => array('type' => 'json'), 'scripts' => array('type' => 'json'), 'variables' => array('type' => 'json'), 'chef' => array('type' => 'json')));
     $id = $this->getParam('roleId');
     if ($id == 0) {
         if ($this->user->isScalrAdmin()) {
             $origin = ROLE_TYPE::SHARED;
             $envId = null;
             $clientId = null;
         } else {
             $origin = ROLE_TYPE::CUSTOM;
             $envId = $this->environment->id;
             $clientId = $this->user->getAccountId();
         }
         // TODO: validate role name via Scalr\Model\Entity\Role::validateName(), validate other fields
         $dbRole = new DBRole(0);
         $dbRole->generation = 2;
         $dbRole->origin = $origin;
         $dbRole->envId = $envId;
         $dbRole->clientId = $clientId;
         $dbRole->catId = $this->getParam('catId');
         $dbRole->name = $this->getParam('name');
         //TODO: VALIDATE osId
         $dbRole->osId = $this->getParam('osId');
         $dbRole->addedByEmail = $this->user->getEmail();
         $dbRole->addedByUserId = $this->user->getId();
         $dbRole->save();
         $dbRole->setBehaviors(array_values($this->getParam('behaviors')));
     } else {
         $dbRole = DBRole::loadById($id);
         if (!$this->user->isScalrAdmin()) {
             $this->user->getPermissions()->validate($dbRole);
         }
     }
     if ($dbRole->origin == ROLE_TYPE::CUSTOM && $this->user->getAccountId()) {
         $variables = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(), Scalr_Scripting_GlobalVariables::SCOPE_ROLE);
         $variables->setValues(is_array($this->getParam('variables')) ? $this->getParam('variables') : [], $dbRole->id);
     } else {
         if ($this->user->isScalrAdmin()) {
             $variables = new Scalr_Scripting_GlobalVariables(0, 0, Scalr_Scripting_GlobalVariables::SCOPE_ROLE);
             $variables->setValues(is_array($this->getParam('variables')) ? $this->getParam('variables') : [], $dbRole->id);
         }
     }
     $dbRole->clearProperties('chef.');
     if (!is_null($this->getParam('chef'))) {
         $dbRole->setProperties($this->getParam('chef'));
     }
     $dbRole->description = $this->getParam('description');
     $images = $this->getParam('images');
     if (!empty($images)) {
         foreach ($images as $i) {
             $dbRole->__getNewRoleObject()->setImage($i['platform'], $i['cloudLocation'], $i['imageId'], $this->user->getId(), $this->user->getEmail());
         }
     }
     $scripts = $this->getParam('scripts');
     if (is_null($scripts)) {
         $scripts = [];
     }
     $dbRole->setScripts($scripts);
     $dbRole->save();
     $this->response->data(['role' => $this->getInfo($dbRole->id, true)]);
     $this->response->success('Role saved');
 }