refactor: 重构权限服务类
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
|
||||
namespace app\common\model\system;
|
||||
use Psr\SimpleCache\InvalidArgumentException;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use think\Model;
|
||||
use app\common\library\ParseData;
|
||||
use think\model\concern\SoftDelete;
|
||||
@@ -81,6 +84,9 @@ class User extends Model
|
||||
* @param array $data
|
||||
* @return string
|
||||
* @throws InvalidArgumentException
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function getAvatarAttr(string $value, array $data): string
|
||||
{
|
||||
|
||||
@@ -1,43 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\common\validate\system;
|
||||
|
||||
use app\admin\service\AuthService;
|
||||
use think\Validate;
|
||||
use app\common\model\system\Admin as AdminModel;
|
||||
|
||||
class Admin extends Validate
|
||||
{
|
||||
/**
|
||||
* 定义验证规则
|
||||
* 格式:'字段名' => ['规则1','规则2'...]
|
||||
* 格式:'字段名' => ['规则1','规则2'...]
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rule = [
|
||||
'name' => 'require|min:2|max:12|chsAlphaNum',
|
||||
'pwd|密码' => 'require|min:6|max:64',
|
||||
protected $rule = [
|
||||
'name' => 'require|min:2|max:12|chsAlphaNum',
|
||||
'pwd|密码' => 'require|min:6|max:64',
|
||||
'group_id' => 'require|checkGroup',
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 定义错误信息
|
||||
* 格式:'字段名.规则名' => '错误信息'
|
||||
* 格式:'字段名.规则名' => '错误信息'
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $message = [
|
||||
'name.require' => '用户名不能为空',
|
||||
'name.min' => '用户名不能少于2个字符',
|
||||
'name.max' => '用户名不能超过12个字符',
|
||||
'name.filters' => '用户名包含禁止注册字符',
|
||||
'name.chsAlphaNum' => '用户名只能是汉字、字母和数字',
|
||||
'pwd.require' => '密码不能为空',
|
||||
'pwd.min' => '密码不能少于6个字符',
|
||||
'pwd.max' => '密码不能超过64个字符',
|
||||
protected $message = [
|
||||
'name.require' => '用户名不能为空',
|
||||
'name.min' => '用户名不能少于2个字符',
|
||||
'name.max' => '用户名不能超过12个字符',
|
||||
'name.filters' => '用户名包含禁止注册字符',
|
||||
'name.chsAlphaNum' => '用户名只能是汉字、字母和数字',
|
||||
'pwd.require' => '密码不能为空',
|
||||
'pwd.min' => '密码不能少于6个字符',
|
||||
'pwd.max' => '密码不能超过64个字符',
|
||||
'group_id.require' => '请选择用户组',
|
||||
'group_id.checkGroup' => '无权限操作',
|
||||
];
|
||||
|
||||
// 测试验证场景
|
||||
protected $scene = [
|
||||
'edit' => ['name']
|
||||
'add' => ['name', 'pwd', 'group_id'],
|
||||
'edit' => ['name', 'group_id'],
|
||||
'login' => ['name', 'pwd'],
|
||||
];
|
||||
|
||||
/**
|
||||
* 验证用户组权限
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkGroup($value): bool
|
||||
{
|
||||
$id = request()->get('id', 0);
|
||||
$result = AdminModel::where('id', $id)->findOrEmpty()->toArray();
|
||||
if (empty($result)) {
|
||||
return true;
|
||||
}
|
||||
$group_id = !empty($value) ? $value . ',' . $result['group_id'] : $result['group_id'];
|
||||
$group_id = array_unique(explode(',', $group_id));
|
||||
$authService = AuthService::instance();
|
||||
if (!$authService->checkRulesForGroup($group_id)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\common\validate\system;
|
||||
|
||||
use app\admin\service\AuthService;
|
||||
use think\Validate;
|
||||
use app\common\model\system\AdminGroup as AdminGroupModel;
|
||||
|
||||
@@ -10,23 +11,31 @@ class AdminGroup extends Validate
|
||||
{
|
||||
/**
|
||||
* 定义验证规则
|
||||
* 格式:'字段名' => ['规则1','规则2'...]
|
||||
* 格式:'字段名' => ['规则1','规则2'...]
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rule = [
|
||||
'pid' => 'notEqId',
|
||||
*/
|
||||
protected $rule = [
|
||||
'id' => 'require|checkGroup',
|
||||
'pid' => 'notEqId',
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 定义错误信息
|
||||
* 格式:'字段名.规则名' => '错误信息'
|
||||
* 格式:'字段名.规则名' => '错误信息'
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $message = [
|
||||
'pid.notEqId' => '选择上级分类错误!',
|
||||
*/
|
||||
protected $message = [
|
||||
'pid.notEqId' => '选择上级分类错误!',
|
||||
'id.require' => '请选择用户组',
|
||||
'id.checkGroup' => '无权限操作',
|
||||
];
|
||||
|
||||
protected $scene = [
|
||||
'add' => ['pid'],
|
||||
'edit' => ['id', 'pid'],
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -36,7 +45,7 @@ class AdminGroup extends Validate
|
||||
* @param $data
|
||||
* @return bool
|
||||
*/
|
||||
protected function notEqId($value, $rules ,$data): bool
|
||||
protected function notEqId($value, $rules, $data): bool
|
||||
{
|
||||
if ($value == $data['id']) {
|
||||
return false;
|
||||
@@ -51,4 +60,22 @@ class AdminGroup extends Validate
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证用户组权限
|
||||
* @param $value
|
||||
* @param $rule
|
||||
* @param $data
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkGroup($value, $rule, $data): bool
|
||||
{
|
||||
$authService = AuthService::instance();
|
||||
$value = explode(',', $value);
|
||||
if (!$authService->checkRulesForGroup($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\common\validate\system;
|
||||
|
||||
@@ -12,20 +12,20 @@ class AdminRules extends Validate
|
||||
* 格式:'字段名' => ['规则1','规则2'...]
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
*/
|
||||
protected $rule = [
|
||||
'pid' => 'notEqId',
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 定义错误信息
|
||||
* 格式:'字段名.规则名' => '错误信息'
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
*/
|
||||
protected $message = [
|
||||
'pid.notEqId' => '选择上级分类错误!',
|
||||
'pid.notEqId' => '选择上级分类错误!',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -43,4 +43,4 @@ class AdminRules extends Validate
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,20 +56,30 @@ class User extends Validate
|
||||
/**
|
||||
* 自定义验证规则
|
||||
* @param $value
|
||||
* @return string|bool
|
||||
* @return bool
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
protected function checkName($value): string|bool
|
||||
protected function checkName($value): bool
|
||||
{
|
||||
$notAllow = saenv('user_reg_notallow');
|
||||
$notAllow = explode(',', $notAllow);
|
||||
if (in_array($value, $notAllow)) {
|
||||
return '用户名不合法!';
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function sceneAdd(): User
|
||||
{
|
||||
return $this->only(['nickname', 'pwd', 'email', 'mobile']);
|
||||
}
|
||||
|
||||
public function sceneEdit(): User
|
||||
{
|
||||
return $this->only(['nickname', 'email']);
|
||||
}
|
||||
|
||||
public function sceneRegister(): User
|
||||
{
|
||||
return $this->only(['nickname', 'pwd']);
|
||||
@@ -84,4 +94,4 @@ class User extends Validate
|
||||
{
|
||||
return $this->only(['mobile', 'captcha']);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user