/** * 保存角色 */ public function actionSave() { $data = $this->helpGpost('role'); extract($data); $purview_ids = $this->helpGpost('purview_ids'); if (empty($role_name)) { $this->helpJsRedirect('请填写角色名称'); } if (($role_id = intval($id)) < 1) { //新增角色 $role_id = Roles::addRole($role_name, $this->user_group_id, $remark); } else { //更新角色 Roles::updateRole($role_id, $role_name, $this->user_group_id, $remark); } //删除角色所有权限 Role_Purview::delByRoleId($role_id); $func_ids = array(); if (!empty($purview_ids)) { $func_ids = explode(',', trim($purview_ids, ',')); if (count($func_ids) > 0) { $rows = []; foreach ($func_ids as $func_id) { $func_id = $func_id; $rows[] = [$role_id, $func_id]; } if (count($rows) > 0) { Role_Purview::batchAddPurview(['role_id', 'func_id'], $rows); } } } //更新角色对应的所有用户的权限 $user_ids = User_Role_Relate::getByRoleid($role_id); foreach ($user_ids as $k => $v) { $uid = $v['user_id']; User_Purview::delByUid($uid); $user_purview_rows = []; foreach ($func_ids as $func_id) { $func_id = $func_id; $user_purview_rows[] = [$uid, $func_id]; } if (count($user_purview_rows) > 0) { User_Purview::batchAddPurview(['user_id', 'func_id'], $user_purview_rows); } } $url = Url::toRoute('role/index'); $this->helpRefresh($url, '保存成功'); }
/** * 保存 */ public function actionSave() { $data = $this->helpGpost('users'); extract($data); if (empty($login_name)) { $this->helpJsRedirect('请输入登录名'); } $role_id = $this->helpGpost('role_id', 0); $user_id = 0; if (($id = intval($id)) > 0) { $ret = Users::updateUser($id, $login_name, $real_name, $email, $this->user_group_id, $phone); $user_id = $id; } else { if (empty($password)) { $this->helpJsRedirect('请输入初始密码'); } $password2 = $this->helpGpost('password2'); if ($password != $password2) { $this->helpJsRedirect('两次密码输入不一致'); } //新增 $ret = Users::addUser($login_name, $password, $real_name, $email, $this->user_group_id, $phone); $user_id = $ret; } if (empty($ret)) { $this->helpJsRedirect('操作失败'); } if ($role_id && $user_id) { //保存用户角色关联 User_Role_Relate::delByUid($user_id); User_Role_Relate::addRelate($role_id, $user_id); //删除用户所有权限 User_Purview::delByUid($user_id); $role_purview = Role_Purview::getByRoleId($role_id); if (count($role_purview) > 0) { $rows = []; foreach ($role_purview as $k => $v) { $func_id = $v['func_id']; $rows[] = [$user_id, $func_id]; } User_Purview::batchAddPurview(['user_id', 'func_id'], $rows); } } $url = Url::toRoute('user/index'); $this->redirect($url); }
public function getMyFunc() { $user_id = $this->getId(); $myFunc = array(); //获取角色权限 $func_ids = []; $myRoles = User_Role_Relate::getByUid($user_id); if (!empty($myRoles)) { foreach ($myRoles as $role) { $rolePurviews = Role_Purview::getByRoleId($role['role_id']); foreach ($rolePurviews as $rolePurview) { $func_ids[] = $rolePurview['func_id']; } } } //获取用户权限 $userPurviews = User_Purview::getByUid($user_id); if (!empty($userPurviews)) { foreach ($userPurviews as $purview) { $func_ids[] = $purview['func_id']; } } $condition = ['id' => $func_ids]; $myFunc = Func::search($condition, 0, 0, array('id' => SORT_ASC), false, 'id'); return $myFunc['list']; }