| <?php  | 
|   | 
| /** | 
|  * 角色控制器 | 
|  */ | 
|   | 
| class RoleController extends System_Controller_Admin{ | 
|   | 
|     public function init(){ | 
|         parent::init(); | 
|         $this->role_service = new Service_Role(); | 
|     } | 
|   | 
|     /** | 
|      * 角色列表 | 
|      */ | 
|     public function ListAction(){ | 
|         if($this->getRequest()->isXmlHttpRequest()){ | 
|             $total = $this->role_service->getRoleListTotal(); | 
|             $data['draw'] = !empty($_REQUEST['draw'])?$_REQUEST['draw']:1; | 
|             $data['recordsTotal'] = $total; | 
|             $data['recordsFiltered'] = $total; | 
|             $data['data'] = $this->role_service->getRoleList(); | 
|             exit($this->sendToDataTable($data)); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 添加角色 | 
|      */ | 
|     public function AddAction(){ | 
|          | 
|         if($this->getRequest()->isXmlHttpRequest()){ | 
|             $data['name'] = $this->post('name'); | 
|             if(!$this->_checkRoleName($data['name'])){ | 
|                 exit($this->showError('用户组名已经存在,无法创建', 400, true)); | 
|             } | 
|             $data['description'] = $this->post('description'); | 
|             $data['create_time'] = date('Y-m-d H:i:s', time()); | 
|             $data['edit_time'] = $data['create_time']; | 
|             $data['modules'] = implode(',', $this->post('actions')); | 
|              if($this->role_service->add($data)){ | 
|                  //更新角色权限 | 
|                  Util::updateRoleAuth(); | 
|                  //$this->redis->set('role_modules', $data['modules']); | 
|                 exit($this->showSuccess('用户组添加成功', true)); | 
|             } | 
|             else{ | 
|                 exit($this->showError($this->role_service->error, 400, true)); | 
|             } | 
|         } | 
|         $this->getView()->assign('modules', $this->redis->get('modules')); | 
|     } | 
|   | 
|     /** | 
|      * 修改角色 | 
|      */ | 
|     public function EditAction($id){ | 
|         $role = $this->role_service->getRoleInfo(array('_id' => $id)); | 
|         if($role){ | 
|             if($this->getRequest()->isXmlHttpRequest()){ | 
|                 $data['name'] = $this->post('name'); | 
|                 if($role['name'] != $data['name']){ | 
|                     if(!$this->_checkRoleName($data['name'])){ | 
|                         exit($this->showError('用户组名已经存在,无法创建', 400, true)); | 
|                     } | 
|                 } | 
|                 $data['description'] = $this->post('description'); | 
|                 $data['modules'] = implode(',', $this->post('actions')); | 
|                 $data['edit_time'] = date('Y-m-d H:i:s', time()); | 
|                 if($this->role_service->update($data, array('_id' => $id))){ | 
|                     //更新角色权限 | 
|                      Util::updateRoleAuth(); | 
|                     exit($this->showSuccess('用户修改成功', true)); | 
|                 } | 
|                 else{ | 
|                     exit($this->showError($this->user_service->error, 400, true)); | 
|                 } | 
|             } | 
|             $this->getView()->assign(array('modules' => $this->redis->get('modules'), 'role' => $role)); | 
|         } | 
|         else{ | 
|             $this->redirect('/error/show/type/no_data'); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 验证用户组名是否存在 | 
|      */ | 
|     public function CheckRoleNameAction(){ | 
|         $name = $this->post('name'); | 
|         $result = $this->_checkRoleName($name); | 
|         if($this->isAjax()){ | 
|             exit($this->send(array('valid' => $result))); | 
|         } | 
|         else{ | 
|             return $result; | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 验证用户组名是否存在 | 
|      * @param  [type] $phone [description] | 
|      * @return [type]        [description] | 
|      */ | 
|     public function _checkRoleName($name){ | 
|         $user = $this->role_service->getRoleInfo(array('name' => $name)); | 
|         if($user) | 
|             return false; | 
|         else | 
|             return true; | 
|     } | 
|   | 
| } |