Exemplo n.º 1
0
 /**
  * Check if a share arrangement is in place.
  *
  * @param array $requestBody
  * @throws Exception
  */
 private function checkForShare(array $requestBody)
 {
     $doSaveResourceLink = true;
     $key = $this->resourceLink->primary_consumer_key;
     $id = $this->resourceLink->primary_resource_link_id;
     $shareRequest = isset($requestBody['custom_share_key']) && !empty($requestBody['custom_share_key']);
     if ($shareRequest) {
         if (!$this->allowSharing) {
             throw new Exception('Your sharing request has been refused because sharing is not being permitted');
         } else {
             // Check if this is a new share key
             $share_key = new ResourceLinkShareKey($this->resourceLink, $requestBody['custom_share_key']);
             if (!is_null($share_key->primary_consumer_key) && !is_null($share_key->primary_resource_link_id)) {
                 // Update resource link with sharing primary resource link details
                 $key = $share_key->primary_consumer_key;
                 $id = $share_key->primary_resource_link_id;
                 if ($key == $this->consumer->getKey() && $id == $this->resourceLink->getId()) {
                     throw new Exception('It is not possible to share your resource link with yourself');
                 }
                 $this->resourceLink->primary_consumer_key = $key;
                 $this->resourceLink->primary_resource_link_id = $id;
                 $this->resourceLink->share_approved = $share_key->auto_approve;
                 if (!$this->resourceLink->save()) {
                     throw new Exception('An error occurred initialising your share arrangement');
                 }
                 $doSaveResourceLink = false;
                 $this->user->getResourceLink()->primary_consumer_key = $key;
                 $this->user->getResourceLink()->primary_resource_link_id = $id;
                 $this->user->getResourceLink()->share_approved = $share_key->auto_approve;
                 $this->user->getResourceLink()->updated = time();
                 // Remove share key
                 $share_key->delete();
             }
             if (!is_null($key)) {
                 throw new Exception('You have requested to share a resource link but none is available');
             }
             if (is_null($this->user->getResourceLink()->share_approved) || !$this->user->getResourceLink()->share_approved) {
                 throw new Exception('Your share request is waiting to be approved');
             }
         }
     } else {
         if (!is_null($key)) {
             // Check no share is in place
             throw new Exception('You have not requested to share a resource link but an arrangement is currently in place');
         }
     }
     // Look up primary resource link
     if (!is_null($key)) {
         $consumer = new ToolConsumer($key, $this->storage);
         // TODO: Move to load function
         if (is_null($consumer->created)) {
             throw new Exception('Unable to load tool consumer');
         }
         $resource_link = new ResourceLink($consumer, $id);
         if (is_null($resource_link->created)) {
             throw new Exception('Unable to load resource link being shared');
         }
         if ($doSaveResourceLink) {
             $this->resourceLink->save();
         }
         $this->resourceLink = $resource_link;
     }
 }