Skip to content

Commit

Permalink
Adding Users Datatable
Browse files Browse the repository at this point in the history
  • Loading branch information
hiamir committed Dec 8, 2022
1 parent 58da840 commit 6763b02
Show file tree
Hide file tree
Showing 24 changed files with 637 additions and 57 deletions.
1 change: 0 additions & 1 deletion app/Actions/Permissions/PermissionFormData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class PermissionFormData

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

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

Expand Down
13 changes: 9 additions & 4 deletions app/Actions/Permissions/PermissionModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ class PermissionModels
public function handle(Permission $permission): array
{
$models = Data::getAvailableModels();

if (auth()->guard('admin')->check() ) {
$exclude = ['Admin', 'Membership', 'Team', 'TeamInvitation'];
$models = array_diff($models, $exclude);
$include=['Role','Permission'];
$exclude = ['Admin', 'Membership', 'Team', 'TeamInvitation'];
$array = [];
foreach (array_diff(array_merge($include,$models),$exclude) as $model) {
$array[$model] = $model;
}
$models = $array;
if (auth()->guard('admin')->check()) {
if (count($models) === 1) $permission['model'] = array_key_first($models);

} elseif (auth()->guard('admin')->check() && auth()->user()->hasRole('super admin')) {
Expand All @@ -26,6 +30,7 @@ public function handle(Permission $permission): array
$models = array_diff($models, $exclude);
if (count($models) === 1) $permission['model'] = array_key_first($models);
}

return $models;
}

Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Permissions/PermissionSanitizeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PermissionSanitizeData
use withAttributes;

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

Expand Down
16 changes: 8 additions & 8 deletions app/Actions/Permissions/PermissionSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ class PermissionSave
/**
* @throws ValidationException
*/
public function handle(Permission $role):array
public function handle(Permission $permission):array
{
$role= PermissionSanitizeData::run($role);
$data = $this->set('role', $role)->fill($role->toArray());
$permission= UserSanitizeData::run($permission);
$data = $this->set('permission', $permission)->fill($permission->toArray());
Validator::make(
$data->attributes,
PermissionValidation::make()->rules($this->role->id),
PermissionValidation::make()->messages(),
UserValidation::make()->rules($this->permission->id),
UserValidation::make()->messages(),
)->validate();
try {
$success = DB::transaction(function () use ($role) {
$role->save();
return [true, $role];
$success = DB::transaction(function () use ($permission) {
$permission->save();
return [true, $permission];
});
} catch (\Exception $e) {
DB::rollback();
Expand Down
8 changes: 4 additions & 4 deletions app/Actions/Permissions/PermissionSubmit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class PermissionSubmit
use Data;


public function handle($thiss,$formType,$role): void
public function handle($thiss,$formType,$permission): void
{
$output = match ($formType) {
'create', 'update' => PermissionSave::run($role),
'delete' => PermissionDelete::run($role),
'create', 'update' => UserSave::run($permission),
'delete' => UserDelete::run($permission),
default => [],
};
$thiss->dispatchBrowserEvent('FirstModel', ['show' => false]);
$thiss->emit('refreshSidebar');
$thiss->afterSave($output, $thiss->formType, $role->name);
$thiss->afterSave($output, $thiss->formType, $permission->name);
}
}
36 changes: 27 additions & 9 deletions app/Actions/Permissions/PermissionValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Actions\Permissions;

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

class PermissionValidation
{
Expand All @@ -12,24 +12,42 @@ class PermissionValidation
public function rules($id=null): array
{
return [
'role.name' => 'required|min:4|unique:roles,name,' . $id,
'role.guard_name' => 'required',
'permission.name' => 'required|min:4|unique:permissions,name,' . $id,
'permission.model' => 'required',
'permission.guard_name' => 'required',
];
}

public function attributes($role): array
public function validationAttributes($permission): array
{
return [
'role.name' =>$role['name'],
'permission.name' =>$permission['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.',
'permission.name.required' => 'Permission name is required.',
'permission.name.min' => 'Permission must be at-least 4 letters long.',
'permission.name.unique' => ':attribute menu already exists!.',
'permission.model.required' => 'Model is required.',
'permission.guard_name.required' => 'Guard is required.',
];
}

public function updated(Permission $permission, $variable, $value){
switch ($variable){
case "permission.name":
($value==='') ? $permission['name']=(strtolower($permission['guard_name']) === 'web') ? 'user-'.strtolower($permission['model']).'-' : strtolower($permission['guard_name']).'-'.strtolower($permission['model']).'-' : $permission['name']=$value;

break;
case "permission.model":
$permission['name']=(strtolower($permission['guard_name']) === 'web') ? 'user-'.strtolower($value).'-' : strtolower($permission['guard_name']).'-'.strtolower($value).'-';
break;
}
($permission['guard_name'] !== null && $permission['model'] !== null) ? $disabled=false : $disabled=true;

return $disabled;
}

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

namespace App\Actions\Users;

use App\Models\User;
use Illuminate\Support\Facades\DB;
use Lorisleiva\Actions\Concerns\AsAction;
use Lorisleiva\Actions\Concerns\WithAttributes;

class UserDelete
{
use AsAction;
use withAttributes;


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


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

namespace App\Actions\Users;


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

class UserFormData
{
use AsAction;
use Data;

public function handle($type,$row=null): User
{
return ( ($type==='update' || $type==='delete') && $row !== null) ? User::find($row['id']) : new User();
}

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

namespace App\Actions\Users;

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

class UserGuards
{
use AsAction;

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

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


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

namespace App\Actions\Users;

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

class UserModels
{
use AsAction;

public function handle(User $user): array
{
$models = Data::getAvailableModels();
$include=['Role','Permission'];
$exclude = ['Admin', 'Membership', 'Team', 'TeamInvitation'];
$array = [];
foreach (array_diff(array_merge($include,$models),$exclude) as $model) {
$array[$model] = $model;
}
$models = $array;
if (auth()->guard('admin')->check()) {
if (count($models) === 1) $user['model'] = array_key_first($models);

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

return $models;
}


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

namespace App\Actions\Users;

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

class UserSanitizeData
{
use AsAction;
use withAttributes;

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

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

namespace App\Actions\Users;

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

class UserSave
{
use AsAction;
use withAttributes;


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



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

namespace App\Actions\Users;


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

class UserSubmit
{
use AsAction;
use Data;


public function handle($thiss,$formType,$permission): void
{
$output = match ($formType) {
'create', 'update' => UserSave::run($permission),
'delete' => UserDelete::run($permission),
default => [],
};
$thiss->dispatchBrowserEvent('FirstModel', ['show' => false]);
$thiss->emit('refreshSidebar');
$thiss->afterSave($output, $thiss->formType, $permission->name);
}
}
Loading

0 comments on commit 6763b02

Please sign in to comment.