| // +------------------------------------------------+ namespace app\common\service; use app\common\model\Auth as AuthModel; use app\common\model\AuthUser as AuthUserModel; use app\common\validate\Auth as AuthValidate; use think\Db; use tools\Tree; class Auth extends _Init { /** * 添加授权分组 * @param [type] $data 要添加的数据 */ public static function add($data) { $validate = new AuthValidate(); if (!$validate->check($data)) { return $validate->getError(); } if (AuthModel::create($data)) { Logs::write('增加分组', $data); return true; } else { return '增加失败'; } } /** * 编辑分组信息 * @param [type] $data 要更新的信息 * @return [type] 返回的信息 true 和 错误信息 */ public static function edit($data) { $validate = new AuthValidate(); if (!$validate->check($data)) { return $validate->getError(); } if (AuthModel::update($data)) { Logs::write('编辑分组', $data); return true; } else { return '编辑失败'; } } /** * 删除方法 * @param [type] $id 要删除的id */ public static function del($id) { $info = AuthModel::get($id); if (!$info) { return '数据不存在'; } elseif ($info->getAttr('user_count') > 0) { return '分组下有用户,不允许删除'; } elseif ($info->save(['status' => -1])) { Logs::write('删除分组', $info); return true; } else { return '删除失败'; } } /** * 修改分组状态 * @param [type] $id 分组id * @param [type] $status 分组的状态 * @return [type] 返回的信息 true 和 错误信息 */ public static function status($id, $status) { $info = AuthModel::get($id); if (!$info) { return '分组不存在'; } elseif ($info->save(['status' => $status])) { Logs::write('修改分组状态', ['status' => $status]); return true; } else { return '状态修改失败'; } } /** * 设置授权菜单节点 * @param [type] $id [description] * @param array $rules [description] */ public static function setAuthRuels($id, $rules = []) { $info = AuthModel::get($id); if (!$info) { return '授权组不存在'; } if (empty($rules)) { $data['rules'] = ''; } else { sort($rules); $data['rules'] = implode(',', $rules); } if ($info->save($data)) { Logs::write('设置授权菜单节点', $data); return true; } else { return '设置授权菜单节点失败'; } } /** * 获取组所有授权节点 * @param [type] $id 分组id * @return [type] [description] */ public static function getAuthRules($id) { $map = [ 'status' => 1, 'auth' => 1, ]; // 获取节点列表 $node_list = Db::name('Menu')->where($map)->order('sort asc')->select(); $auth_rules = explode(',', AuthModel::where('id', $id)->value('rules')); foreach ($node_list as $key => $value) { if (in_array($value['id'], $auth_rules)) { $node_list[$key]['checked'] = "checked"; } else { $node_list[$key]['checked'] = ""; } } return Tree::list2tree($node_list); } /** * 获取分组内未授权的所有用户 * @param [type] $id 分组id * @return [type] 用户ids */ public static function getAuthedUids($id) { return AuthUserModel::where('auth_id', $id)->column('uid') ?: [0]; } /** * 增加用户 * @param [type] $id 分组id * @param array $uids 用户ids */ public static function setAuthUser($id, $uid = []) { $AuthUserModel = new AuthUserModel(); if (empty($uid)) { return '授权用户不能是空的'; } $list = []; foreach ($uid as $userid) { $list[] = ['auth_id' => $id, 'uid' => $userid]; } if ($AuthUserModel->saveAll($list)) { Logs::write('授权用户', ['auth_id' => $id, 'uid' => $uid]); return true; } else { return '授权用户失败'; } } /** * 移除授权 * @param [type] $id 分组id * @param [type] $uid 用户id * @return [type] */ public static function removeAuthUser($id, $uid = []) { $map = [ 'auth_id' => $id, 'uid' => ['in', $uid], ]; if (AuthUserModel::where($map)->delete()) { Logs::write('移除授权', ['auth_id' => $id, 'uid' => $uid]); return true; } else { return '移除授权失败'; } } }