Neos\Neos\Domain\Service\UserService::currentUserCanPublishToWorkspace PHP Method

currentUserCanPublishToWorkspace() public method

In future versions, this logic may be implemented in Neos in a more generic way (for example, by means of an ACL object), but for now, this method exists in order to at least centralize and encapsulate the required logic.
public currentUserCanPublishToWorkspace ( Workspace $workspace ) : boolean
$workspace Neos\ContentRepository\Domain\Model\Workspace The workspace
return boolean
    public function currentUserCanPublishToWorkspace(Workspace $workspace)
    {
        if ($workspace->getName() === 'live') {
            return $this->securityContext->hasRole('Neos.Neos:LivePublisher');
        }
        if ($workspace->getOwner() === $this->getCurrentUser() || $workspace->getOwner() === null) {
            return true;
        }
        return false;
    }

Usage Example

 /**
  * Shows a list of existing workspaces
  *
  * @return string
  */
 public function indexAction()
 {
     $user = $this->userService->getCurrentUser();
     $workspacesArray = [];
     /** @var Workspace $workspace */
     foreach ($this->workspaceRepository->findAll() as $workspace) {
         // FIXME: This check should be implemented through a specialized Workspace Privilege or something similar
         if ($workspace->getOwner() !== null && $workspace->getOwner() !== $user) {
             continue;
         }
         $workspaceArray = ['name' => $workspace->getName(), 'title' => $workspace->getTitle(), 'description' => $workspace->getDescription(), 'baseWorkspace' => $workspace->getBaseWorkspace()];
         if ($user !== null) {
             $workspaceArray['readonly'] = !$this->userService->currentUserCanPublishToWorkspace($workspace);
         }
         $workspacesArray[] = $workspaceArray;
     }
     $this->view->assign('workspaces', $workspacesArray);
 }
All Usage Examples Of Neos\Neos\Domain\Service\UserService::currentUserCanPublishToWorkspace