예제 #1
0
 /**
  * Gets a Capsule by the specified ID.  Will determine if the Capsule is owned by the User.  If it is not
  * owned by the User, it will determine if the User has discovered it.
  *
  * @param mixed $id The ID of the Capsule
  */
 public function getCapsule($id)
 {
     // Make sure an ID was specified
     if (!$id) {
         throw new BadRequestException();
     }
     // Get the Capsule
     $capsule = $this->Capsule->getById($id);
     // If the Capsule could not be found, indicate resource not found
     if (!$capsule) {
         throw new NotFoundException();
     }
     // See if the User owns the Capsule
     $isOwned = $this->Auth->user('id') == $capsule['Capsule']['user_id'];
     // If the Capsule is not owned, see if it is discovered by the User
     $discovery = null;
     if (!$isOwned) {
         $discovery = $this->Capsule->Discovery->getByCapsuleIdForUser($id, $this->Auth->user('id'));
         if (!isset($discovery['Discovery']['opened']) || $discovery['Discovery']['opened'] == false) {
             $this->Capsule->Discovery->setAsOpened($discovery['Discovery']['id']);
         }
         // If the User does not own the Capsule or has not discovered it, indicate resource not found
         if (!$discovery) {
             throw new NotFoundException();
         }
     }
     $this->controller->set('capsule', $capsule);
     $this->controller->set('discovery', $discovery);
     $this->controller->set('isOwned', $isOwned);
 }