Skip to content

Commit e70977d

Browse files
committed
Choose model and validation rules. Removed roles() from User at it is already
provided by the Shinobi Trait. CDN changed naming for pace themes/templates, updated master to match.
1 parent 9327d99 commit e70977d

File tree

6 files changed

+61
-25
lines changed

6 files changed

+61
-25
lines changed

src/Config/watchtower.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@
2121
'site_title' => 'Watchtower',
2222

2323

24+
/*
25+
|--------------------------------------------------------------------------
26+
| Default model to use
27+
|--------------------------------------------------------------------------
28+
|
29+
| By default, watchtower uses its own internal User model. If you have a
30+
| User model you would rather use, provide the name here.
31+
|
32+
| To provide additional Validation rules to the default Update or Store
33+
| form request, place them under the rules heading. By default, the
34+
| rules for password, password confirmation and username are part
35+
| of the request already. But you can override them with yours.
36+
|
37+
*/
38+
'user' => [
39+
'model' => \Smarch\Watchtower\Models\User::class,
40+
'rules' => [
41+
'update' => [],
42+
'store' => [],
43+
],
44+
],
45+
46+
2447
/*
2548
|--------------------------------------------------------------------------
2649
| Default bootstrap theme

src/Controllers/UserController.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,25 @@
1919
class UserController extends Controller
2020
{
2121

22+
protected $model = \Smarch\Watchtower\Models\User::class;
23+
2224
/**
23-
* Set resource in constructor.
25+
* Set resource model in constructor.
2426
*/
25-
function __construct() {}
27+
function __construct() {
28+
$this->model = $this->getModel();
29+
}
30+
31+
32+
/**
33+
* Determine which model to use
34+
* @return Model Instance
35+
*/
36+
function getModel() {
37+
$model = config('watchtower.user.model', $this->model);
38+
return new $model;
39+
}
40+
2641

2742
/**
2843
* Display a listing of the resource.
@@ -34,11 +49,11 @@ public function index(Request $request)
3449
if ( Shinobi::can( config('watchtower.acl.user.index', false) ) ) {
3550
if ( $request->has('search_value') ) {
3651
$value = $request->get('search_value');
37-
$users = User::where('name', 'LIKE', '%'.$value.'%')
52+
$users = $this->model::where('name', 'LIKE', '%'.$value.'%')
3853
->orderBy('name')->paginate( config('watchtower.pagination.users', 15) );
3954
session()->flash('search_value', $value);
4055
} else {
41-
$users = User::orderBy('name')->paginate( config('watchtower.pagination.users', 15) );
56+
$users = $this->model::orderBy('name')->paginate( config('watchtower.pagination.users', 15) );
4257
session()->forget('search_value');
4358
}
4459

@@ -48,6 +63,7 @@ public function index(Request $request)
4863
return view( config('watchtower.views.layouts.unauthorized'), [ 'message' => 'view user list' ]);
4964
}
5065

66+
5167
/**
5268
* Show the form for creating a new resource.
5369
*
@@ -74,7 +90,7 @@ public function store(UserStoreRequest $request)
7490
$message = " You are not permitted to create users.";
7591

7692
if ( Shinobi::can ( config('watchtower.acl.user.create', false) ) ) {
77-
User::create($request->all());
93+
$this->model::create($request->all());
7894
$level = "success";
7995
$message = "<i class='fa fa-check-square-o fa-1x'></i> Success! User created.";
8096
}
@@ -92,7 +108,7 @@ public function store(UserStoreRequest $request)
92108
public function show($id)
93109
{
94110
if ( Shinobi::canAtLeast( [ config('watchtower.acl.user.show', false), config('watchtower.acl.user.edit', false) ] ) ) {
95-
$resource = User::findOrFail($id);
111+
$resource = $this->model::findOrFail($id);
96112
$show = "1";
97113
return view( config('watchtower.views.users.show'), compact('resource','show') );
98114
}
@@ -109,7 +125,7 @@ public function show($id)
109125
public function edit($id)
110126
{
111127
if ( Shinobi::canAtLeast( [ config('watchtower.acl.user.edit', false), config('watchtower.acl.user.show', false) ] ) ) {
112-
$resource = User::findOrFail($id);
128+
$resource = $this->model::findOrFail($id);
113129
$show = "0";
114130
return view( config('watchtower.views.users.edit'), compact('resource','show') );
115131
}
@@ -129,7 +145,7 @@ public function update($id, UserUpdateRequest $request)
129145
$message = " You are not permitted to update users.";
130146

131147
if ( Shinobi::can ( config('watchtower.acl.user.edit', false) ) ) {
132-
$user = User::findOrFail($id);
148+
$user = $this->model::findOrFail($id);
133149
if ($request->get('password') == '') {
134150
$user->update( $request->except('password') );
135151
} else {
@@ -155,7 +171,7 @@ public function destroy($id)
155171
$message = " You are not permitted to destroy user objects";
156172

157173
if ( Shinobi::can ( config('watchtower.acl.user.destroy', false) ) ) {
158-
User::destroy($id);
174+
$this->model::destroy($id);
159175
$level = "warning";
160176
$message = "<i class='fa fa-check-square-o fa-1x'></i> Success! User deleted.";
161177
}
@@ -173,7 +189,7 @@ public function destroy($id)
173189
public function editUserRoles($id)
174190
{
175191
if ( Shinobi::can( config('watchtower.acl.user.role', false) ) ) {
176-
$user = User::findOrFail($id);
192+
$user = $this->model::findOrFail($id);
177193

178194
$roles = $user->roles;
179195

@@ -199,7 +215,7 @@ public function updateUserRoles($id, Request $request)
199215
$message = " You are not permitted to update user roles.";
200216

201217
if ( Shinobi::can ( config('watchtower.acl.user.role', false) ) ) {
202-
$user = User::findOrFail($id);
218+
$user = $this->model::findOrFail($id);
203219
if ($request->has('ids')) {
204220
$user->roles()->sync( $request->get('ids') );
205221
} else {
@@ -221,7 +237,7 @@ public function showUserMatrix()
221237
{
222238
if ( Shinobi::can( config('watchtower.acl.user.viewmatrix', false) ) ) {
223239
$roles = Role::all();
224-
$users = User::orderBy('name')->get();
240+
$users = $this->model::orderBy('name')->get();
225241
$us = DB::table('role_user')->select('role_id as r_id','user_id as u_id')->get();
226242

227243
$pivot = [];

src/Models/User.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,4 @@ class User extends Model
2828
* @var array
2929
*/
3030
protected $hidden = ['password', 'remember_token'];
31-
32-
/**
33-
* The roles that have the permissions.
34-
*/
35-
public function roles()
36-
{
37-
return $this->belongsToMany('Smarch\Watchtower\Models\Role');
38-
}
3931
}

src/Requests/UserStoreRequest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ public function authorize()
3838
public function rules()
3939
{
4040

41-
return [
41+
$rules = array_merge([
4242
'name' => 'required|max:255|unique:users',
4343
'email' => 'required|email|unique:users',
4444
'password' => 'required|confirmed|min:6',
45-
];
45+
], config('watchtower.user.rules.store') );
46+
47+
return $rules;
48+
4649
}
4750
}

src/Requests/UserUpdateRequest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ public function authorize()
3838
public function rules()
3939
{
4040

41-
return [
41+
$rules = array_merge([
4242
'name' => 'required|max:255|unique:users,name,'.$this->user,
4343
'email' => 'required|email|unique:users,email,'.$this->user,
4444
'password' => 'confirmed|min:6',
45-
];
45+
], config('watchtower.user.rules.update') );
46+
47+
return $rules;
4648

4749
}
4850

src/Views/layouts/master.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
2727

2828
<!-- Pace loader -->
29-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/black/pace-theme-big-counter.min.css">
29+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/silver/pace-theme-center-circle.min.css">
3030

3131
<!-- Sweetalert (modal) styles -->
3232
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">

0 commit comments

Comments
 (0)