colly_wyx
2018-05-29 d479d9fbcb37f8b861031c94a83d0e37761210c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?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['start'] = !empty($_REQUEST['start'])?$_REQUEST['start']:0;
            $data['length'] = !empty($_REQUEST['length'])?$_REQUEST['length']:10;
            $data['recordsTotal'] = $total;
            $data['recordsFiltered'] = $total;
            $data['data'] = $this->role_service->getRoleList(array(), array(), array(), $data['length'], $data['start']);
            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;
    }
 
}