/** * @param integer $id * @param string $name * @throws Exception * @throws Scalr_Exception_Core */ public function xSaveAction($id = 0, $name) { $this->request->restrictAccess('ROLES', 'MANAGE'); $validator = new \Scalr\UI\Request\Validator(); $validator->addErrorIf(!preg_match('/^' . RoleCategory::NAME_REGEXP . '$/', $name), 'name', "Name should start and end with letter or number and contain only letters, numbers, spaces and dashes."); $validator->addErrorIf(strlen($name) > RoleCategory::NAME_LENGTH, 'name', "Name should be less than 18 characters"); $scope = $this->request->getScope(); $criteria = [['name' => $name]]; if ($id) { $criteria[] = ['id' => ['$ne' => $id]]; } if ($this->user->isScalrAdmin()) { $criteria[] = ['accountId' => NULL]; } else { $criteria[] = ['$or' => [['accountId' => $this->user->getAccountId()], ['accountId' => NULL]]]; if ($scope == 'account') { $criteria[] = ['envId' => NULL]; } else { $criteria[] = ['$or' => [['envId' => NULL], ['envId' => $this->getEnvironmentId(true)]]]; } } $validator->addErrorIf(RoleCategory::find($criteria)->count(), 'name', 'This name is already in use. Note that Role Categories names are case-insensitive.'); if (!$validator->isValid($this->response)) { return; } if ($id) { $category = RoleCategory::findPk($id); /* @var $category RoleCategory */ if (!$category) { throw new Exception('Role Category not found'); } $this->request->checkPermissions($category, true); $category->name = $name; $category->save(); } else { $category = new RoleCategory(); if ($this->user->isScalrAdmin()) { $category->accountId = NULL; $category->envId = NULL; } else { $category->accountId = $this->user->getAccountId(); $category->envId = $scope == 'account' ? NULL : $this->getEnvironmentId(); } $category->name = $name; $category->save(); } $used = $category->getUsed(); $this->response->data(['category' => ['id' => $category->id, 'name' => $category->name, 'used' => $used, 'scope' => $scope, 'status' => $used ? 'In use' : 'Not used']]); $this->response->success('Role Category successfully saved'); }