Skip to content

Commit

Permalink
fix: group permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
itzArtha committed Jan 7, 2025
1 parent 6066767 commit 1a40c62
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 65 deletions.
4 changes: 2 additions & 2 deletions app/Actions/HumanResources/Employee/UI/EditEmployee.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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'),
Expand Down

This file was deleted.

29 changes: 29 additions & 0 deletions app/Actions/HumanResources/Employee/UI/GetPermissionGroupData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* Author: Raul Perusquia <[email protected]>
* 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);
}, []);
}
}
8 changes: 4 additions & 4 deletions app/Actions/SysAdmin/User/UI/EditUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
]
Expand All @@ -204,7 +204,7 @@ class_basename(Warehouse::class) => Organisation::all()->map(function (Organisat
];
})->toArray(),
'value' => [
'group' => $jobPositionsGroupData,
'group' => $permissionsGroupData,
'organisations' => $jobPositionsOrganisationsData,
],

Expand Down
99 changes: 99 additions & 0 deletions app/Actions/SysAdmin/User/UpdateUsersModelHasPermission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/*
* Author: Raul Perusquia <[email protected]>
* 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);
}
}
12 changes: 10 additions & 2 deletions app/Actions/Traits/WithReorganisePositions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']);
}

Expand Down
4 changes: 2 additions & 2 deletions app/Actions/UI/Grp/Layout/GetFulfilmentNavigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -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
Expand Down
22 changes: 11 additions & 11 deletions resources/js/Components/Forms/Fields/Permissions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
}
Expand All @@ -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,
Expand Down Expand Up @@ -184,12 +184,12 @@ const selectedOrganisation = ref<typeof organisation[number] | null>(organisatio
<FontAwesomeIcon v-if="jobGroup.icon" :icon="jobGroup.icon" class='text-gray-400 fixed-width' aria-hidden='true' />
{{ jobGroup.department }}
</div>

<!-- Section: Radio (the clickable area) -->
<div class="h-full col-span-2 flex-col transition-all duration-200 ease-in-out">
<div class="flex items-center divide-x divide-slate-300">
<!-- Button: Radio position -->

<div class="pl-2 flex items-center gap-x-4">
<template v-for="subDepartment, idxSubDepartment in jobGroup.subDepartment">
<!-- If subDepartment is have atleast 1 Fulfilment, or have atleast 1 Shop, or have atleast 1 Warehouse, or have atleast 1 Production, or is a simple sub department (i.e buyer, administrator, etc) -->
Expand Down Expand Up @@ -222,13 +222,13 @@ const selectedOrganisation = ref<typeof organisation[number] | null>(organisatio
</button>
</template>
</div>

</div>
</div>
</div>
</template>
</div>

<Button @click="submitGroupPermissions" full label="Save group permissions" class="mt-4" :disabled="!form.isDirty || form.processing" :loading="form.processing" />
</Fieldset>

Expand All @@ -243,7 +243,7 @@ const selectedOrganisation = ref<typeof organisation[number] | null>(organisatio
Access
</div>
</div>
<div v-for="(review, slugReview) in props.fieldData.organisation_list.data"
class="border-l-2 border-indigo-500 pl-2 flex flex-col mb-1 gap-y-1"
>
Expand All @@ -255,7 +255,7 @@ const selectedOrganisation = ref<typeof organisation[number] | null>(organisatio
<div class="">{{ review.name }}</div>
<div v-tooltip="trans('Number job positions')" class="pl-3 pr-2">0/{{ review.number_job_positions }}</div>
</div>
<Collapse as="section" :when="review.slug == selectedOrganisation?.slug">
{{ form[fieldName] }}
<div v-if="options?.[review.slug]" class="border border-gray-300 rounded-md mb-2">
Expand Down Expand Up @@ -294,7 +294,7 @@ const selectedOrganisation = ref<typeof organisation[number] | null>(organisatio
</div> -->
<!-- {{ selectedOrganisation?.slug }} --- {{ form[fieldName][selectedOrganisation?.slug] }} -->



</div>
</template>
2 changes: 2 additions & 0 deletions routes/grp/web/models/sys_admin/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* Copyright (c) 2024, Raul A Perusquia Flores
*/

use App\Actions\SysAdmin\User\UpdateUsersModelHasPermission;
use App\Actions\SysAdmin\User\UpdateUsersPseudoJobPositions;
use App\Actions\SysAdmin\User\UpdateUser;
use Illuminate\Support\Facades\Route;

Route::name('user.')->prefix('user/{user:id}')->group(function () {
Route::patch('', UpdateUser::class)->name('update');
Route::patch('positions', UpdateUsersPseudoJobPositions::class)->name('pseudo-job-positions.update')->withoutScopedBindings();
Route::patch('permissions', UpdateUsersModelHasPermission::class)->name('permissions.update')->withoutScopedBindings();
});

0 comments on commit 1a40c62

Please sign in to comment.