Exemplo n.º 1
0
 public function saveAction()
 {
     $returnJson = array();
     $returnJson['error'] = false;
     $roleModel = new Admin_Model_Role();
     $data = array('role_name' => $_POST['role_name'], 'role_acl_key' => $_POST['role_acl_key']);
     $validate = array('role_name' => array('required' => true, 'alphanumeric', 'roleExists'), 'role_acl_key' => array('required' => true, 'ealphanumeric', 'roleExists'));
     // var_dump($validate);
     if ($_POST['save_type'] == 'update') {
         $data['role_id'] = intval($_POST['role_id']);
         $validate['role_name'][1] = 'roleExistsUpdate';
         $validate['role_acl_key'][1] = 'roleExistsUpdate';
     }
     if ($roleModel->isValidRow($data, $validate)) {
         // узнаём уровень вложенности предка
         $parentId = intval($_POST['role_parent_id']);
         if ($parentId) {
             $result = $roleModel->find(K_Db_Select::create()->fields('role_level,role_name')->where(array('role_id' => $parentId)));
             if (isset($result) && is_array($result) && count($result)) {
                 $roleParent = $result[0]->toArray();
                 $roleParentName = $roleParent['role_name'];
                 $data['role_parent_id'] = $parentId;
                 $data['role_level'] = $roleParent['role_level'] + 1;
             } else {
                 $data['role_level'] = 0;
                 $data['role_parent_id'] = null;
                 $roleParentName = 'Нет предка';
             }
         } else {
             $data['role_level'] = 0;
             $data['role_parent_id'] = null;
             $roleParentName = 'Нет предка';
         }
         if ($_POST['save_type'] == 'add') {
             $role_id = $roleModel->save($data);
             $returnJson['role']['type'] = 'add';
             $returnJson['role']['id'] = $role_id;
         } else {
             if ($_POST['save_type'] == 'update') {
                 $role_id = $roleModel->update($data, array('role_id' => (int) $_POST['role_id']));
                 $returnJson['role']['type'] = 'update';
                 $role_id = intval($_POST['role_id']);
                 $returnJson['role']['id'] = $_POST['role_id'];
             }
         }
         $returnJson['role']['name'] = $data['role_name'];
         $returnJson['role']['role_acl_key'] = $data['role_acl_key'];
         $returnJson['role']['parentid'] = $data['role_parent_id'] == null ? '' : $data['role_parent_id'];
         $returnJson['role']['parentname'] = $roleParentName;
         $returnJson['error'] = false;
         $returnJson['msg'] = "<strong>OK:</strong>Роль удачно сохранена теперь вы можете настроить доступы для неё";
     } else {
         $returnJson['error'] = true;
         $returnJson['msg'] = $roleModel->getErrorsD($this->dictionary);
     }
     $this->putJSON($returnJson);
 }
Exemplo n.º 2
0
 public function editAction()
 {
     // 返回值数组
     $result = array('success' => true, 'info' => '编辑成功');
     $request = $this->getRequest()->getParams();
     $now = date('Y-m-d H:i:s');
     $user_session = new Zend_Session_Namespace('user');
     $user = $user_session->user_info['user_id'];
     $json = json_decode($request['json']);
     $updated = $json->updated;
     $inserted = $json->inserted;
     $deleted = $json->deleted;
     $role = new Admin_Model_Role();
     $member = new Admin_Model_Member();
     if (count($updated) > 0) {
         foreach ($updated as $val) {
             if ($role->fetchAll("id != " . $val->id . " and name = '" . $val->name . "'")->count() > 0) {
                 $result['result'] = false;
                 $result['info'] = '角色:' . $val->name . ' 重名';
                 echo Zend_Json::encode($result);
                 exit;
             } else {
                 $data = array('parentid' => $val->parentId, 'name' => $val->name, 'description' => $val->description, 'remark' => $val->remark, 'active' => $val->active, 'update_time' => $now, 'update_user' => $user);
                 $where = "id = " . $val->id;
                 try {
                     $role->update($data, $where);
                 } catch (Exception $e) {
                     $result['result'] = false;
                     $result['info'] = $e->getMessage();
                     echo Zend_Json::encode($result);
                     exit;
                 }
             }
         }
     }
     if (count($inserted) > 0) {
         foreach ($inserted as $val) {
             if ($role->fetchAll("name = '" . $val->name . "'")->count() > 0) {
                 $result['result'] = false;
                 $result['info'] = '角色:' . $val->name . ' 重名';
                 echo Zend_Json::encode($result);
                 exit;
             } else {
                 $data = array('parentid' => $val->parentId, 'name' => $val->name, 'description' => $val->description, 'remark' => $val->remark, 'active' => $val->active, 'create_time' => $now, 'create_user' => $user, 'update_time' => $now, 'update_user' => $user);
                 try {
                     $role_id = $role->insert($data);
                     // 自动添加系统管理员到新角色中
                     $adminIds = $member->getUserids("系统管理员");
                     foreach ($adminIds as $admin) {
                         if ($member->fetchAll("user_id = " . $admin . " and role_id = " . $role_id)->count() == 0) {
                             try {
                                 $member->insert(array('user_id' => $admin, 'role_id' => $role_id));
                             } catch (Exception $e) {
                                 $result['result'] = false;
                                 $result['info'] = $e->getMessage();
                                 echo Zend_Json::encode($result);
                                 exit;
                             }
                         }
                     }
                 } catch (Exception $e) {
                     $result['result'] = false;
                     $result['info'] = $e->getMessage();
                     echo Zend_Json::encode($result);
                     exit;
                 }
             }
         }
     }
     if (count($deleted) > 0) {
         foreach ($deleted as $val) {
             $adminIds = $member->getMemberWithNoManager($val->id);
             if (count($adminIds) == 0) {
                 try {
                     $role->deleteRoleTreeData($val->id);
                 } catch (Exception $e) {
                     $result['result'] = false;
                     $result['info'] = $e->getMessage();
                     echo Zend_Json::encode($result);
                     exit;
                 }
             } else {
                 $result['result'] = false;
                 $result['info'] = '角色ID ' . $val->id . '有管理员以外的其它成员,请先删除其它成员';
                 echo Zend_Json::encode($result);
                 exit;
             }
         }
     }
     echo Zend_Json::encode($result);
     exit;
 }