예제 #1
0
 /**
  * ription 给角色设置操作权限
  *
  * @author
  *
  * @final
  *
  * @param int $id
  *            角色id
  * @param array $priv
  *            权限节点
  * @param int $r_action_type
  *            读操作
  * @param int $w_action_type
  *            写操作
  */
 public function priv($id = 0)
 {
     if (!$this->check_power('cpuser_role')) {
         return;
     }
     $id = intval($id);
     $id && ($role = RoleModel::get_role($id));
     if (!$role) {
         message('角色不存在');
         return;
     }
     $sql = "select class_id, class_name from {pre}question_class";
     $res = $this->db->query($sql)->result_array();
     $new_q_type = array();
     foreach ($res as $row) {
         $new_q_type[$row['class_id']] = $row['class_name'];
     }
     if ($this->input->post('dosubmit')) {
         $privs = $this->input->post('priv');
         if (empty($privs)) {
             $privs = array();
         }
         $subject_id_arr = array();
         $grade_id_arr = array();
         $q_type_id_arr = array();
         $arr = array();
         // pr($privs,1);
         $subject_id_count = 0;
         // 学科为空,过滤掉group_question,group_exam_paper
         if (empty($privs['group_subject'])) {
             unset($privs['group_question']);
             unset($privs['group_exam_paper']);
         }
         foreach ($privs as $key => $val) {
             // 单独区分出学科管理
             if ($key == 'group_subject') {
                 $subject_id_arr[] = count((array) $val) == count(C('subject')) ? '-1' : implode(',', (array) $val);
             } elseif ($key == 'group_grade') {
                 $grade_id_arr[] = count((array) $val) == count(C('grades')) ? '-1' : implode(',', (array) $val);
             } elseif ($key == 'group_q_type') {
                 $q_type_id_arr[] = count((array) $val) == count($new_q_type) ? '-1' : implode(',', (array) $val);
             } else {
                 $arr[] = implode(',', (array) $val);
             }
         }
         // 题库读写权限
         $r_action_type = intval($this->input->post('r_action_type'));
         $w_action_type = intval($this->input->post('w_action_type'));
         $r_action_type = $r_action_type < 1 || $r_action_type > 3 ? 1 : $r_action_type;
         $w_action_type = $w_action_type < 1 || $w_action_type > 3 ? 1 : $w_action_type;
         $action_type = array('question' => array('r' => $r_action_type, 'w' => $w_action_type));
         $this->db->update('role', array('subject_id' => implode(',', $subject_id_arr), 'grade_id' => implode(',', $grade_id_arr), 'q_type_id' => implode(',', $q_type_id_arr), 'action_list' => implode(',', $arr), 'action_type' => serialize($action_type)), array('role_id' => $id));
         admin_log('edit', 'role_priv', $id);
         message('权限设置成功', 'admin/role/priv/' . $id);
     } else {
         // 读写权限
         $data['action_type'] = array('1' => '自己创建', '2' => '所在学科', '3' => '所有学科');
         // 所有学科
         $data['subject'] = C('subject');
         $data['grade'] = C('grades');
         $data['q_type'] = $new_q_type;
         // $role['privs_subject'] = explode(',', $role['subject_id']);
         $role['privs_subject'] = $role['subject_id'] == '-1' ? array_keys($data['subject']) : explode(',', $role['subject_id']);
         $role['privs_grade'] = $role['grade_id'] == '-1' ? array_keys($data['grade']) : explode(',', $role['grade_id']);
         $role['privs_q_type'] = $role['q_type_id'] == '-1' ? array_keys($data['q_type']) : explode(',', $role['q_type_id']);
         $role['privs'] = explode(',', $role['action_list']);
         $data['roles'] = C('roles', 'app/admin/roles');
         $action_type = @unserialize($role['action_type']);
         $role['action_type'] = is_array($action_type) ? $action_type : array();
         $data['user'] = $role;
         // 模版
         $this->load->view('role/priv', $data);
     }
 }