-
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
24 changed files
with
637 additions
and
57 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
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
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
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,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; | ||
} | ||
|
||
|
||
} |
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,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(); | ||
} | ||
|
||
} |
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,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; | ||
} | ||
|
||
|
||
} |
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,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; | ||
} | ||
|
||
|
||
} |
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\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; | ||
} | ||
|
||
} |
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\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; | ||
} | ||
|
||
|
||
|
||
} |
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\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); | ||
} | ||
} |
Oops, something went wrong.