Xpressengine\Permission\PermissionHandler::register PHP Method

register() public method

Register permission information
public register ( string $name, Grant $grant, string $siteKey = 'default' ) : Permission
$name string permission name
$grant Grant grant instance
$siteKey string site key name
return Permission
    public function register($name, Grant $grant, $siteKey = 'default')
    {
        $permission = $this->getOrNew($name, $siteKey);
        if (strrpos($name, '.') !== false) {
            $pname = substr($name, 0, strrpos($name, '.'));
            if (!$this->repo->findByName($siteKey, $pname)) {
                throw new NoParentException(['name' => $name]);
            }
        }
        $permission->setGrant($grant);
        if ($permission->exists !== true) {
            return $this->repo->insert($permission);
        }
        return $this->repo->update($permission);
    }

Usage Example

 /**
  * 위젯박스를 생성한다.
  *
  * @param array $data 생성할 위젯박스의 데이터
  *
  * @return void
  */
 public function create($data)
 {
     $id = array_get($data, 'id');
     if ($id === null) {
         throw new IDNotFoundException();
     }
     if (str_contains($id, '.')) {
         throw new InvalidIDException();
     }
     if ($this->repository->find($id) !== null) {
         throw new IDAlreadyExistsException();
     }
     $options = array_get($data, 'options', []);
     if (is_array($options)) {
         $options = json_encode($options);
     }
     $title = array_get($data, 'title', $id);
     $content = array_get($data, 'content', '');
     $widgetbox = $this->repository->create(compact('id', 'title', 'content', 'options'));
     $grant = new Grant();
     $grant->set('edit', [Grant::RATING_TYPE => Rating::SUPER, Grant::GROUP_TYPE => [], Grant::USER_TYPE => [], Grant::EXCEPT_TYPE => []]);
     $this->permissionHandler->register('widgetbox.' . $id, $grant);
     return $widgetbox;
 }
All Usage Examples Of Xpressengine\Permission\PermissionHandler::register