Skip to content

Commit

Permalink
Adding Spartie Roles & Permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
hiamir committed Dec 7, 2022
1 parent 233a5f7 commit 58da840
Show file tree
Hide file tree
Showing 32 changed files with 1,337 additions and 5 deletions.
1 change: 0 additions & 1 deletion app/Actions/Menu/MenuOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function save(Menu $menu)
)->validate();
try {
$success = DB::transaction(function () use ($menu) {

$menu->save();
return [true, $menu];
});
Expand Down
34 changes: 34 additions & 0 deletions app/Actions/Permissions/PermissionDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Actions\Permissions;

use App\Models\Menu;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Lorisleiva\Actions\Concerns\AsAction;
use Lorisleiva\Actions\Concerns\WithAttributes;
use Spatie\Permission\Models\Permission;

class PermissionDelete
{
use AsAction;
use withAttributes;


public function handle(Permission $role):array
{
try {
$success = DB::transaction(function () use ($role) {
$role->delete();
return [true, $role];
});
} catch (\Exception $e) {
DB::rollback();
return [false, $e->getMessage()];
}
return $success;
}


}
21 changes: 21 additions & 0 deletions app/Actions/Permissions/PermissionFormData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Actions\Permissions;


use App\Traits\Data;
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\Permission\Models\Permission;

class PermissionFormData
{
use AsAction;
use Data;

public function handle($type,$row=null): Permission
{

return ( ($type==='update' || $type==='delete') && $row !== null) ? Permission::find($row['id']) : new Permission();
}

}
29 changes: 29 additions & 0 deletions app/Actions/Permissions/PermissionGuards.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Actions\Permissions;

use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\Permission\Models\Permission;

class PermissionGuards
{
use AsAction;

public function handle(Permission $permission): array
{
if (auth()->guard('admin')->check() && auth()->user()->hasRole('admin')) {
$guards = ['web'=>'User'];
if (count($guards) === 1) $permission['guard_name'] = array_key_first($guards);

} elseif(auth()->guard('admin')->check() && auth()->user()->hasRole('super admin')) {
$guards = ['admin'=>'Admin'];
if (count($guards) === 1) $permission['guard_name'] = array_key_first($guards);
}else{
$guards = ['web'=>'User'];
if (count($guards) === 1) $permission['guard_name'] = array_key_first($guards);
}
return $guards;
}


}
33 changes: 33 additions & 0 deletions app/Actions/Permissions/PermissionModels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Actions\Permissions;

use App\Traits\Data;
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\Permission\Models\Permission;

class PermissionModels
{
use AsAction;

public function handle(Permission $permission): array
{
$models = Data::getAvailableModels();

if (auth()->guard('admin')->check() ) {
$exclude = ['Admin', 'Membership', 'Team', 'TeamInvitation'];
$models = array_diff($models, $exclude);
if (count($models) === 1) $permission['model'] = array_key_first($models);

} elseif (auth()->guard('admin')->check() && auth()->user()->hasRole('super admin')) {
if (count($models) === 1) $permission['model'] = array_key_first($models);
} else {
$exclude = ['Admin', 'Membership', 'Team', 'TeamInvitation'];
$models = array_diff($models, $exclude);
if (count($models) === 1) $permission['model'] = array_key_first($models);
}
return $models;
}


}
19 changes: 19 additions & 0 deletions app/Actions/Permissions/PermissionSanitizeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Actions\Permissions;

use Lorisleiva\Actions\Concerns\AsAction;
use Lorisleiva\Actions\Concerns\WithAttributes;
use Spatie\Permission\Models\Permission;

class PermissionSanitizeData
{
use AsAction;
use withAttributes;

public function handle($data) :Permission{
$data->name=ucfirst($data->name);
return $data;
}

}
44 changes: 44 additions & 0 deletions app/Actions/Permissions/PermissionSave.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Actions\Permissions;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Lorisleiva\Actions\Concerns\AsAction;
use Lorisleiva\Actions\Concerns\WithAttributes;
use Spatie\Permission\Models\Permission;

class PermissionSave
{
use AsAction;
use withAttributes;


/**
* @throws ValidationException
*/
public function handle(Permission $role):array
{
$role= PermissionSanitizeData::run($role);
$data = $this->set('role', $role)->fill($role->toArray());
Validator::make(
$data->attributes,
PermissionValidation::make()->rules($this->role->id),
PermissionValidation::make()->messages(),
)->validate();
try {
$success = DB::transaction(function () use ($role) {
$role->save();
return [true, $role];
});
} catch (\Exception $e) {
DB::rollback();
return [false, $e->getMessage()];
}
return $success;
}



}
26 changes: 26 additions & 0 deletions app/Actions/Permissions/PermissionSubmit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Actions\Permissions;


use App\Traits\Data;
use Lorisleiva\Actions\Concerns\AsAction;

class PermissionSubmit
{
use AsAction;
use Data;


public function handle($thiss,$formType,$role): void
{
$output = match ($formType) {
'create', 'update' => PermissionSave::run($role),
'delete' => PermissionDelete::run($role),
default => [],
};
$thiss->dispatchBrowserEvent('FirstModel', ['show' => false]);
$thiss->emit('refreshSidebar');
$thiss->afterSave($output, $thiss->formType, $role->name);
}
}
35 changes: 35 additions & 0 deletions app/Actions/Permissions/PermissionValidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Actions\Permissions;

use App\Models\Menu;
use Lorisleiva\Actions\Concerns\AsAction;

class PermissionValidation
{
use AsAction;

public function rules($id=null): array
{
return [
'role.name' => 'required|min:4|unique:roles,name,' . $id,
'role.guard_name' => 'required',
];
}

public function attributes($role): array
{
return [
'role.name' =>$role['name'],
];
}

public function messages(){
return [
'role.name.required' => 'Menu name is required.',
'role.name.min' => 'Menu must be at-least 4 letters long.',
'role.name.unique' => ':attribute menu already exists!.',
'role.guard_name.required' => 'Guard is required.',
];
}
}
34 changes: 34 additions & 0 deletions app/Actions/Roles/RoleDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Actions\Roles;

use App\Models\Menu;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Lorisleiva\Actions\Concerns\AsAction;
use Lorisleiva\Actions\Concerns\WithAttributes;
use Spatie\Permission\Models\Role;

class RoleDelete
{
use AsAction;
use withAttributes;


public function handle(Role $role):array
{
try {
$success = DB::transaction(function () use ($role) {
$role->delete();
return [true, $role];
});
} catch (\Exception $e) {
DB::rollback();
return [false, $e->getMessage()];
}
return $success;
}


}
21 changes: 21 additions & 0 deletions app/Actions/Roles/RoleFormData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Actions\Roles;


use App\Traits\Data;
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\Permission\Models\Role;

class RoleFormData
{
use AsAction;
use Data;

public function handle($type,$row=null): Role
{

return ( ($type==='update' || $type==='delete') && $row !== null) ? Role::find($row['id']) : new Role();
}

}
35 changes: 35 additions & 0 deletions app/Actions/Roles/RoleGuards.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Actions\Roles;

use App\Models\Menu;
use App\Traits\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Lorisleiva\Actions\Concerns\AsAction;
use Lorisleiva\Actions\Concerns\WithAttributes;
use Spatie\Permission\Models\Role;

class RoleGuards
{
use AsAction;

public function handle(Role $role): array
{
if (auth()->guard('admin')->check() && auth()->user()->hasRole('admin')) {
$guards = ['web'=>'User'];
if (count($guards) === 1) $role['guard_name'] = array_key_first($guards);

} elseif(auth()->guard('admin')->check() && auth()->user()->hasRole('super admin')) {
$guards = ['admin'=>'Admin'];
if (count($guards) === 1) $role['guard_name'] = array_key_first($guards);
}else{
$guards = ['web'=>'User'];
if (count($guards) === 1) $role['guard_name'] = array_key_first($guards);
}
return $guards;
}


}
23 changes: 23 additions & 0 deletions app/Actions/Roles/RoleSanitizeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Actions\Roles;

use App\Models\Menu;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Lorisleiva\Actions\Concerns\AsAction;
use Lorisleiva\Actions\Concerns\WithAttributes;
use Spatie\Permission\Models\Role;

class RoleSanitizeData
{
use AsAction;
use withAttributes;

public function handle($data) :Role{
$data->name=ucfirst($data->name);
return $data;
}

}
Loading

0 comments on commit 58da840

Please sign in to comment.