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');
}