Last active
February 24, 2020 20:07
-
-
Save stephenjude/ed6b79361cd46752773bb25aa24eccd8 to your computer and use it in GitHub Desktop.
Simple implementation of Spatie Permission Role package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
use Spatie\Permission\Models\Permission; | |
use Spatie\Permission\Models\Role; | |
trait TenantRolesAndPermissions | |
{ | |
protected $see_email_students = 'see email students'; | |
protected $see_pin_students = 'see pin students'; | |
protected $update_student_pin = 'update student pin'; | |
protected $update_student_email = 'update student email'; | |
protected $entry_agent_role = 'entry_agent'; | |
protected $email_agent_role = 'email_agent'; | |
protected $roles = [ | |
'email_agent' => 'getEmailAgentPermissions', | |
'entry_agent' => 'getEntryAgentPermissions', | |
]; | |
public function setupRolesAndPermisions() | |
{ | |
$this->createPermissions(); | |
$this->createRoles(); | |
} | |
public function createRoles() | |
{ | |
$entry_agent = Role::firstOrCreate(['guard_name' => 'staff-api', 'name' => $this->entry_agent_role]); | |
$email_agent = Role::firstOrCreate(['guard_name' => 'staff-api', 'name' => $this->email_agent_role]); | |
$email_agent->syncPermissions($this->getEmailAgentPermissions()); | |
$entry_agent->syncPermissions($this->getEntryAgentPermissions()); | |
} | |
public function getRolePermissions($role): array | |
{ | |
$role_permissions = collect($this->roles)->only($role); | |
if ($role_permissions) { | |
$get_role_permission = $role_permissions[$role]; | |
return $this->$get_role_permission(); | |
} | |
return []; | |
} | |
public function checkAccess($staff, $permission) | |
{ | |
return $staff->hasPermissionTo($permission); | |
} | |
public function getEmailAgentPermissions(): array | |
{ | |
return [ | |
$this->see_email_students, | |
$this->update_student_email, | |
]; | |
} | |
public function getEntryAgentPermissions(): array | |
{ | |
return [ | |
$this->see_pin_students, | |
$this->update_student_pin, | |
]; | |
} | |
public function createPermissions() | |
{ | |
Permission::firstOrCreate(['guard_name' => 'staff-api', 'name' => $this->see_email_students]); | |
Permission::firstOrCreate(['guard_name' => 'staff-api', 'name' => $this->see_pin_students]); | |
Permission::firstOrCreate(['guard_name' => 'staff-api', 'name' => $this->update_student_pin]); | |
Permission::firstOrCreate(['guard_name' => 'staff-api', 'name' => $this->update_student_email]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Contracts\Auth\MustVerifyEmail; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Spatie\Permission\Traits\HasRoles; | |
class Staff extends Authenticatable | |
{ | |
use Notifiable, HasRoles; | |
protected $guard_name = 'staff-api'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Staff; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Http\Request; | |
use App\Traits\RolesAndPermissions; | |
use App\Models\Staff; | |
class StaffController extends Controller | |
{ | |
use RolesAndPermissions; | |
public function login() | |
{ | |
$data = request()->validate([ | |
'email' => 'bail|required|email', | |
'password' => 'bail|required' | |
]); | |
$staff = Staff::create($data); | |
$staff->assignRole($this->entry_agent_role); | |
return $staff; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment