예제 #1
0
파일: Role.php 프로젝트: hosivan90/toxotes
 public function executeAddMember()
 {
     $this->validAjaxRequest();
     $ajax = new \AjaxResponse();
     if (!$this->isAllowed(PERMISSION_ROLE_PERMISSION_MANAGE)) {
         $ajax->type = \AjaxResponse::ERROR;
         $ajax->message = t("You do not have permission fot this action");
         return $this->renderText($ajax->toString());
     }
     $user_id = $this->post('user_id', 'INT', 0);
     if (!$user_id || !($user = \Users::retrieveById($user_id))) {
         $ajax->type = \AjaxResponse::ERROR;
         $ajax->message = t("User not found");
         return $this->renderText($ajax->toString());
     }
     $role_id = $this->post('role_id', 'INT', 0);
     if (!$role_id || !($role = \Roles::retrieveById($role_id))) {
         $ajax->type = \AjaxResponse::ERROR;
         $ajax->message = t("Role not found");
         return $this->renderText($ajax->toString());
     }
     if (\Users::SECTION_STAFF != $user->getSection()) {
         $ajax->type = \AjaxResponse::ERROR;
         $ajax->message = t("User %username% is not staff", array("%username%" => $user->getUsername()));
         return $this->renderText($ajax->toString());
     }
     if (!($userRole = \UserRole::findOneByRoleIdAndUserId($role->getId(), $user->getId()))) {
         $userRole = new \UserRole();
         $userRole->setRoleId($role->getId());
         $userRole->setUserId($user->getId());
         if ($userRole->save()) {
             $role->setMemberNo($role->getMemberNo() + 1);
             $role->save(false);
         }
     }
     $ajax->type = \AjaxResponse::SUCCESS;
     $ajax->message = t("Add member successful!");
     $ajax->user = $user->toArray();
     $ajax->role = $role->toArray();
     return $this->renderText($ajax->toString());
 }