作者 Karson

新增管理员添加编辑唯一性验证

新增规则管理添加编辑唯一性验证
修复最新版nice-validator导致提示换行的BUG
修复php think menu -c all-controller无法重建菜单的BUG
... ... @@ -249,7 +249,7 @@ class Crud extends Command
//根据表名匹配对应的Fontawesome图标
$iconPath = ROOT_PATH . str_replace('/', DS, '/public/assets/libs/font-awesome/less/variables.less');
$iconName = is_file($iconPath) && stripos(file_get_contents($iconPath), '@fa-var-' . $table . ':') ? $table : 'fa fa-circle-o';
$iconName = is_file($iconPath) && stripos(file_get_contents($iconPath), '@fa-var-' . $table . ':') ? 'fa fa-' . $table : 'fa fa-circle-o';
//控制器默认以表名进行处理,以下划线进行分隔,如果需要自定义则需要传入controller,格式为目录层级
$controller = str_replace('_', '', $controller);
... ...
... ... @@ -86,7 +86,7 @@ class Menu extends Command
}
else
{
$this->model->destroy([]);
$this->model->where('id', '>', 0)->delete();
$controllerDir = $adminPath . 'controller' . DS;
// 扫描新的节点信息并导入
$treelist = $this->import($this->scandir($controllerDir));
... ... @@ -215,7 +215,7 @@ class Menu extends Command
//导入中文语言包
\think\Lang::load(dirname(__DIR__) . DS . 'lang/zh-cn.php');
//先定入菜单的数据
$pid = 0;
foreach ($controllerArr as $k => $v)
... ... @@ -262,7 +262,7 @@ class Menu extends Command
}
//过滤掉其它字符
$comment = preg_replace(array('/^\/\*\*(.*)[\n\r\t]/u', '/[\s]+\*\//u', '/\*\s@(.*)/u', '/[\s|\*]+/u'), '', $comment);
$title = $comment ? $comment : ucfirst($n->name);
//获取主键,作为AuthRule更新依据
... ... @@ -274,13 +274,13 @@ class Menu extends Command
}
//获取主键
protected function getAuthRulePK($name) {
protected function getAuthRulePK($name)
{
if (!empty($name))
{
return $this->model
->where('name', $name)
->value('id');
}
}
... ...
... ... @@ -28,7 +28,7 @@ class Admin extends Backend
$this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
$this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin() ? true : false);
$groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
$groupIds = $this->auth->getGroupIds();
Tree::instance()->init($groupList);
... ... @@ -117,8 +117,11 @@ class Admin extends Backend
$params['salt'] = Random::alnum();
$params['password'] = md5(md5($params['password']) . $params['salt']);
$params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
$admin = $this->model->create($params);
$result = $this->model->validate('Admin.add')->save($params);
if ($result === false)
{
$this->error($this->model->getError());
}
$group = $this->request->post("group/a");
//过滤不允许的组别,避免越权
... ... @@ -126,7 +129,7 @@ class Admin extends Backend
$dataset = [];
foreach ($group as $value)
{
$dataset[] = ['uid' => $admin->id, 'group_id' => $value];
$dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
}
model('AuthGroupAccess')->saveAll($dataset);
$this->success();
... ... @@ -158,7 +161,17 @@ class Admin extends Backend
{
unset($params['password'], $params['salt']);
}
$row->save($params);
//这里需要针对username和email做唯一验证
$adminValidate = \think\Loader::validate('Admin');
$adminValidate->rule([
'username' => 'require|max:50|unique:admin,username,' . $row->id,
'email' => 'require|email|unique:admin,email,' . $row->id
]);
$result = $row->validate('Admin.edit')->save($params);
if ($result === false)
{
$this->error($row->getError());
}
// 先移除所有权限
model('AuthGroupAccess')->where('uid', $row->id)->delete();
... ...
... ... @@ -74,7 +74,11 @@ class Rule extends Backend
{
$this->error(__('The non-menu rule must have parent'));
}
$this->model->create($params);
$result = $this->model->validate()->save($params);
if ($result === FALSE)
{
$this->error($this->model->getError());
}
Cache::rm('__menu__');
$this->success();
}
... ... @@ -100,7 +104,16 @@ class Rule extends Backend
{
$this->error(__('The non-menu rule must have parent'));
}
$row->save($params);
//这里需要针对name做唯一验证
$ruleValidate = \think\Loader::validate('AuthRule');
$ruleValidate->rule([
'name' => 'require|format|unique:AuthRule,name,' . $row->id,
]);
$result = $row->validate()->save($params);
if ($result === FALSE)
{
$this->error($row->getError());
}
Cache::rm('__menu__');
$this->success();
}
... ...
<?php
return [
'Toggle all' => '显示全部',
'Condition' => '规则条件',
'Remark' => '备注',
'Icon' => '图标',
'Alert' => '警告',
'Name' => '规则URL',
'Controller/Action' => '控制器名/方法名',
'Ismenu' => '菜单',
'Search icon' => '搜索图标',
'The non-menu rule must have parent' => '非菜单规则节点必须有父级',
'If not necessary, use the command line to build rule' => '非必要情况下请直接使用命令行php think menu来生成',
'Toggle all' => '显示全部',
'Condition' => '规则条件',
'Remark' => '备注',
'Icon' => '图标',
'Alert' => '警告',
'Name' => '规则URL',
'Controller/Action' => '控制器名/方法名',
'Ismenu' => '菜单',
'Search icon' => '搜索图标',
'The non-menu rule must have parent' => '非菜单规则节点必须有父级',
'If not necessary, use the command line to build rule' => '非必要情况下请直接使用命令行php think menu来生成',
'Name only supports letters, numbers, underscore and pipe' => 'URL规则只能是小写字母、数字、下划线和|组成',
];
... ...
<?php
namespace app\admin\validate;
use think\Validate;
class Admin extends Validate
{
/**
* 验证规则
*/
protected $rule = [
'username' => 'require|max:50|unique:admin',
'nickname' => 'require',
'password' => 'require',
'email' => 'require|email|unique:admin,email',
];
/**
* 提示消息
*/
protected $message = [
];
/**
* 字段描述
*/
protected $field = [
];
/**
* 验证场景
*/
protected $scene = [
'add' => ['username', 'email', 'nickname', 'password'],
'edit' => ['username', 'email', 'nickname'],
];
public function __construct(array $rules = [], $message = [], $field = [])
{
$this->field = [
'username' => __('Username'),
'nickname' => __('Nickname'),
'password' => __('Password'),
'email' => __('Email'),
];
parent::__construct($rules, $message, $field);
}
}
... ...
<?php
namespace app\admin\validate;
use think\Validate;
class AuthRule extends Validate
{
/**
* 正则
*/
protected $regex = ['format' => '[a-z0-9_\|]+'];
/**
* 验证规则
*/
protected $rule = [
'name' => 'require|format|unique:AuthRule',
'title' => 'require',
];
/**
* 提示消息
*/
protected $message = [
'name.format' => 'URL规则只能是小写字母、数字、下划线和|组成'
];
/**
* 字段描述
*/
protected $field = [
];
/**
* 验证场景
*/
protected $scene = [
];
public function __construct(array $rules = [], $message = [], $field = [])
{
$this->field = [
'name' => __('Name'),
'title' => __('Title'),
];
$this->message['name.format'] = __('Name only supports letters, numbers, underscore and pipe');
parent::__construct($rules, $message, $field);
}
}
... ...
... ... @@ -449,6 +449,15 @@ form.form-horizontal .control-label {
.fixed-table-container {
border: none!important;
}
/*修复nice-validator新版下的一处BUG*/
.nice-validator input,
.nice-validator select,
.nice-validator textarea,
.nice-validator [contenteditable] {
display: inline-block;
*display: inline;
*zoom: 1;
}
/*修复nice-validator和summernote的编辑框冲突*/
.nice-validator .note-editor .note-editing-area .note-editable {
display: inherit;
... ...
... ... @@ -476,6 +476,15 @@ form.form-horizontal .control-label {
border:none!important;
}
/*修复nice-validator新版下的一处BUG*/
.nice-validator {
input, select, textarea, [contenteditable]{
display: inline-block;
*display: inline;
*zoom: 1;
}
}
/*修复nice-validator和summernote的编辑框冲突*/
.nice-validator .note-editor .note-editing-area .note-editable{
display:inherit;
... ...