Skip to content

Commit b4c7212

Browse files
committed
Admin Roles - update the roles and roles index
1 parent 86a8967 commit b4c7212

File tree

7 files changed

+50
-25
lines changed

7 files changed

+50
-25
lines changed

app/Events/UserRegistered.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(User $user, $token = null)
2525
// if token - add admin role and mark as claimed
2626
$userInvite = UserInvite::whereToken($token)->whereNull('claimed_at')->first();
2727
if ($userInvite) {
28-
$roles[] = Role::$ADMIN;
28+
$roles[] = Role::$BASE_ADMIN;
2929
// set invite claimed
3030
$user->update(['gender' => 'male']);
3131
$userInvite->update(['claimed_at' => Carbon::now()]);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function index()
2323
{
2424
save_resource_url();
2525

26-
$items = User::with('roles')->whereRole(Role::$ADMIN)->get();
26+
$items = User::with('roles')->whereRole(Role::$BASE_ADMIN)->get();
2727

2828
return $this->view('settings.administrators.index', compact('items'));
2929
}

app/Http/Middleware/AuthenticateAdmin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AuthenticateAdmin
1818
public function handle($request, Closure $next, $guard = null)
1919
{
2020
// not logged in as an admin - logout and go home
21-
if (!user()->isAdmin()) {
21+
if (!user()->isBaseAdmin()) {
2222
\Auth::logout();
2323

2424
return redirect()->guest('/');

app/Models/Role.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ class Role extends TitanCMSModel
1212
// basic website
1313
public static $WEBSITE = 'website'; // 1
1414

15-
// basic admin
16-
public static $ADMIN = 'admin'; // 2
15+
// base user
16+
public static $USER = 'user'; // 2
1717

18-
// admin + analytics
19-
public static $ANALYTICS = 'analytics'; // 3
18+
// basic admin
19+
public static $BASE_ADMIN = 'base_admin'; // 3
2020

21-
public static $ADMIN_SUPER = 'admin_super'; // 4
21+
public static $ADMIN_SUPER = 'admin'; // 4
2222

2323
public static $DEVELOPER = 'developer'; // 5
2424

25+
// base admin + analytics only
26+
public static $ANALYTICS = 'analytics'; // 6
27+
2528
protected $table = 'roles';
2629

2730
protected $guarded = ['id'];

app/Models/Traits/UserAdmin.php

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

77
trait UserAdmin
88
{
9+
/**
10+
* If User is base admin
11+
* On /admin login validation and all /admin navigation
12+
* @return bool
13+
*/
14+
public function isBaseAdmin()
15+
{
16+
return $this->hasRole(Role::$BASE_ADMIN);
17+
}
18+
919
/**
1020
* If User is admin
1121
* @return bool

database/seeds/RoleTableSeeder.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,50 @@ public function run()
1111

1212
// basic website only role
1313
Role::create([
14-
'icon' => 'user',
14+
'icon' => 'desktop',
1515
'name' => 'Website',
1616
'slug' => '/',
1717
'keyword' => 'website',
1818
]);
1919

20-
// basic admin role
20+
// basic user (website or/and admin or any other accounts)
2121
Role::create([
22-
'icon' => 'user-secret',
23-
'name' => 'Basic Admin',
24-
'slug' => '/admin',
25-
'keyword' => 'admin',
22+
'icon' => 'user',
23+
'name' => 'Base User',
24+
'slug' => '/',
25+
'keyword' => 'user',
2626
]);
2727

28-
// admin and analytics
28+
// base admin role (to be able to log into /admin)
2929
Role::create([
30-
'icon' => 'user-circle',
31-
'name' => 'Analytics',
30+
'icon' => 'user-secret',
31+
'name' => 'Basic Admin',
3232
'slug' => '/admin',
33-
'keyword' => 'analytics',
33+
'keyword' => 'base_admin',
3434
]);
3535

36+
// super
3637
Role::create([
3738
'icon' => 'user-secret',
3839
'name' => 'Admin',
3940
'slug' => '/admin',
40-
'keyword' => 'admin_super',
41+
'keyword' => 'admin',
4142
]);
4243

44+
// developer
4345
Role::create([
4446
'icon' => 'universal-access',
4547
'name' => 'Developer',
4648
'slug' => '/admin',
4749
'keyword' => 'developer',
4850
]);
51+
52+
// will only be able to view /admin and /admin/analytics
53+
Role::create([
54+
'icon' => 'user-circle',
55+
'name' => 'Analytics',
56+
'slug' => '/admin',
57+
'keyword' => 'analytics',
58+
]);
4959
}
5060
}

database/seeds/UserTableSeeder.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use App\User;
44
use Carbon\Carbon;
5+
use App\Models\Role;
56
use Illuminate\Database\Seeder;
67

78
class UserTableSeeder extends Seeder
@@ -74,13 +75,14 @@ public function run(Faker\Generator $faker)
7475
}*/
7576
}
7677

78+
/**
79+
* Add all the roles to the user
80+
* @param $user
81+
*/
7782
private function addAllRolesToUser($user)
7883
{
79-
$user->syncRoles([
80-
\App\Models\Role::$WEBSITE,
81-
\App\Models\Role::$ADMIN,
82-
\App\Models\Role::$ADMIN_SUPER,
83-
\App\Models\Role::$DEVELOPER,
84-
]);
84+
$roles = Role::all()->pluck('keyword', 'id')->values();
85+
86+
$user->syncRoles($roles);
8587
}
8688
}

0 commit comments

Comments
 (0)