コード例 #1
0
ファイル: RoleCategoryAdapter.php プロジェクト: scalr/scalr
 /**
  * {@inheritdoc}
  * @see \Scalr\Api\DataType\ApiEntityAdapter::validateEntity()
  */
 public function validateEntity($entity)
 {
     if (!$entity instanceof RoleCategory) {
         throw new \InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\RoleCategory class"));
     }
     if (!preg_match('/^' . RoleCategory::NAME_REGEXP . '$/', $entity->name)) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Invalid name of the Role Category. Name should start and end with letter or number and contain only letters, numbers, spaces and dashes.');
     }
     if (strlen($entity->name) > RoleCategory::NAME_LENGTH) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Name should be less than 18 characters');
     }
     if (is_null($entity->id)) {
         $criteria = $this->controller->getScopeCriteria($entity->getScope());
         $criteria[] = ['name' => $entity->name];
         if (!empty(RoleCategory::findOne($criteria))) {
             throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, sprintf('Role Category with name %s already exists', $entity->name));
         }
     } else {
         if (empty(RoleCategory::findPk($entity->id))) {
             throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Role Category with id: %d", $entity->id));
         }
     }
 }
コード例 #2
0
ファイル: RoleAdapter.php プロジェクト: mheydt/scalr
 /**
  * {@inheritdoc}
  * @see \Scalr\Api\DataType\ApiEntityAdapter::validateEntity()
  */
 public function validateEntity($entity)
 {
     if (!$entity instanceof Entity\Role) {
         throw new \InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\Role class"));
     }
     if ($entity->id !== null) {
         if (!is_integer($entity->id)) {
             throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid value of the identifier");
         }
         //Checks if the role does exist
         if (!Entity\Role::findPk($entity->id)) {
             throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Role with ID: %d", $entity->id));
         }
     }
     //Is this a new Role
     if (!$entity->id) {
         $entity->addedByEmail = $this->controller->getUser()->email;
         $entity->addedByUserId = $this->controller->getUser()->id;
     }
     if (!$entity::isValidName($entity->name)) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid name of the Role");
     }
     $entity->description = $entity->description ?: '';
     $this->validateString($entity->description, 'Invalid description');
     if (!$this->controller->hasPermissions($entity, true)) {
         //Checks entity level write access permissions
         throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions");
     }
     //We only allow to either create or modify Environment Scope Roles
     if ($entity->getScope() !== $this->controller->getScope()) {
         throw new ApiErrorException(403, ErrorMessage::ERR_SCOPE_VIOLATION, sprintf("Invalid scope"));
     }
     //Checks the Role Category
     if (!empty($entity->catId)) {
         //Tries to find out the specified Role category
         $category = Entity\RoleCategory::findPk($entity->catId);
         if ($category instanceof Entity\RoleCategory) {
             //Checks if the specified RoleCategory either shared or belongs to User's scope.
             if ($category->getScope() !== ScopeInterface::SCOPE_SCALR && $category->envId !== $this->controller->getEnvironment()->id) {
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "The specified category isn't owned by your environment.");
             }
         } else {
             throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "The Role category does not exist");
         }
     } else {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Role category should be provided with the request.");
     }
     if (empty($entity->osId)) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property 'os.id'");
     }
     //Tries to find out the specified OS
     if (empty(Entity\Os::findPk($entity->osId))) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "OS with id '{$entity->osId}' not found.");
     }
 }
コード例 #3
0
ファイル: Categories.php プロジェクト: scalr/scalr
 /**
  * @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');
 }