Scalr\Api\Service\User\V1beta0\Controller\Roles::getRole PHP Method

getRole() public method

Gets role from database using User's Environment
public getRole ( integer $roleId, boolean $restrictToCurrentScope = false, boolean $modify = null ) : Role | null
$roleId integer The identifier of the Role
$restrictToCurrentScope boolean optional Whether it should additionally check that role corresponds to current scope
$modify boolean optional Whether it should check access permission with modify flag
return Scalr\Model\Entity\Role | null Returns Role entity on success or NULL otherwise
    public function getRole($roleId, $restrictToCurrentScope = false, $modify = null)
    {
        $criteria = $this->getDefaultCriteria();
        $criteria[] = ['id' => $roleId];
        $role = Entity\Role::findOne($criteria);
        /* @var $role Entity\Role */
        if (!$role) {
            throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "The Role either does not exist or isn't in scope for the current Environment.");
        }
        //To be over-suspicious check READ access to Role object
        $this->checkPermissions($role, $modify);
        if ($restrictToCurrentScope && $role->getScope() !== $this->getScope()) {
            throw new ApiErrorException(403, ErrorMessage::ERR_SCOPE_VIOLATION, "The Role is not either from the {$this->getScope()} scope or owned by your {$this->getScope()}.");
        }
        return $role;
    }

Usage Example

Example #1
0
 /**
  * Gets role from database using User's Environment
  *
  * @param   int     $roleId          The identifier of the Role
  * @param   bool    $modify optional Flag checking write permissions
  *
  * @return  Role|null
  *
  * @throws  ApiErrorException
  */
 public function getRole($roleId, $modify = false)
 {
     if (empty($this->roleController)) {
         $this->roleController = $this->getContainer()->api->controller(static::$roleControllerClass);
     }
     return $this->roleController->getRole($roleId, $modify);
 }