-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,337 additions
and
5 deletions.
There are no files selected for viewing
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
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
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; | ||
} | ||
|
||
|
||
} |
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
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(); | ||
} | ||
|
||
} |
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
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; | ||
} | ||
|
||
|
||
} |
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
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; | ||
} | ||
|
||
|
||
} |
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
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; | ||
} | ||
|
||
} |
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
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; | ||
} | ||
|
||
|
||
|
||
} |
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
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); | ||
} | ||
} |
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
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.', | ||
]; | ||
} | ||
} |
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
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; | ||
} | ||
|
||
|
||
} |
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
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(); | ||
} | ||
|
||
} |
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
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; | ||
} | ||
|
||
|
||
} |
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
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; | ||
} | ||
|
||
} |
Oops, something went wrong.