Example #1
0
 /**
  * Bucket collaborators restful api
  * 
  * @return	void
  */
 public function action_collaborators()
 {
     $this->template = '';
     $this->auto_render = FALSE;
     $query = $this->request->query('q') ? $this->request->query('q') : NULL;
     if ($query) {
         echo json_encode(Model_User::get_like($query, array($this->user->id, $this->bucket->account->user->id)));
         return;
     }
     switch ($this->request->method()) {
         case "DELETE":
             // Is the logged in user an owner?
             if (!$this->owner) {
                 throw new HTTP_Exception_403();
             }
             $user_id = intval($this->request->param('id', 0));
             $user_orm = ORM::factory('user', $user_id);
             if (!$user_orm->loaded()) {
                 return;
             }
             $collaborator_orm = $this->bucket->bucket_collaborators->where('user_id', '=', $user_orm->id)->find();
             if ($collaborator_orm->loaded()) {
                 $collaborator_orm->delete();
                 Model_User_Action::delete_invite($this->user->id, 'bucket', $this->bucket->id, $user_orm->id);
             }
             break;
         case "PUT":
             // Is the logged in user an owner?
             if (!$this->owner) {
                 throw new HTTP_Exception_403();
             }
             $user_id = intval($this->request->param('id', 0));
             $user_orm = ORM::factory('user', $user_id);
             $collaborator_array = json_decode($this->request->body(), TRUE);
             $collaborator_orm = ORM::factory("bucket_collaborator")->where('bucket_id', '=', $this->bucket->id)->where('user_id', '=', $user_orm->id)->find();
             if (!$collaborator_orm->loaded()) {
                 $collaborator_orm->bucket = $this->bucket;
                 $collaborator_orm->user = $user_orm;
                 Model_User_Action::create_action($this->user->id, 'bucket', $this->bucket->id, $user_orm->id);
             }
             if (isset($collaborator_array['read_only'])) {
                 $collaborator_orm->read_only = (bool) $collaborator_array['read_only'];
             }
             $collaborator_orm->save();
             break;
     }
 }