Skip to content

Commit 717f131

Browse files
authored
Merge pull request #12 from liaodeity/8.x
8.3.0 增加Validator类用于表单认证
2 parents ced9384 + 4ff7051 commit 717f131

File tree

11 files changed

+518
-261
lines changed

11 files changed

+518
-261
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Release Notes
22

3-
## [Unreleased](https://github.com/liaodeity/laravel-admin-cms/compare/v8.2.4...8.x)
3+
## [Unreleased](https://github.com/liaodeity/laravel-admin-cms/compare/v8.2.5...8.x)
4+
5+
## [v8.2.5(2021-06-28)](https://github.com/liaodeity/laravel-admin-cms/compare/v8.2.4...v8.2.5)
6+
7+
### Added
8+
- 添加会员管理导出记录功能([#11](https://github.com/liaodeity/laravel-admin-cms/pull/11)
9+
10+
### Changed
11+
- 更新路由归类
12+
- 调整视图文件操作按钮判断
13+
- 添加导出询问确认
414

515
## [v8.2.4(2021-06-23)](https://github.com/liaodeity/laravel-admin-cms/compare/v8.2.3...v8.2.4)
616

app/Http/Controllers/Admin/UserMemberController.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use App\Models\User;
2323
use App\Models\User\UserMember;
2424
use App\Repositories\UserRepository;
25+
use App\Validators\User\UserMemberValidator;
26+
use App\Validators\User\UserValidator;
2527
use Illuminate\Http\Request;
2628
use Illuminate\Support\Facades\DB;
2729
use Illuminate\Support\Facades\Hash;
@@ -75,7 +77,7 @@ private function getData (Request $request, bool $export = false)
7577
{
7678
$limit = $request->input ('limit', 15);
7779
QueryWhere::defaultOrderBy ('users.id', 'DESC')->setRequest ($request->all ());
78-
$M = $this->repository->makeModel ()->select ('user_members.*', 'users.name','users.email', 'users.login_count', 'users.last_login_at',
80+
$M = $this->repository->makeModel ()->select ('user_members.*', 'users.name', 'users.email', 'users.login_count', 'users.last_login_at',
7981
'user_infos.real_name', 'user_infos.gender', 'user_infos.telephone', 'user_infos.address');
8082
$M->join ('user_members', 'users.id', '=', 'user_members.user_id');
8183
$M->leftJoin ('user_infos', 'user_infos.user_id', '=', 'users.id');
@@ -132,22 +134,15 @@ public function create ()
132134
*/
133135
public function store (Request $request)
134136
{
135-
$request->validate ([
136-
'User.name' => 'required',
137-
'User.password' => 'required',
138-
'UserMember.status' => 'required'
139-
], [], [
140-
'User.name' => '登录账号',
141-
'User.password' => '登录密码',
142-
'UserMember.status' => '状态'
143-
]);
144137
if (!check_admin_auth ($this->module_name . '_create')) {
145138
return auth_error_return ();
146139
}
147140
DB::beginTransaction ();
148141
$inputUser = $request->input ('User');
149142
$input = $this->formatRequestInput (__FUNCTION__, $inputUser);
150143
try {
144+
$this->repository->makeValidator ()->with ($request->input ('User'))->passes (UserValidator::RULE_CREATE);
145+
$this->repository->makeValidator (UserMemberValidator::class)->with ($request->input ('UserMember'))->passes (UserMemberValidator::RULE_CREATE);
151146
if (!User::isSuperAdmin ()) {
152147
throw new BusinessException('非超级管理员,无法操作');
153148
}
@@ -236,20 +231,14 @@ public function edit (UserMember $userMember)
236231
*/
237232
public function update (Request $request, UserMember $userMember)
238233
{
239-
$request->validate ([
240-
'User.name' => 'required',
241-
'UserMember.status' => 'required'
242-
], [], [
243-
'User.name' => '登录账号',
244-
'User.password' => '登录密码',
245-
'UserMember.status' => '状态'
246-
]);
247234
if (!check_admin_auth ($this->module_name . ' edit')) {
248235
return auth_error_return ();
249236
}
250237
$input = $request->input ('User');
251238
$input = $this->formatRequestInput (__FUNCTION__, $input);
252239
try {
240+
$this->repository->makeValidator ()->with ($request->input ('User'))->passes (UserValidator::RULE_UPDATE);
241+
$this->repository->makeValidator (UserMemberValidator::class)->with ($request->input ('UserMember'))->passes (UserMemberValidator::RULE_UPDATE);
253242
if (array_get ($input, 'password')) {
254243
$input['password'] = Hash::make ($input['password']);
255244
} else {

app/Repositories/BaseRepository.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,30 @@
1414
namespace App\Repositories;
1515

1616
use App\Exceptions\BusinessException;
17+
use App\Validators\BaseValidator;
1718
use Illuminate\Container\Container as Application;
19+
use Illuminate\Contracts\Container\BindingResolutionException;
1820
use Illuminate\Database\Eloquent\Model;
1921

2022
/**
2123
* 基础业务类
2224
* Class BaseRepository
2325
* @package App\Repositories
2426
*/
25-
class BaseRepository
27+
class BaseRepository implements InterfaceRepository
2628
{
2729
/**
2830
* @var Application
2931
*/
3032
protected $app;
33+
/**
34+
* @var Model
35+
*/
3136
protected $model;
37+
/**
38+
* @var BaseValidator
39+
*/
40+
protected $validator;
3241

3342
public function __construct (Application $app)
3443
{
@@ -40,6 +49,7 @@ public function __construct (Application $app)
4049
* add by gui
4150
* @return Model|mixed
4251
* @throws BusinessException
52+
* @throws \Illuminate\Contracts\Container\BindingResolutionException
4353
*/
4454
public function makeModel ()
4555
{
@@ -52,6 +62,46 @@ public function makeModel ()
5262
return $this->model = $model;
5363
}
5464

65+
/**
66+
* 模型类 add by gui
67+
*/
68+
public function model ()
69+
{
70+
71+
}
72+
73+
/**
74+
* add by gui
75+
* @param null $validator
76+
* @return Model|mixed
77+
* @throws BusinessException
78+
*/
79+
public function makeValidator ($validator = null): BaseValidator
80+
{
81+
if (is_null ($validator)) {
82+
$validator = $this->validator ();
83+
}
84+
try {
85+
$validator = $this->app->make ($validator);
86+
} catch (BindingResolutionException $e) {
87+
throw new BusinessException($e->getMessage ());
88+
}
89+
90+
if (!$validator instanceof BaseValidator) {
91+
throw new BusinessException("Class {$validator} must be an instance of App\\Validators\\LiaoValidator");
92+
}
93+
94+
return $this->validator = $validator;
95+
}
96+
97+
/**
98+
* 表单认证类 add by gui
99+
*/
100+
public function validator ()
101+
{
102+
103+
}
104+
55105
/**
56106
* add by gui
57107
* @param array $attributes
@@ -106,4 +156,14 @@ public function find ($id)
106156
{
107157
return $this->model->find ($id);
108158
}
159+
160+
/**
161+
* 是否允许删除 add by gui
162+
* @param $id
163+
* @return bool
164+
*/
165+
public function allowDelete ($id)
166+
{
167+
return true;
168+
}
109169
}

app/Repositories/InterfaceRepository.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| Author: 廖春贵 < liaodeity@gmail.com >
1111
|-----------------------------------------------------------------------------------------------------------
1212
*/
13+
1314
namespace App\Repositories;
1415

1516

@@ -19,5 +20,7 @@ interface InterfaceRepository
1920
{
2021
public function model ();
2122

22-
public function allowDelete (Model $model);
23+
public function validator ();
24+
25+
public function allowDelete ($id);
2326
}

app/Repositories/LogRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function model ()
2424
return Log::class;
2525
}
2626

27-
public function allowDelete (Model $model)
27+
public function allowDelete ($id)
2828
{
2929
return true;
3030
}

app/Repositories/UserMemberRepository.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
| Author: 廖春贵 < liaodeity@gmail.com >
1111
|-----------------------------------------------------------------------------------------------------------
1212
*/
13+
1314
namespace App\Repositories;
1415

1516

1617
use App\Models\User\UserMember;
18+
use App\Validators\User\UserMemberValidator;
1719

1820
class UserMemberRepository extends BaseRepository implements InterfaceRepository
1921
{
@@ -23,6 +25,11 @@ public function model ()
2325
return UserMember::class;
2426
}
2527

28+
public function validator ()
29+
{
30+
return UserMemberValidator::class;
31+
}
32+
2633
public function allowDelete ($id)
2734
{
2835
return true;

app/Repositories/UserRepository.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
| Author: 廖春贵 < liaodeity@gmail.com >
1111
|-----------------------------------------------------------------------------------------------------------
1212
*/
13+
1314
namespace App\Repositories;
1415

1516

1617
use App\Exceptions\BusinessException;
1718
use App\Models\Log;
1819
use App\Models\User;
1920
use App\Services\LoginService;
21+
use App\Validators\User\UserValidator;
2022
use Illuminate\Support\Facades\Hash;
2123

2224
class UserRepository extends BaseRepository implements InterfaceRepository
@@ -27,6 +29,11 @@ public function model ()
2729
return User::class;
2830
}
2931

32+
public function validator ()
33+
{
34+
return UserValidator::class;
35+
}
36+
3037
public function allowDelete ($id)
3138
{
3239
return true;

app/Validators/BaseValidator.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/*
3+
|-----------------------------------------------------------------------------------------------------------
4+
| laravel-admin-cms [ 简单高效的开发插件系统 ]
5+
|-----------------------------------------------------------------------------------------------------------
6+
| Licensed ( MIT )
7+
| ----------------------------------------------------------------------------------------------------------
8+
| Copyright (c) 2020-2021 https://gitee.com/liaodeiy/laravel-admin-cms All rights reserved.
9+
| ----------------------------------------------------------------------------------------------------------
10+
| Author: 廖春贵 < liaodeity@gmail.com >
11+
|-----------------------------------------------------------------------------------------------------------
12+
*/
13+
14+
namespace App\Validators;
15+
16+
17+
use App\Exceptions\BusinessException;
18+
use Illuminate\Support\Facades\Validator;
19+
20+
class BaseValidator
21+
{
22+
const RULE_CREATE = 'create';
23+
const RULE_UPDATE = 'update';
24+
const ROLE_SAVE = 'save';
25+
/**
26+
* @var Validator
27+
*/
28+
protected $validator;
29+
/**
30+
* 验证规则
31+
* @var array
32+
*/
33+
protected $rules = [];
34+
/**
35+
* 验证字段
36+
* @var array
37+
*/
38+
protected $attributes = [];
39+
/**
40+
* 验证信息
41+
* @var array
42+
*/
43+
protected $messages = [];
44+
protected $data = [];
45+
private $errors;
46+
47+
/**
48+
* BaseValidator constructor.
49+
*/
50+
public function __construct ()
51+
{
52+
$this->validator = new Validator();
53+
}
54+
55+
/**
56+
* 要验证的表单参数数组
57+
* @param array $data
58+
* @return $this
59+
*/
60+
public function with ($data = null)
61+
{
62+
if(!is_array ($data)){
63+
$data = [];
64+
}
65+
$this->data = $data;
66+
67+
return $this;
68+
}
69+
70+
/**
71+
* 进行验证 add by gui
72+
* @param null $action
73+
* @return bool
74+
* @throws BusinessException
75+
*/
76+
public function passes ($action = null)
77+
{
78+
$rules = array_get ($this->rules, $action, []);
79+
$messages = $this->messages;
80+
$attributes = $this->attributes;
81+
$validator = Validator::make ($this->data, $rules, $messages, $attributes);
82+
if ($validator->fails ()) {
83+
$this->errors = $validator->getMessageBag ();
84+
throw new BusinessException($validator->getMessageBag ()->first ());
85+
}
86+
87+
return true;
88+
}
89+
90+
/**
91+
* 获取验证规则 add by gui
92+
* @return array
93+
*/
94+
public function getRules ()
95+
{
96+
return $this->rules;
97+
}
98+
99+
/**
100+
* 设置验证规则 add by gui
101+
* @param $rules
102+
* @return BaseValidator
103+
*/
104+
public function setRules ($rules): BaseValidator
105+
{
106+
$this->rules = $rules;
107+
108+
return $this;
109+
}
110+
}

0 commit comments

Comments
 (0)