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', 'os', '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 = 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'))); if ($this->user->isScalrAdmin()) { $dbRole->setTags($this->getParam('tags')); } } 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); } } if ($dbRole->origin == ROLE_TYPE::CUSTOM) { //chef $dbRole->clearProperties('chef.'); $dbRole->setProperties($this->getParam('chef')); } $dbRole->description = $this->getParam('description'); $removedImages = $this->getParam('removedImages'); if (!empty($removedImages)) { foreach ($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['location'] : '', $image['szr_version'], $image['architecture']); } $dbRole->setScripts($this->getParam('scripts')); $dbRole->save(); $this->response->data(array('role' => $this->getInfo($dbRole->id, true))); $this->response->success('Role saved'); }