Skip to content

Instantly share code, notes, and snippets.

@stephenjude
Last active February 24, 2020 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenjude/ed6b79361cd46752773bb25aa24eccd8 to your computer and use it in GitHub Desktop.
Save stephenjude/ed6b79361cd46752773bb25aa24eccd8 to your computer and use it in GitHub Desktop.
Simple implementation of Spatie Permission Role package
<?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]);
}
}
<?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';
}
<?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