Skip to content

Commit

Permalink
Admin Roles - update the roles and roles index
Browse files Browse the repository at this point in the history
  • Loading branch information
bpocallaghan committed Jun 1, 2018
1 parent 86a8967 commit b4c7212
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/Events/UserRegistered.php
Expand Up @@ -25,7 +25,7 @@ public function __construct(User $user, $token = null)
// if token - add admin role and mark as claimed
$userInvite = UserInvite::whereToken($token)->whereNull('claimed_at')->first();
if ($userInvite) {
$roles[] = Role::$ADMIN;
$roles[] = Role::$BASE_ADMIN;
// set invite claimed
$user->update(['gender' => 'male']);
$userInvite->update(['claimed_at' => Carbon::now()]);
Expand Down
Expand Up @@ -23,7 +23,7 @@ public function index()
{
save_resource_url();

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

return $this->view('settings.administrators.index', compact('items'));
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/AuthenticateAdmin.php
Expand Up @@ -18,7 +18,7 @@ class AuthenticateAdmin
public function handle($request, Closure $next, $guard = null)
{
// not logged in as an admin - logout and go home
if (!user()->isAdmin()) {
if (!user()->isBaseAdmin()) {
\Auth::logout();

return redirect()->guest('/');
Expand Down
13 changes: 8 additions & 5 deletions app/Models/Role.php
Expand Up @@ -12,16 +12,19 @@ class Role extends TitanCMSModel
// basic website
public static $WEBSITE = 'website'; // 1

// basic admin
public static $ADMIN = 'admin'; // 2
// base user
public static $USER = 'user'; // 2

// admin + analytics
public static $ANALYTICS = 'analytics'; // 3
// basic admin
public static $BASE_ADMIN = 'base_admin'; // 3

public static $ADMIN_SUPER = 'admin_super'; // 4
public static $ADMIN_SUPER = 'admin'; // 4

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

// base admin + analytics only
public static $ANALYTICS = 'analytics'; // 6

protected $table = 'roles';

protected $guarded = ['id'];
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Traits/UserAdmin.php
Expand Up @@ -6,6 +6,16 @@

trait UserAdmin
{
/**
* If User is base admin
* On /admin login validation and all /admin navigation
* @return bool
*/
public function isBaseAdmin()
{
return $this->hasRole(Role::$BASE_ADMIN);
}

/**
* If User is admin
* @return bool
Expand Down
32 changes: 21 additions & 11 deletions database/seeds/RoleTableSeeder.php
Expand Up @@ -11,40 +11,50 @@ public function run()

// basic website only role
Role::create([
'icon' => 'user',
'icon' => 'desktop',
'name' => 'Website',
'slug' => '/',
'keyword' => 'website',
]);

// basic admin role
// basic user (website or/and admin or any other accounts)
Role::create([
'icon' => 'user-secret',
'name' => 'Basic Admin',
'slug' => '/admin',
'keyword' => 'admin',
'icon' => 'user',
'name' => 'Base User',
'slug' => '/',
'keyword' => 'user',
]);

// admin and analytics
// base admin role (to be able to log into /admin)
Role::create([
'icon' => 'user-circle',
'name' => 'Analytics',
'icon' => 'user-secret',
'name' => 'Basic Admin',
'slug' => '/admin',
'keyword' => 'analytics',
'keyword' => 'base_admin',
]);

// super
Role::create([
'icon' => 'user-secret',
'name' => 'Admin',
'slug' => '/admin',
'keyword' => 'admin_super',
'keyword' => 'admin',
]);

// developer
Role::create([
'icon' => 'universal-access',
'name' => 'Developer',
'slug' => '/admin',
'keyword' => 'developer',
]);

// will only be able to view /admin and /admin/analytics
Role::create([
'icon' => 'user-circle',
'name' => 'Analytics',
'slug' => '/admin',
'keyword' => 'analytics',
]);
}
}
14 changes: 8 additions & 6 deletions database/seeds/UserTableSeeder.php
Expand Up @@ -2,6 +2,7 @@

use App\User;
use Carbon\Carbon;
use App\Models\Role;
use Illuminate\Database\Seeder;

class UserTableSeeder extends Seeder
Expand Down Expand Up @@ -74,13 +75,14 @@ public function run(Faker\Generator $faker)
}*/
}

/**
* Add all the roles to the user
* @param $user
*/
private function addAllRolesToUser($user)
{
$user->syncRoles([
\App\Models\Role::$WEBSITE,
\App\Models\Role::$ADMIN,
\App\Models\Role::$ADMIN_SUPER,
\App\Models\Role::$DEVELOPER,
]);
$roles = Role::all()->pluck('keyword', 'id')->values();

$user->syncRoles($roles);
}
}

0 comments on commit b4c7212

Please sign in to comment.