Skip to content

Commit 72b17b4

Browse files
committed
Navigation and User Roles - validate role middleware
1 parent 501a5ee commit 72b17b4

File tree

14 files changed

+207
-114
lines changed

14 files changed

+207
-114
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ A Laravel CMS Starter project with AdminLTE theme and core features.
1010

1111
### What is New?
1212
- [Impersonation](https://github.com/bpocallaghan/impersonate) When logged in, [go here](http://bpocallaghan.co.za/admin/settings/admin/users) and click on the 'impersonate user' action.
13+
- Roles (Assign a role to user and assign role to navigation. Can only see navigation for given role)
1314

1415
## Features / What it includes
1516
- Admin LTE admin theme

app/Http/Controllers/Admin/Settings/Admin/NavigationController.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Http\Requests;
66
use App\Models\Notification;
7+
use App\Models\Role;
78
use Illuminate\Http\Request;
89
use App\Models\NavigationAdmin;
910
use Yajra\Datatables\Datatables;
@@ -30,8 +31,12 @@ public function index()
3031
*/
3132
public function create()
3233
{
34+
$roles = Role::getAllLists();
35+
$parents = NavigationAdmin::getAllLists();
36+
3337
return $this->view('settings.admin.navigation.add_edit')
34-
->with('parents', NavigationAdmin::getAllLists());
38+
->with('roles', $roles)
39+
->with('parents', $parents);
3540
}
3641

3742
/**
@@ -44,14 +49,28 @@ public function store(Request $request)
4449
{
4550
$this->validate($request, NavigationAdmin::$rules, NavigationAdmin::$messages);
4651

47-
$inputs = $request->all();
52+
$inputs = $request->only([
53+
'icon',
54+
'title',
55+
'slug',
56+
'description',
57+
'help_index_title',
58+
'help_index_content',
59+
'help_create_title',
60+
'help_create_content',
61+
'help_edit_title',
62+
'help_edit_content',
63+
'parent_id',
64+
'url_parent_id'
65+
]);
4866
$inputs['is_hidden'] = boolval($request->has('is_hidden'));
4967
$inputs['url_parent_id'] = ($inputs['url_parent_id'] == 0 ? $inputs['parent_id'] : $inputs['url_parent_id']);
5068

5169
$row = $this->createEntry(NavigationAdmin::class, $inputs);
5270

5371
if ($row) {
5472
$row->updateUrl()->save();
73+
$row->roles()->attach(input('roles'));
5574
}
5675

5776
return redirect_to_resource();
@@ -78,10 +97,12 @@ public function show($id)
7897
*/
7998
public function edit($id)
8099
{
100+
$roles = Role::getAllLists();
81101
$navigation = NavigationAdmin::findOrFail($id);
82102

83103
return $this->view('settings.admin.navigation.add_edit')
84104
->with('item', $navigation)
105+
->with('roles', $roles)
85106
->with('parents', NavigationAdmin::getAllLists());
86107
}
87108

@@ -96,12 +117,26 @@ public function update($id, Request $request)
96117
{
97118
$this->validate($request, NavigationAdmin::$rules, NavigationAdmin::$messages);
98119

99-
$inputs = $request->all();
120+
$inputs = $request->only([
121+
'icon',
122+
'title',
123+
'slug',
124+
'description',
125+
'help_index_title',
126+
'help_index_content',
127+
'help_create_title',
128+
'help_create_content',
129+
'help_edit_title',
130+
'help_edit_content',
131+
'parent_id',
132+
'url_parent_id'
133+
]);
100134
$inputs['is_hidden'] = boolval($request->has('is_hidden'));
101135

102136
$navigation = NavigationAdmin::findOrFail($id);
103137
$navigation = $this->updateEntry($navigation, $inputs);
104138
$navigation->updateUrl()->save();
139+
$navigation->roles()->sync(input('roles'));
105140

106141
return redirect_to_resource();
107142
}
@@ -134,7 +169,8 @@ public function getTableData()
134169
})->editColumn('is_hidden', function ($row) {
135170
return ($row->is_hidden == 1 ? 'Yes' : '');
136171
})->addColumn('action', function ($row) {
137-
return action_row($this->selectedNavigation->url, $row->id, $row->title, ['edit', 'delete']);
172+
return action_row($this->selectedNavigation->url, $row->id, $row->title,
173+
['edit', 'delete']);
138174
})->make(true);
139175
}
140176

@@ -144,6 +180,6 @@ public function getTableData()
144180
*/
145181
protected function getTableRows()
146182
{
147-
return NavigationAdmin::with('parent')->get();
183+
return NavigationAdmin::with('parent', 'roles')->get();
148184
}
149185
}

app/Http/Kernel.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ class Kernel extends HttpKernel
5050
* @var array
5151
*/
5252
protected $routeMiddleware = [
53-
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
53+
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
5454
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
55-
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
56-
'can' => \Illuminate\Auth\Middleware\Authorize::class,
57-
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
58-
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
55+
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
56+
'can' => \Illuminate\Auth\Middleware\Authorize::class,
57+
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
58+
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
5959

60-
'auth.admin' => \App\Http\Middleware\AuthenticateAdmin::class,
60+
'role' => \App\Http\Middleware\ValidateRole::class,
61+
'auth.admin' => \App\Http\Middleware\AuthenticateAdmin::class,
6162
];
6263
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use App\Models\NavigationAdmin;
6+
use Auth;
7+
use Closure;
8+
9+
class ValidateRole
10+
{
11+
/**
12+
* Handle an incoming request.
13+
*
14+
* @param \Illuminate\Http\Request $request
15+
* @param \Closure $next
16+
*
17+
* @param $selectedNavigationId
18+
* @return mixed
19+
* @internal param null $guard
20+
*/
21+
public function handle($request, Closure $next, $selectedNavigationId)
22+
{
23+
$selectedNavigation = NavigationAdmin::findOrFail($selectedNavigationId);
24+
25+
// check if user role is in navigation role
26+
$userRoles = user()->getRolesList();
27+
$navValid = $selectedNavigation->roles()->whereIn('roles.id', $userRoles)->first();
28+
29+
if (!$navValid) {
30+
return redirect(user()->roles()->first()->slug);
31+
}
32+
33+
return $next($request);
34+
}
35+
}

app/Models/NavigationAdmin.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,6 @@
66

77
class NavigationAdmin extends TitanAdminNavigation
88
{
9-
/**
10-
* Remove the ending '/'
11-
* @return string
12-
*/
13-
public function getUrlAttribute()
14-
{
15-
return rtrim($this->attributes['url'], '/');
16-
}
17-
18-
/**
19-
* Get the roles
20-
* @return \Eloquent
21-
*/
22-
public function roles()
23-
{
24-
return $this->belongsToMany(Role::class, 'navigation_admin_role')->withTimestamps();
25-
}
26-
279
/**
2810
* Get all the rows as an array (ready for dropdowns)
2911
*

app/Models/Role.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ class Role extends TitanCMSModel
99
{
1010
use SoftDeletes;
1111

12-
public static $BASIC = 'basic'; // 1
12+
// basic website
13+
public static $BASIC = 'website'; // 1
1314

14-
// Admin - Developer / access to everything
15+
// basic admin
1516
public static $ADMIN = 'admin'; // 2
1617

17-
// Super Admin - Developer / access to everything
18-
public static $DEVELOPER = 'developer'; // 3
18+
// admin + analytics
19+
public static $ANALYTICS = 'analytics'; // 3
20+
21+
public static $ADMIN_SUPER = 'admin_super'; // 4
22+
23+
public static $DEVELOPER = 'developer'; // 5
1924

2025
protected $table = 'roles';
2126

@@ -33,13 +38,18 @@ public function getIconTitleAttribute()
3338
return '<i class="fa fa-' . $this->attributes['icon'] . '"</i> ' . $this->attributes['title'];
3439
}
3540

41+
public function getTitleSlugAttribute()
42+
{
43+
return $this->attributes['title'] . ' (' . $this->attributes['slug'] . ')';
44+
}
45+
3646
/**
3747
* Get all the rows as an array (ready for dropdowns)
3848
*
3949
* @return array
4050
*/
4151
public static function getAllLists()
4252
{
43-
return self::orderBy('level')->get()->pluck('title', 'id')->toArray();
53+
return self::orderBy('title')->get()->pluck('title', 'id')->toArray();
4454
}
4555
}

database/migrations/2014_10_12_000000_create_users_table.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public function up()
2222
$table->string('image')->nullable();
2323
$table->string('gender', 10)->nullable();
2424
$table->date('born_at')->nullable();
25-
$table->integer('security_level')->default(1);
2625
$table->string('password', 60)->nullable();
2726
$table->timestamp('password_updated_at')->nullable();
2827
$table->rememberToken();

database/migrations/2017_06_19_122044_create_roles_table.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public function up()
1919
$table->string('slug', 50);
2020
$table->string('keyword', 50)->unique();
2121
$table->string('summary')->nullable();
22-
$table->integer('level');
2322
$table->timestamps();
2423
$table->softDeletes();
2524
$table->integer('created_by')->unsigned();

database/seeds/NavigationAdminTableSeeder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class NavigationAdminTableSeeder extends Seeder
88
public function run()
99
{
1010
NavigationAdmin::truncate();
11+
DB::delete('TRUNCATE navigation_admin_role');
1112

1213
$csvPath = database_path() . '/seeds/csv/' . 'navigation_admin.csv';
1314
$items = csv_to_array($csvPath);
@@ -34,7 +35,8 @@ public function run()
3435
'updated_by' => 1,
3536
]);
3637

37-
$row->roles()->attach(2);
38+
$roles = explode(',', $item['roles']);
39+
$row->roles()->attach($roles);
3840
}
3941
}
4042
}

database/seeds/RoleTableSeeder.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,42 @@ public function run()
99
{
1010
Role::truncate();
1111

12+
// basic website only role
1213
Role::create([
1314
'icon' => 'user',
14-
'title' => 'Basic',
15+
'title' => 'Website',
1516
'slug' => '/',
16-
'keyword' => 'basic',
17-
'level' => '1',
17+
'keyword' => 'website',
1818
]);
1919

20+
// basic admin role
2021
Role::create([
2122
'icon' => 'user-secret',
22-
'title' => 'Admin',
23-
'slug' => 'admin',
23+
'title' => 'Basic Admin',
24+
'slug' => '/admin',
2425
'keyword' => 'admin',
25-
'level' => '10',
26+
]);
27+
28+
// admin and analytics
29+
Role::create([
30+
'icon' => 'user-circle',
31+
'title' => 'Analytics',
32+
'slug' => '/admin',
33+
'keyword' => 'analytics',
34+
]);
35+
36+
Role::create([
37+
'icon' => 'user-secret',
38+
'title' => 'Admin',
39+
'slug' => '/admin',
40+
'keyword' => 'admin_super',
2641
]);
2742

2843
Role::create([
2944
'icon' => 'universal-access',
3045
'title' => 'Developer',
31-
'slug' => 'admin',
46+
'slug' => '/admin',
3247
'keyword' => 'developer',
33-
'level' => '20',
3448
]);
3549
}
3650
}

0 commit comments

Comments
 (0)