Jackalope\Transport\Jackrabbit\Client::login PHP Method

login() public method

{@inheritDoc}
public login ( PHPCR\CredentialsInterface $credentials = null, $workspaceName = null )
$credentials PHPCR\CredentialsInterface
    public function login(CredentialsInterface $credentials = null, $workspaceName = null)
    {
        if ($this->credentials) {
            throw new RepositoryException('Do not call login twice. Rather instantiate a new Transport object ' . 'to log in as different user or for a different workspace.');
        }
        if (!$credentials instanceof SimpleCredentials) {
            $hint = is_null($credentials) ? 'jackalope-jackrabbit does not support "null" credentials' : 'Only SimpleCredentials are supported. Unkown credentials type: ' . get_class($credentials);
            throw new LoginException($hint);
        }
        $this->credentials = $credentials;
        if (!$workspaceName) {
            $request = $this->getRequest(Request::PROPFIND, $this->server);
            $request->setBody($this->buildPropfindRequest(array('dcr:workspaceName')));
            $dom = $request->executeDom();
            $answer = $dom->getElementsByTagNameNS(self::NS_DCR, 'workspaceName');
            $workspaceName = $answer->item(0)->textContent;
        }
        $this->workspace = $workspaceName;
        $this->workspaceUri = $this->server . $workspaceName;
        $this->workspaceUriRoot = $this->workspaceUri . "/jcr:root";
        if (!$this->checkLoginOnServer) {
            return $workspaceName;
        }
        $request = $this->getRequest(Request::PROPFIND, $this->workspaceUri);
        $request->setBody($this->buildPropfindRequest(array('D:workspace', 'dcr:workspaceName')));
        $dom = $request->executeDom();
        $set = $dom->getElementsByTagNameNS(self::NS_DCR, 'workspaceName');
        if ($set->length != 1) {
            throw new RepositoryException('Unexpected answer from server: ' . $dom->saveXML());
        }
        if ($set->item(0)->textContent != $this->workspace) {
            throw new RepositoryException('Wrong workspace in answer from server: ' . $dom->saveXML());
        }
        return $workspaceName;
    }

Usage Example

 /**
  * {@inheritDoc}
  */
 public function getNodePathForIdentifier($uuid, $workspace = null)
 {
     if (null !== $workspace && $workspace != $this->workspace) {
         $client = new Client($this->factory, $this->server);
         $client->login($this->credentials, $workspace);
         return $client->getNodePathForIdentifier($uuid);
     }
     $request = $this->getRequest(Request::REPORT, $this->workspaceUri);
     $request->setBody($this->buildLocateRequest($uuid));
     $dom = $request->executeDom();
     /* answer looks like
           <D:multistatus xmlns:D="DAV:">
             <D:response>
                 <D:href>http://localhost:8080/server/tests/jcr%3aroot/tests_level1_access_base/idExample/</D:href>
             </D:response>
         </D:multistatus>
        */
     $set = $dom->getElementsByTagNameNS(self::NS_DAV, 'href');
     if ($set->length != 1) {
         throw new RepositoryException('Unexpected answer from server: ' . $dom->saveXML());
     }
     $fullPath = $set->item(0)->textContent;
     if (strncmp($this->workspaceUriRoot, $fullPath, strlen($this->workspaceUri))) {
         throw new RepositoryException("Server answered a path that is not in the current workspace: uuid={$uuid}, path={$fullPath}, workspace=" . $this->workspaceUriRoot);
     }
     return $this->stripServerRootFromUri(substr(urldecode($fullPath), 0, -1));
 }