diff --git a/app/Actions/HumanResources/Employee/UI/EditEmployee.php b/app/Actions/HumanResources/Employee/UI/EditEmployee.php index 9a4f2d72f..57ffc0bd5 100644 --- a/app/Actions/HumanResources/Employee/UI/EditEmployee.php +++ b/app/Actions/HumanResources/Employee/UI/EditEmployee.php @@ -43,9 +43,9 @@ public function authorize(ActionRequest $request): bool public function asController(Organisation $organisation, Employee $employee, ActionRequest $request): Employee { - $this->organisation = $organisation; $this->initialisation($organisation, $request); + return $this->handle($employee); } @@ -58,7 +58,7 @@ public function htmlResponse(Employee $employee, ActionRequest $request): Respon $user = $employee->getUser(); $jobPositionsOrganisationData = GetJobPositionsOrganisationData::run($employee, $this->organisation); - $jobPositionsGroupData = GetJobPositionsGroupData::run($employee, $this->organisation->group); + $jobPositionsGroupData = GetPermissionGroupData::run($employee, $this->group); $sections['properties'] = [ 'label' => __('Properties'), diff --git a/app/Actions/HumanResources/Employee/UI/GetJobPositionsGroupData.php b/app/Actions/HumanResources/Employee/UI/GetJobPositionsGroupData.php deleted file mode 100644 index 2f1411a6c..000000000 --- a/app/Actions/HumanResources/Employee/UI/GetJobPositionsGroupData.php +++ /dev/null @@ -1,44 +0,0 @@ - - * Created: Tue, 14 Mar 2023 19:13:28 Malaysia Time, Kuala Lumpur, Malaysia - * Copyright (c) 2023, Raul A Perusquia Flores - */ - -namespace App\Actions\HumanResources\Employee\UI; - -use App\Models\HumanResources\Employee; -use App\Models\SysAdmin\Group; -use App\Models\SysAdmin\Guest; -use App\Models\SysAdmin\User; -use Lorisleiva\Actions\Concerns\AsAction; - -class GetJobPositionsGroupData -{ - use AsAction; - - public function handle(Employee|User|Guest $employee, Group $group): array - { - return (array) $employee->jobPositions->map(function ($jobPosition) use ($group) { - $scopes = collect($jobPosition->pivot->scopes)->mapWithKeys(function ($scopeIds, $scope) use ($jobPosition, $group) { - return match ($scope) { - 'Warehouse' => [ - 'warehouses' => $group->warehouses->whereIn('id', $scopeIds)->pluck('slug')->toArray() - ], - 'Shop' => [ - 'shops' => $group->shops->whereIn('id', $scopeIds)->pluck('slug')->toArray() - ], - 'Fulfilment' => [ - 'fulfilments' => $group->fulfilments->whereIn('id', $scopeIds)->pluck('slug')->toArray() - ], - default => [] - }; - }); - - return [$jobPosition->code => $scopes->toArray()]; - })->reduce(function ($carry, $item) { - return array_merge_recursive($carry, $item); - }, []); - } -} diff --git a/app/Actions/HumanResources/Employee/UI/GetPermissionGroupData.php b/app/Actions/HumanResources/Employee/UI/GetPermissionGroupData.php new file mode 100644 index 000000000..1bb80c4fc --- /dev/null +++ b/app/Actions/HumanResources/Employee/UI/GetPermissionGroupData.php @@ -0,0 +1,29 @@ + + * Created: Tue, 14 Mar 2023 19:13:28 Malaysia Time, Kuala Lumpur, Malaysia + * Copyright (c) 2023, Raul A Perusquia Flores + */ + +namespace App\Actions\HumanResources\Employee\UI; + +use App\Models\HumanResources\Employee; +use App\Models\SysAdmin\Group; +use App\Models\SysAdmin\Guest; +use App\Models\SysAdmin\User; +use Lorisleiva\Actions\Concerns\AsAction; + +class GetPermissionGroupData +{ + use AsAction; + + public function handle(Employee|User|Guest $user, Group $group): array + { + return (array) $user->permissions->map(function ($permission) use ($group) { + return [$permission->name]; + })->reduce(function ($carry, $item) { + return array_merge_recursive($carry, $item); + }, []); + } +} diff --git a/app/Actions/SysAdmin/User/UI/EditUser.php b/app/Actions/SysAdmin/User/UI/EditUser.php index f9ed40189..87b606a94 100644 --- a/app/Actions/SysAdmin/User/UI/EditUser.php +++ b/app/Actions/SysAdmin/User/UI/EditUser.php @@ -8,7 +8,7 @@ namespace App\Actions\SysAdmin\User\UI; -use App\Actions\HumanResources\Employee\UI\GetJobPositionsGroupData; +use App\Actions\HumanResources\Employee\UI\GetPermissionGroupData; use App\Actions\HumanResources\Employee\UI\GetJobPositionsOrganisationData; use App\Actions\OrgAction; use App\Enums\SysAdmin\Authorisation\RolesEnum; @@ -100,7 +100,7 @@ class_basename(Warehouse::class) => Organisation::all()->map(function (Organisat $jobPositionsOrganisationsData[] = $jobPositionsOrganisationData; } - $jobPositionsGroupData = GetJobPositionsGroupData::run($user, $this->group); + $permissionsGroupData = GetPermissionGroupData::run($user, $this->group); $organisations = $user->group->organisations; $reviewData = $organisations->mapWithKeys(function ($organisation) { @@ -186,7 +186,7 @@ class_basename(Warehouse::class) => Organisation::all()->map(function (Organisat 'organisation_list' => $organisationList, 'updateRoute' => [ 'method' => 'patch', - "name" => "grp.models.user.pseudo-job-positions.update", + "name" => "grp.models.user.permissions.update", 'parameters' => [ 'user' => $user->id ] @@ -204,7 +204,7 @@ class_basename(Warehouse::class) => Organisation::all()->map(function (Organisat ]; })->toArray(), 'value' => [ - 'group' => $jobPositionsGroupData, + 'group' => $permissionsGroupData, 'organisations' => $jobPositionsOrganisationsData, ], diff --git a/app/Actions/SysAdmin/User/UpdateUsersModelHasPermission.php b/app/Actions/SysAdmin/User/UpdateUsersModelHasPermission.php new file mode 100644 index 000000000..c47ad9e06 --- /dev/null +++ b/app/Actions/SysAdmin/User/UpdateUsersModelHasPermission.php @@ -0,0 +1,99 @@ + + * Created: Mon, 30 Sept 2024 18:51:48 Malaysia Time, Kuala Lumpur, Malaysia + * Copyright (c) 2024, Raul A Perusquia Flores + */ + +namespace App\Actions\SysAdmin\User; + +use App\Actions\GrpAction; +use App\Actions\Traits\WithPreparePositionsForValidation; +use App\Actions\Traits\WithActionUpdate; +use App\Actions\Traits\WithReorganisePositions; +use App\Http\Resources\HumanResources\EmployeeResource; +use App\Models\HumanResources\Employee; +use App\Models\SysAdmin\Organisation; +use App\Models\SysAdmin\User; +use Illuminate\Support\Arr; +use Lorisleiva\Actions\ActionRequest; + +class UpdateUsersModelHasPermission extends GrpAction +{ + use WithActionUpdate; + use WithPreparePositionsForValidation; + use WithReorganisePositions; + + protected bool $asAction = false; + + private Employee $employee; + + private User $user; + + + private Organisation $organisation; + + public function handle(User $user, array $modelData): User + { + $permissions = Arr::get($modelData, 'permissions', []); + $positions = $this->reorganisePositionsSlugsToIds($permissions); + + $user->syncPermissions($positions); + + $user->refresh(); + + return $user; + } + + + public function authorize(ActionRequest $request): bool + { + if ($this->asAction) { + return true; + } + + return $request->user()->hasPermissionTo("sysadmin.edit"); + } + + public function rules(): array + { + return [ + 'permissions' => ['sometimes', 'array'], + 'permissions.group' => ['sometimes', 'array'] + ]; + } + + public function action(User $user, Organisation $organisation, $modelData): User + { + $this->asAction = true; + $this->user = $user; + $this->organisation = $organisation; + + $this->initialisation($user->group, $modelData); + + return $this->handle($user, $this->validatedData); + } + + public function prepareForValidation(ActionRequest $request): void + { + $this->preparePositionsForValidation(); + //todo check if is a valid current model (gust|employee) in user_has_models if is an active one reject + + + } + + public function asController(User $user, ActionRequest $request): User + { + $this->user = $user; + + $this->initialisation(app('group'), $request); + + return $this->handle($user, $this->validatedData); + } + + public function jsonResponse(User $user): EmployeeResource + { + return new EmployeeResource($user); + } +} diff --git a/app/Actions/Traits/WithReorganisePositions.php b/app/Actions/Traits/WithReorganisePositions.php index 84fe448b2..61dee88a9 100644 --- a/app/Actions/Traits/WithReorganisePositions.php +++ b/app/Actions/Traits/WithReorganisePositions.php @@ -14,6 +14,7 @@ use App\Models\Inventory\Warehouse; use App\Models\Production\Production; use App\Models\SysAdmin\Organisation; +use App\Models\SysAdmin\Permission; use Illuminate\Support\Arr; trait WithReorganisePositions @@ -26,15 +27,22 @@ public function reorganisePositionsSlugsToIds($positionsWithSlugs): array if (array_key_exists('group', $positionsWithSlugs)) { $positionsWithSlugs = [ - ['slug' => $positionsWithSlugs['group']] + ['slug' => $positionsWithSlugs['group'][0]] ]; + foreach ($positionsWithSlugs as $positionData) { + + $permission = Permission::firstWhere('name', $positionData['slug']); + + $positions[] = $permission->id; + } + + return $positions; } $positions = []; foreach ($positionsWithSlugs as $positionData) { $jobPosition = JobPosition::firstWhere('slug', $positionData['slug']); - $positions[$jobPosition->id] = $this->reorganiseScopes($positionData['scopes']); } diff --git a/app/Actions/UI/Grp/Layout/GetFulfilmentNavigation.php b/app/Actions/UI/Grp/Layout/GetFulfilmentNavigation.php index 1d7177025..c88d44d80 100644 --- a/app/Actions/UI/Grp/Layout/GetFulfilmentNavigation.php +++ b/app/Actions/UI/Grp/Layout/GetFulfilmentNavigation.php @@ -254,7 +254,7 @@ public function handle(Fulfilment $fulfilment, User $user): array 'root' => 'grp.org.fulfilments.show.crm.', 'route' => [ 'name' => 'grp.org.fulfilments.show.crm.customers.index', - 'parameters' => [$fulfilment->organisation->slug, $fulfilment->slug, ['elements[status]'=>'active']] + 'parameters' => [$fulfilment->organisation->slug, $fulfilment->slug, ['elements[status]' => 'active']] ], 'topMenu' => [ @@ -266,7 +266,7 @@ public function handle(Fulfilment $fulfilment, User $user): array 'root' => 'grp.org.fulfilments.show.crm.customers.', 'route' => [ 'name' => 'grp.org.fulfilments.show.crm.customers.index', - 'parameters' => [$fulfilment->organisation->slug, $fulfilment->slug, ['elements[status]'=>'active']] + 'parameters' => [$fulfilment->organisation->slug, $fulfilment->slug, ['elements[status]' => 'active']] ], ], // Prospects for fulfilment is still not supported diff --git a/resources/js/Components/Forms/Fields/Permissions.vue b/resources/js/Components/Forms/Fields/Permissions.vue index fd1a77642..37b476c62 100644 --- a/resources/js/Components/Forms/Fields/Permissions.vue +++ b/resources/js/Components/Forms/Fields/Permissions.vue @@ -48,7 +48,7 @@ console.log('mmmmm', props.options) const handleBox = (shopsSelected: string[], shopSlug: string) => { // console.log('ffff', shopsSelected) // if (shopsSelected.includes(shopSlug)) { - + // const indexShopSlug = shopsSelected.indexOf(shopSlug) // if (indexShopSlug !== -1) { // shopsSelected.splice(indexShopSlug, 1) @@ -79,7 +79,7 @@ const groupPositionList = { icon: 'fas fa-computer-classic', subDepartment: [ { - slug: "system-admin", + slug: "sysadmin", label: trans("System Administrator"), // number_employees: props.options.positions?.data?.find(position => position.slug == 'system_admin')?.number_employees || 0, } @@ -92,13 +92,13 @@ const groupPositionList = { level: 'group_procurement', subDepartment: [ { - slug: "gp-sc", + slug: "supply-chain", grade: "manager", label: trans("Supply Chain Manager"), // number_employees: props.options.positions?.data?.find(position => position.slug == 'gp-sc')?.number_employees || 0, }, { - slug: "gp-g", + slug: "goods", grade: "manager", label: trans("Goods Manager"), // number_employees: props.options.positions?.data?.find(position => position.slug == 'gp-g')?.number_employees || 0, @@ -184,12 +184,12 @@ const selectedOrganisation = ref(organisatio