delete user module
This commit is contained in:
110
catch/permissions/Auth.php
Normal file
110
catch/permissions/Auth.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
namespace catchAdmin\Auth;
|
||||
|
||||
use catchAdmin\permissions\model\Permissions;
|
||||
use catchAdmin\permission\model\Users;
|
||||
use catcher\exceptions\LoginFailedException;
|
||||
use thans\jwt\facade\JWTAuth;
|
||||
use think\facade\Session;
|
||||
|
||||
class Auth
|
||||
{
|
||||
protected const USER_ID = 'catch_uid';
|
||||
/**
|
||||
* 登陆
|
||||
*
|
||||
* @time 2019年11月28日
|
||||
* @param $params
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws LoginFailedException
|
||||
*/
|
||||
public static function login($params)
|
||||
{
|
||||
$user = Users::where('email', $params['email'])->find();
|
||||
|
||||
if (!$user) {
|
||||
throw new LoginFailedException('登陆失败, 请检查用户名和密码');
|
||||
}
|
||||
|
||||
if (!password_verify($params['password'], $user->password)) {
|
||||
throw new LoginFailedException('登陆失败, 请检查用户名和密码');
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Session::set(self::getLoginUserKey(), $user);
|
||||
|
||||
return JWTAuth::builder([self::USER_ID => $user->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登陆
|
||||
*
|
||||
* @time 2019年11月28日
|
||||
* @return bool
|
||||
*/
|
||||
public static function logout(): bool
|
||||
{
|
||||
Session::delete(self::getLoginUserKey());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月15日
|
||||
* @return mixed
|
||||
*/
|
||||
public static function user()
|
||||
{
|
||||
$user = Users::where('id', JWTAuth::auth()[self::USER_ID])
|
||||
->field(['id', 'username', 'status'])->find();
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public static function getUserInfo()
|
||||
{
|
||||
$user = self::user();
|
||||
|
||||
$roles = $user->getRoles();
|
||||
|
||||
$user->permissions = Permissions::whereIn('id', $user->getPermissionsBy())
|
||||
->field(['permission_name as title', 'route', 'icon'])
|
||||
->select();
|
||||
|
||||
$user->roles = $roles;
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月15日
|
||||
* @return string
|
||||
*/
|
||||
protected static function getLoginUserKey(): string
|
||||
{
|
||||
// return md5(self::USER_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月15日
|
||||
* @param $mark
|
||||
* @param $module
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasPermissions($mark, $module): bool
|
||||
{
|
||||
$permissionIds = self::user()->get->getPermissionsBy();
|
||||
|
||||
$permissionId = Permissions::where('module', $module)
|
||||
->where('permission_mark', $mark)->value('id');
|
||||
|
||||
return in_array($permissionId, $permissionIds);
|
||||
}
|
||||
}
|
34
catch/permissions/AuthTokenMiddleware.php
Normal file
34
catch/permissions/AuthTokenMiddleware.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions;
|
||||
|
||||
use catcher\Code;
|
||||
use catcher\exceptions\FailedException;
|
||||
use thans\jwt\exception\TokenBlacklistException;
|
||||
use thans\jwt\exception\TokenExpiredException;
|
||||
use thans\jwt\exception\TokenInvalidException;
|
||||
use thans\jwt\facade\JWTAuth;
|
||||
use think\Middleware;
|
||||
|
||||
class AuthTokenMiddleware extends Middleware
|
||||
{
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
try {
|
||||
JWTAuth::auth();
|
||||
} catch (\Exception $e) {
|
||||
if ($e instanceof TokenExpiredException) {
|
||||
throw new FailedException('token 过期', Code::LOST_LOGIN);
|
||||
}
|
||||
if ($e instanceof TokenBlacklistException) {
|
||||
throw new FailedException('token 被加入黑名单', Code::LOST_LOGIN);
|
||||
}
|
||||
if ($e instanceof TokenInvalidException) {
|
||||
throw new FailedException('token 不合法', Code::LOST_LOGIN);
|
||||
}
|
||||
|
||||
throw new FailedException('登录用户不合法', Code::LOST_LOGIN);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
229
catch/permissions/controller/User.php
Normal file
229
catch/permissions/controller/User.php
Normal file
@@ -0,0 +1,229 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions\controller;
|
||||
|
||||
use catcher\base\CatchRequest as Request;
|
||||
use catchAdmin\permissions\model\Permissions;
|
||||
use catchAdmin\permissions\model\Roles;
|
||||
use catchAdmin\user\model\Users;
|
||||
use catchAdmin\user\request\CreateRequest;
|
||||
use catchAdmin\user\request\UpdateRequest;
|
||||
use catcher\base\CatchController;
|
||||
use catcher\CatchAuth;
|
||||
use catcher\CatchCacheKeys;
|
||||
use catcher\CatchResponse;
|
||||
use catcher\Tree;
|
||||
use catcher\Utils;
|
||||
use think\facade\Cache;
|
||||
|
||||
class User extends CatchController
|
||||
{
|
||||
protected $user;
|
||||
|
||||
public function __construct(Users $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月04日
|
||||
* @param Request $request
|
||||
* @return string
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
return CatchResponse::paginate($this->user->getList($request->param()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*
|
||||
* @time 2020年01月07日
|
||||
* @param CatchAuth $auth
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function info(CatchAuth $auth)
|
||||
{
|
||||
$user = $auth->user();
|
||||
|
||||
$roles = $user->getRoles();
|
||||
|
||||
$permissionIds = $user->getPermissionsBy($user->id);
|
||||
// 缓存用户权限
|
||||
Cache::set(CatchCacheKeys::USER_PERMISSIONS . $user->id, $permissionIds);
|
||||
|
||||
$user->permissions = Permissions::getCurrentUserPermissions($permissionIds);
|
||||
|
||||
$user->roles = $roles;
|
||||
|
||||
// 用户数据权限
|
||||
// $user->data_range = Roles::getDepartmentUserIdsBy($roles);
|
||||
|
||||
return CatchResponse::success($user);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月06日
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
public function create()
|
||||
{}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param CreateRequest $request
|
||||
* @time 2019年12月06日
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function save(CreateRequest $request)
|
||||
{
|
||||
$this->user->storeBy($request->post());
|
||||
|
||||
$this->user->attach($request->param('roles'));
|
||||
|
||||
$this->user->attachJobs($request->param('jobs'));
|
||||
|
||||
return CatchResponse::success('', '添加成功');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月04日
|
||||
* @param $id
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function read($id)
|
||||
{
|
||||
$user = $this->user->findBy($id);
|
||||
$user->roles = $user->getRoles();
|
||||
$user->jobs = $user->getJobs();
|
||||
return CatchResponse::success($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function edit($id){}
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月04日
|
||||
* @param $id
|
||||
* @param UpdateRequest $request
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function update($id, UpdateRequest $request)
|
||||
{
|
||||
$this->user->updateBy($id, $request->post());
|
||||
|
||||
$user = $this->user->findBy($id);
|
||||
|
||||
$user->detach();
|
||||
$user->detachJobs();
|
||||
|
||||
if (!empty($request->param('roles'))) {
|
||||
$user->attach($request->param('roles'));
|
||||
}
|
||||
if (!empty($request->param('jobs'))) {
|
||||
$user->attachJobs($request->param('jobs'));
|
||||
}
|
||||
return CatchResponse::success();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月04日
|
||||
* @param $id
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$ids = Utils::stringToArrayBy($id);
|
||||
|
||||
foreach ($ids as $_id) {
|
||||
$user = $this->user->findBy($_id);
|
||||
// 删除角色
|
||||
$user->detach();
|
||||
// 删除岗位
|
||||
$user->detachJobs();
|
||||
|
||||
$this->user->deleteBy($_id);
|
||||
}
|
||||
|
||||
return CatchResponse::success();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月07日
|
||||
* @param $id
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function switchStatus($id): \think\response\Json
|
||||
{
|
||||
$ids = Utils::stringToArrayBy($id);
|
||||
|
||||
foreach ($ids as $_id) {
|
||||
|
||||
$user = $this->user->findBy($_id);
|
||||
|
||||
$this->user->updateBy($_id, [
|
||||
'status' => $user->status == Users::ENABLE ? Users::DISABLE : Users::ENABLE,
|
||||
]);
|
||||
}
|
||||
|
||||
return CatchResponse::success([], '操作成功');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月07日
|
||||
* @param $id
|
||||
* @return \think\response\Json
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
*/
|
||||
public function recover($id): \think\response\Json
|
||||
{
|
||||
$trashedUser = $this->user->findBy($id, ['*'], true);
|
||||
|
||||
if ($this->user->where('email', $trashedUser->email)->find()) {
|
||||
return CatchResponse::fail(sprintf('该恢复用户的邮箱 [%s] 已被占用', $trashedUser->email));
|
||||
}
|
||||
|
||||
return CatchResponse::success($this->user->recover($id));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param Request $request
|
||||
* @param Roles $roles
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getRoles(Request $request, Roles $roles): \think\response\Json
|
||||
{
|
||||
$roles = Tree::done($roles->getList());
|
||||
|
||||
$roleIds = [];
|
||||
if ($request->param('uid')) {
|
||||
$userHasRoles = $this->user->findBy($request->param('uid'))->getRoles();
|
||||
foreach ($userHasRoles as $role) {
|
||||
$roleIds[] = $role->pivot->role_id;
|
||||
}
|
||||
}
|
||||
|
||||
return CatchResponse::success([
|
||||
'roles' => $roles,
|
||||
'hasRoles' => $roleIds,
|
||||
]);
|
||||
}
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class Users extends Migrator
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
|
||||
*
|
||||
* The following commands can be used in this method and Phinx will
|
||||
* automatically reverse them when rolling back:
|
||||
*
|
||||
* createTable
|
||||
* renameTable
|
||||
* addColumn
|
||||
* renameColumn
|
||||
* addIndex
|
||||
* addForeignKey
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('users',array('engine'=>'Innodb', 'comment' => '用户表', 'signed' => false));
|
||||
$table->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用户名'))
|
||||
->addColumn('password', 'string',array('limit' => 255,'comment'=>'用户密码'))
|
||||
->addColumn('email', 'string',array('limit' => 100, 'comment'=>'邮箱 登录'))
|
||||
->addColumn('creator_id', 'integer',['default' => 0, 'comment'=>'创建人ID'])
|
||||
->addColumn('department_id', 'integer',['default' => 0, 'comment'=>'部门ID'])
|
||||
->addColumn('status', 'boolean',array('limit' => 1,'default'=> 1,'comment'=>'用户状态 1 正常 2 禁用'))
|
||||
->addColumn('last_login_ip', 'string',array('limit' => 50,'default'=>0,'comment'=>'最后登录IP'))
|
||||
->addColumn('last_login_time', 'integer',array('default'=>0,'comment'=>'最后登录时间', 'signed' => false))
|
||||
->addColumn('created_at', 'integer', array('default'=>0,'comment'=>'创建时间', 'signed' => false ))
|
||||
->addColumn('updated_at', 'integer', array('default'=>0,'comment'=>'更新时间', 'signed' => false))
|
||||
->addColumn('deleted_at', 'integer', array('default'=>0,'comment'=>'删除状态,0未删除 >0 已删除', 'signed' => false))
|
||||
->create();
|
||||
}
|
||||
}
|
24
catch/permissions/database/seeds/UsersSeed.php
Normal file
24
catch/permissions/database/seeds/UsersSeed.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Seeder;
|
||||
|
||||
class UsersSeed extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run Method.
|
||||
*
|
||||
* Write your database seeder using this method.
|
||||
*
|
||||
* More information on writing seeders is available here:
|
||||
* http://docs.phinx.org/en/latest/seeding.html
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
return \catchAdmin\user\model\Users::create([
|
||||
'username' => 'admin',
|
||||
'password' => 'admin',
|
||||
'email' => 'admin@gmail.com',
|
||||
'creator_id' => 1,
|
||||
]);
|
||||
}
|
||||
}
|
86
catch/permissions/model/Users.php
Normal file
86
catch/permissions/model/Users.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions\model;
|
||||
|
||||
use catchAdmin\permissions\model\search\UserSearch;
|
||||
use catcher\base\CatchModel;
|
||||
|
||||
class Users extends CatchModel
|
||||
{
|
||||
use HasRolesTrait;
|
||||
use HasJobsTrait;
|
||||
use UserSearch;
|
||||
|
||||
protected $name = 'users';
|
||||
|
||||
protected $field = [
|
||||
'id', //
|
||||
'username', // 用户名
|
||||
'password', // 用户密码
|
||||
'email', // 邮箱 登录
|
||||
'creator_id', // 创建者ID
|
||||
'department_id', // 部门ID
|
||||
'status', // 用户状态 1 正常 2 禁用
|
||||
'last_login_ip', // 最后登录IP
|
||||
'last_login_time', // 最后登录时间
|
||||
'created_at', // 创建时间
|
||||
'updated_at', // 更新时间
|
||||
'deleted_at', // 删除状态,0未删除 >0 已删除
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
* set password
|
||||
*
|
||||
* @time 2019年12月07日
|
||||
* @param $value
|
||||
* @return false|string
|
||||
*/
|
||||
public function setPasswordAttr($value)
|
||||
{
|
||||
return password_hash($value, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户列表
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @param $search
|
||||
* @throws \think\db\exception\DbException
|
||||
* @return \think\Paginator
|
||||
*/
|
||||
public function getList($search): \think\Paginator
|
||||
{
|
||||
return $this->withoutField(['updated_at'], true)
|
||||
->catchSearch()
|
||||
->catchLeftJoin(Department::class, 'id', 'department_id', ['department_name'])
|
||||
->order($this->getTable() . '.id', 'desc')
|
||||
->paginate($search['limit'] ?? parent::LIMIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取权限
|
||||
*
|
||||
* @time 2019年12月12日
|
||||
* @param $uid
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return array
|
||||
*/
|
||||
public function getPermissionsBy($uid = 0): array
|
||||
{
|
||||
// 获取超级管理配置 超级管理员全部权限
|
||||
if ($uid == config('catch.permissions.super_admin_id')) {
|
||||
return Permissions::select()->column('id');
|
||||
}
|
||||
|
||||
$roles = $uid ? $this->findBy($uid)->getRoles() : $this->getRoles();
|
||||
|
||||
$permissionIds = [];
|
||||
foreach ($roles as $role) {
|
||||
$permissionIds = array_merge($permissionIds, $role->getPermissions()->column('id'));
|
||||
}
|
||||
|
||||
return array_unique($permissionIds);
|
||||
}
|
||||
}
|
25
catch/permissions/model/search/UserSearch.php
Normal file
25
catch/permissions/model/search/UserSearch.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions\model\search;
|
||||
|
||||
trait UserSearch
|
||||
{
|
||||
public function searchUsernameAttr($query, $value, $data)
|
||||
{
|
||||
return $query->whereLike('username', $value);
|
||||
}
|
||||
|
||||
public function searchEmailAttr($query, $value, $data)
|
||||
{
|
||||
return $query->whereLike('email', $value);
|
||||
}
|
||||
|
||||
public function searchStatusAttr($query, $value, $data)
|
||||
{
|
||||
return $query->where($this->aliasField('status'), $value);
|
||||
}
|
||||
|
||||
public function searchDepartmentIdAttr($query, $value, $data)
|
||||
{
|
||||
return $query->where($this->aliasField('department_id'), $value);
|
||||
}
|
||||
}
|
24
catch/permissions/request/CreateRequest.php
Normal file
24
catch/permissions/request/CreateRequest.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace catchAdmin\permission\request;
|
||||
|
||||
use catchAdmin\user\model\Users;
|
||||
use catcher\base\CatchRequest;
|
||||
|
||||
class CreateRequest extends CatchRequest
|
||||
{
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
// TODO: Implement rules() method.
|
||||
return [
|
||||
'username|用户名' => 'require|max:20',
|
||||
'password|密码' => 'require|min:5|max:12',
|
||||
'email|邮箱' => 'require|email|unique:'.Users::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function message(): array
|
||||
{
|
||||
// TODO: Implement message() method.
|
||||
}
|
||||
}
|
23
catch/permissions/request/UpdateRequest.php
Normal file
23
catch/permissions/request/UpdateRequest.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace catchAdmin\permission\request;
|
||||
|
||||
use catchAdmin\user\model\Users;
|
||||
use catcher\base\CatchRequest;
|
||||
|
||||
class UpdateRequest extends CatchRequest
|
||||
{
|
||||
protected function rules(): array
|
||||
{
|
||||
// TODO: Implement rules() method.
|
||||
return [
|
||||
'username|用户名' => 'require|max:20',
|
||||
'password|密码' => 'sometimes|min:5|max:12',
|
||||
'email|邮箱' => 'require|email|unique:'.Users::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function message(): array
|
||||
{
|
||||
// TODO: Implement message() method.
|
||||
}
|
||||
}
|
@@ -11,3 +11,11 @@ $router->resource('departments', '\catchAdmin\permissions\controller\Department'
|
||||
$router->resource('jobs', '\catchAdmin\permissions\controller\Job');
|
||||
|
||||
$router->get('jobs/all', '\catchAdmin\permissions\controller\Job@getAll');
|
||||
|
||||
// 用户
|
||||
$router->resource('users', '\catchAdmin\user\controller\User');
|
||||
// 切换状态
|
||||
$router->put('users/switch/status/<id>', '\catchAdmin\user\controller\User@switchStatus');
|
||||
$router->put('users/recover/<id>', '\catchAdmin\user\controller\User@recover');
|
||||
$router->get('users/get/roles', '\catchAdmin\user\controller\User@getRoles');
|
||||
$router->get('user/info', '\catchAdmin\user\controller\User@info');
|
Reference in New Issue
Block a user