first commit
This commit is contained in:
58
backend/app/Services/Auth/AuthService.php
Normal file
58
backend/app/Services/Auth/AuthService.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace App\Services\Auth;
|
||||
|
||||
use App\Models\Auth\Permissions;
|
||||
|
||||
class AuthService{
|
||||
|
||||
/**
|
||||
* @title 用户登录
|
||||
*
|
||||
* @param [type] $request
|
||||
* @return void
|
||||
*/
|
||||
public function login(){
|
||||
$credentials = request(['username', 'password']);
|
||||
|
||||
if (! $token = auth()->attempt($credentials)) {
|
||||
abort(0, 'Unauthorized');
|
||||
}
|
||||
$user = auth()->user();
|
||||
$user['token'] = $token;
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 获取已授权菜单
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getAuthMenu(){
|
||||
$order = "sort asc, id desc";
|
||||
$map = [];
|
||||
if(auth()->user()['uid'] != env('ADMIN_ROOT')){
|
||||
$map[] = ['name', 'IN', auth()->user()['permission']];
|
||||
}
|
||||
$map[] = ['type', '<>', 'button'];
|
||||
$list = Permissions::where($map)->orderBy('sort', 'asc')->orderBy('id', 'desc')->get();
|
||||
return (new \App\Support\Tree())->listToTree($list->toArray(), 'id', 'parent_id', 'children');
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 获取已授权菜单
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getAuthPermissions(){
|
||||
$map = [];
|
||||
if(auth()->user()['uid'] != env('ADMIN_ROOT')){
|
||||
$map[] = ['name', 'IN', request()->auth()['permission']];
|
||||
}
|
||||
$list = Permissions::where($map)->get();
|
||||
$data = [];
|
||||
foreach($list as $item){
|
||||
$data[] = $item['name'];
|
||||
};
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
139
backend/app/Services/Auth/UsersLogService.php
Normal file
139
backend/app/Services/Auth/UsersLogService.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace App\Services\Auth;
|
||||
|
||||
use App\Models\Auth\Users;
|
||||
use App\Models\Auth\UsersLog;
|
||||
// use xin\helper\Server;
|
||||
// use xin\helper\Time;
|
||||
|
||||
class UsersLogService{
|
||||
|
||||
/**
|
||||
* @title 获取用户操作日志
|
||||
*
|
||||
* @param [type] $request
|
||||
* @return void
|
||||
*/
|
||||
public function getUserLogList($request){
|
||||
$param = $request->all();
|
||||
$map = [];
|
||||
if(isset($param['date_type']) && $param['date_type']){
|
||||
$time = Time::today();
|
||||
if($param['date_type'] == 'seven'){
|
||||
$time = Time::dayToNow(7);
|
||||
}elseif($param['date_type'] == 'yesterday'){
|
||||
$time = Time::yesterday(7);
|
||||
}elseif($param['date_type'] == 'week'){
|
||||
$time = Time::week(7);
|
||||
}
|
||||
$map[] = ['create_time', 'BETWEEN TIME', $time];
|
||||
}
|
||||
if(isset($param['method']) && $param['method']){
|
||||
$map[] = ['method', '=', strtoupper($param['method'])];
|
||||
}
|
||||
if(isset($param['date']) && $param['date'] && count($param['date']) == 2){
|
||||
$map[] = ['create_time', 'BETWEEN TIME', $param['date']];
|
||||
}
|
||||
|
||||
$list = UsersLog::with(['user'])->where($map)->orderBy('create_time', 'desc')->paginate($param['pageSize']);
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 获取用户操作日志
|
||||
*
|
||||
* @param [type] $request
|
||||
* @return void
|
||||
*/
|
||||
public function getMyLogList($request){
|
||||
$param = $request->param();
|
||||
$map = [];
|
||||
$map[] = ['uid', '=', $request->user['uid']];
|
||||
if(isset($param['method']) && $param['method']){
|
||||
$map[] = ['method', '=', strtoupper($param['method'])];
|
||||
}
|
||||
if(isset($param['date_type']) && $param['date_type']){
|
||||
$time = Time::today();
|
||||
if($param['date_type'] == 'seven'){
|
||||
$time = Time::dayToNow(7);
|
||||
}elseif($param['date_type'] == 'yesterday'){
|
||||
$time = Time::yesterday(7);
|
||||
}elseif($param['date_type'] == 'week'){
|
||||
$time = Time::week(7);
|
||||
}
|
||||
$map[] = ['create_time', 'BETWEEN TIME', $time];
|
||||
}
|
||||
if(isset($param['date']) && $param['date'] && count($param['date']) == 2){
|
||||
$map[] = ['create_time', 'BETWEEN TIME', $param['date']];
|
||||
}
|
||||
|
||||
$list = UsersLog::with(['user'])->where($map)->order('create_time desc')->paginate($request->pageConfig);
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 用户操作记录
|
||||
*
|
||||
* @param [type] $request
|
||||
* @param integer $code
|
||||
* @return void
|
||||
*/
|
||||
public function record($request, $code = 200){
|
||||
$param = array_merge($request->get(), $request->post());
|
||||
|
||||
if(!isset($request->user['uid'])){
|
||||
return false;
|
||||
}
|
||||
$param = strlen(json_encode($param)) > 1000 ? 'param to loog' : json_encode($param);
|
||||
$data = [
|
||||
'uid' => isset($request->user['uid']) ? $request->user['uid'] : '',
|
||||
'title' => self::getCurrentTitle($request),
|
||||
'route' => $request->baseUrl(),
|
||||
'params' => $param,
|
||||
'method' => $request->method(),
|
||||
'client_ip' => Server::getRemoteIp(),
|
||||
'browser' => $request->header('user-agent'),
|
||||
'code' => $code
|
||||
];
|
||||
if($data['route'] == '/admin/system/log/index'){
|
||||
return false;
|
||||
}
|
||||
UsersLog::create($data);
|
||||
}
|
||||
|
||||
protected static function getCurrentTitle($request) {
|
||||
$mate = '';
|
||||
$controller = strtr(strtolower($request->controller()), '.', '\\');
|
||||
$action = $request->action();
|
||||
$class = "\\app\\controller\\" . $controller;
|
||||
if (class_exists($class)) {
|
||||
$reflection = new \ReflectionClass($class);
|
||||
$group_doc = self::Parser($reflection->getDocComment());
|
||||
if (isset($group_doc['title'])) {
|
||||
$mate = $group_doc['title'];
|
||||
}
|
||||
$method = $reflection->getMethods(\ReflectionMethod::IS_FINAL | \ReflectionMethod::IS_PUBLIC);
|
||||
foreach ($method as $key => $v) {
|
||||
if ($action == $v->name) {
|
||||
$title_doc = self::Parser($v->getDocComment());
|
||||
if (isset($title_doc['title'])) {
|
||||
$mate = isset($title_doc['title']) ? $title_doc['title'] : '';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $mate;
|
||||
}
|
||||
|
||||
protected static function Parser($text) {
|
||||
$doc = new \doc\Doc();
|
||||
return $doc->parse($text);
|
||||
}
|
||||
}
|
||||
195
backend/app/Services/Auth/UsersService.php
Normal file
195
backend/app/Services/Auth/UsersService.php
Normal file
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace App\Services\Auth;
|
||||
|
||||
use App\Models\Auth\Users;
|
||||
use App\Models\Auth\RolesAccess;
|
||||
use think\facade\Config;
|
||||
|
||||
class UsersService{
|
||||
|
||||
/**
|
||||
* @title 获取管理员列表
|
||||
*
|
||||
* @param [type] $request
|
||||
* @return void
|
||||
*/
|
||||
public function getUserList(){
|
||||
$param = request()->input();
|
||||
$map = [];
|
||||
if(isset($param['name']) && $param['name']){
|
||||
$map[] = ["username|nickname", "like", "%{$param['name']}%"];
|
||||
}
|
||||
if(isset($param['department_id']) && $param['department_id']){
|
||||
$map[] = ["department_id", "IN", getDepartmentChild($param['department_id'])];
|
||||
}
|
||||
if(isset($param['role_id']) && $param['role_id']){
|
||||
$exp = is_array($param['role_id']) ? "IN" : "=";
|
||||
$subMap = [['role_id', $exp, $param['role_id']]];
|
||||
$map[] = ['uid', "IN", function($q) use($subMap){
|
||||
$q->name('user_has_roles')->where($subMap)->field('uid');
|
||||
}];
|
||||
}
|
||||
$list = Users::with(['roles', 'department'])->where($map)->orderBy('uid')->paginate()->each(function($item){
|
||||
$roleId = [];
|
||||
$roleName = [];
|
||||
$identify = [];
|
||||
foreach($item->roles as $val){
|
||||
$roleId[] = $val['id'];
|
||||
$roleName[] = $val['title'];
|
||||
$identify[] = $val['identify'];
|
||||
}
|
||||
$item->role_id = $roleId;
|
||||
$item->roleName = $roleName;
|
||||
$item->identify = $identify;
|
||||
});
|
||||
return $list;
|
||||
}
|
||||
/**
|
||||
* 创建用户
|
||||
*
|
||||
* @param [type] $request
|
||||
* @return void
|
||||
*/
|
||||
public function createUsers($request){
|
||||
$param = $request->param();
|
||||
$data = [
|
||||
'username' => $param['username'],
|
||||
'nickname' => $param['nickname'],
|
||||
'password' => $param['password'],
|
||||
'department_id' => $param['department_id']
|
||||
];
|
||||
|
||||
$user = Users::create($data);
|
||||
return $user;
|
||||
}
|
||||
/**
|
||||
* @title 批量导入
|
||||
*
|
||||
* @param [type] $request
|
||||
* @return void
|
||||
*/
|
||||
public function insertAll($request){
|
||||
$data = $request->param('data');
|
||||
$users = [];
|
||||
foreach($data as $item){
|
||||
$user = Users::where('uid', $item['XH'])->findOrEmpty();
|
||||
if($user->isEmpty()){
|
||||
$users = [
|
||||
'uid' => $item['XH'],
|
||||
'username' => $item['XH'],
|
||||
'nickname' => $item['XM'],
|
||||
'password' => md5(''),
|
||||
'email' => "e@mail.cn",
|
||||
'avatar' => $this->request->domain() . str_replace("/pic", "/", $item['RXZP']),
|
||||
'creator_id' => 1,
|
||||
'department_id' => 3,
|
||||
'user_type' => $item['PYCC'],
|
||||
'status' => 1,
|
||||
'sex' => $item['XB'],
|
||||
'xueyuan' => $item['XY'],
|
||||
'student_class' => $item['BJMC'],
|
||||
];
|
||||
Users::create($users);
|
||||
}
|
||||
}
|
||||
return $users;
|
||||
}
|
||||
public function updateUsers($request){
|
||||
$param = $request->param();
|
||||
$roles = isset($param['role_id']) ? $param['role_id'] : [];
|
||||
$user = Users::where('uid', '=', $param['uid'])->findOrEmpty();
|
||||
if(!$user->isEmpty()){
|
||||
if(isset($param['nickname']) && $param['nickname']){
|
||||
$data['nickname'] = $param['nickname'];
|
||||
}
|
||||
if(isset($param['email']) && $param['email']){
|
||||
$data['email'] = $param['email'];
|
||||
}
|
||||
if(isset($param['avatar']) && $param['avatar']){
|
||||
$data['avatar'] = $param['avatar'];
|
||||
}
|
||||
if(isset($param['department_id']) && $param['department_id']){
|
||||
$data['department_id'] = is_array($param['department_id']) ? end($param['department_id']) : $param['department_id'];
|
||||
}
|
||||
$user->save($data);
|
||||
}
|
||||
if(!empty($roles)){
|
||||
$this->updateRoles($param['uid'], $roles); //更新角色
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function updateUserPassword($request){
|
||||
$user = Users::where('uid', '=', $request->user['uid'])->findOrEmpty();
|
||||
$params = $request->param();
|
||||
if(!$user->isEmpty()){
|
||||
if(password_verify($params['oldpassword'], $user->password)){
|
||||
$user->save(['password' => $params['password']]);
|
||||
}else{
|
||||
throw new \think\Exception("当前密码错误!", 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 获取用户权限信息
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getUserAuth($uid){
|
||||
$user = Users::with(['roles', 'roles.permissions', 'department'])->where('uid', '=', $uid)->findOrEmpty();
|
||||
if(!$user->isEmpty()){
|
||||
$permission = [];
|
||||
$apiList = [];
|
||||
$data_range = [];
|
||||
$mobile_module = [];
|
||||
foreach ($user->roles as $role) {
|
||||
$data_range[] = $role['data_range'];
|
||||
$mobile_module = array_merge($mobile_module, $role['mobile_module']);
|
||||
foreach($role->permissions as $item){
|
||||
$permission[] = $item['name'];
|
||||
$apiList = array_merge($apiList, $item['api_list']);
|
||||
}
|
||||
}
|
||||
$user['permission'] = $permission;
|
||||
$user['data_range'] = isset($data_range[0]) ? $data_range[0] : 1; //暂时适配到过角色的数据权限问题 后续调整
|
||||
$user['mobile_module'] = $mobile_module;
|
||||
$user['apiList'] = $apiList;
|
||||
return $user;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function userInfo($uid){
|
||||
$user = $this->getUserAuth($uid);
|
||||
return $user->append(['access_token']);
|
||||
}
|
||||
/**
|
||||
* @title 更新用户角色
|
||||
*
|
||||
* @param int $uid
|
||||
* @param array $roles
|
||||
* @param int $manage_class 用户班级权限
|
||||
* @return void
|
||||
*/
|
||||
public function updateRoles($uid, $roles, $manage_class = 0){
|
||||
RolesAccess::where('uid', '=', $uid)->delete();
|
||||
$save = [];
|
||||
foreach ($roles as $role) {
|
||||
$save[] = ['role_id' => $role, 'uid' => $uid];
|
||||
}
|
||||
(new RolesAccess())->saveAll($save);
|
||||
if($uid && $manage_class){
|
||||
Users::update(['manage_class' => $manage_class], ['uid' => $uid]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user