Skip to content

Commit

Permalink
Permission: clean up old mess
Browse files Browse the repository at this point in the history
  • Loading branch information
ilestis committed Nov 30, 2023
1 parent 4f67627 commit b84013f
Show file tree
Hide file tree
Showing 15 changed files with 577 additions and 602 deletions.
11 changes: 8 additions & 3 deletions app/Http/Controllers/Campaign/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@
use App\Http\Requests\StoreCampaignRole;
use App\Models\Campaign;
use App\Models\CampaignRole;
use App\Services\Permissions\RolePermissionService;
use Illuminate\Http\Request;

class RoleController extends Controller
{
protected string $view = 'campaigns.roles';

protected RolePermissionService $service;

/**
* Create a new controller instance.
* @return void
*/
public function __construct()
public function __construct(RolePermissionService $rolePermissionService)
{
$this->middleware('auth');
$this->middleware('campaign.member');
$this->service = $rolePermissionService;
}

/**
Expand Down Expand Up @@ -132,6 +136,7 @@ public function show(Campaign $campaign, CampaignRole $campaignRole)
'role' => $campaignRole,
'campaign' => $campaign,
'members' => $members,
'permissionService' => $this->service->role($campaignRole)
]);
}

Expand Down Expand Up @@ -176,7 +181,7 @@ public function savePermissions(Request $request, Campaign $campaign, CampaignRo
$this->authorize('view', [$campaignRole, $campaign]);
$this->authorize('update', $campaignRole);

$campaignRole->savePermissions($request->post('permissions', []));
$this->service->role($campaignRole)->savePermissions($request->post('permissions', []));

return redirect()->route('campaign_roles.show', [$campaign, 'campaign_role' => $campaignRole])
->with('success', trans('crud.permissions.success'));
Expand Down Expand Up @@ -249,7 +254,7 @@ public function toggle(Campaign $campaign, CampaignRole $campaignRole, int $enti
abort(404);
}

$enabled = $campaignRole->toggle($entityType, $action);
$enabled = $this->service->role($campaignRole)->toggle($entityType, $action);
return response()->json([
'success' => true,
'status' => $enabled,
Expand Down
81 changes: 0 additions & 81 deletions app/Models/CampaignRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,49 +131,6 @@ public function rolePermissions()
->whereNull('entity_id');
}

/**
*/
public function savePermissions(array $permissions = [])
{
// Load existing
$existing = [];
foreach ($this->rolePermissions as $permission) {
if (empty($permission->entity_type_id)) {
$existing['campaign_' . $permission->action] = $permission;
continue;
}
$existing[$permission->entity_type_id . '_' . $permission->action] = $permission;
}

// Loop on submitted form
if (empty($permissions)) {
$permissions = [];
}

foreach ($permissions as $key => $module) {
// Check if exists$
if (isset($existing[$key])) {
// Do nothing
unset($existing[$key]);
} else {
$action = Str::after($key, '_');
if ($module === 'campaign') {
$module = 0;
}

$this->add($module, (int) $action);
}
}

// Delete existing that weren't updated
foreach ($existing as $permission) {
// Only delete if it's a "general" and not an entity specific permission
if (!is_numeric($permission->entityId())) {
$permission->delete();
}
}
}

/**
*/
public function scopeSearch(Builder $builder, string $search = null): Builder
Expand All @@ -182,44 +139,6 @@ public function scopeSearch(Builder $builder, string $search = null): Builder
->where('name', 'like', "%{$search}%");
}

/**
* Toggle an entity's action permission
*/
public function toggle(int $entityType, int $action): bool
{
$perm = $this->permissions()
->where('entity_type_id', $entityType)
->where('action', $action)
->whereNull('entity_id')
->first();

if ($perm) {
$perm->delete();
return false;
}

$this->add($entityType, $action);
return true;
}

/**
* Add a campaign permission for the role
*/
protected function add(int $entityType, int $action): CampaignPermission
{
if ($entityType === 0) {
$entityType = null;
}
return CampaignPermission::create([
//'key' => $key,
'campaign_role_id' => $this->id,
//'table_name' => $value,
'access' => true,
'action' => $action,
'entity_type_id' => $entityType
//'campaign_id' => $campaign->id,
]);
}
/**
*/
public function url(string $sub): string
Expand Down
1 change: 1 addition & 0 deletions app/Models/Quest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function scopePreparedWith(Builder $query): Builder
'entity',
'entity.image',
'entity.calendarDateEvents',
'entity.calendarDate',
'quests',
'instigator',
//'elements',
Expand Down
8 changes: 8 additions & 0 deletions app/Models/Relations/CampaignRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ public function members()
return $this->hasMany('App\Models\CampaignUser');
}

public function nonAdmins()
{
return $this
->members()
->withoutAdmins()
->with(['user', 'user.campaignRoles'])
;
}
/**
*/
public function roles()
Expand Down
10 changes: 7 additions & 3 deletions app/Services/BulkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Services\Entity\MoveService;
use App\Services\Entity\TagService;
use App\Services\Entity\TransformService;
use App\Services\Permissions\BulkPermissionService;
use App\Traits\CampaignAware;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand All @@ -25,7 +26,7 @@ class BulkService

protected EntityService $entityService;

protected PermissionService $permissionService;
protected BulkPermissionService $permissionService;

protected TransformService $transformService;

Expand All @@ -46,7 +47,7 @@ class BulkService

public function __construct(
EntityService $entityService,
PermissionService $permissionService,
BulkPermissionService $permissionService,
TransformService $transformService,
MoveService $moveService
) {
Expand Down Expand Up @@ -129,7 +130,10 @@ public function permissions(array $permissions = [], bool $override = true): int
foreach ($this->ids as $id) {
$entity = $model->findOrFail($id);
if (auth()->user()->can('update', $entity)) {
$this->permissionService->change($permissions, $entity->entity, $override);
$this->permissionService
->entity($entity->entity)
->override($override)
->change($permissions);
$this->count++;
}
}
Expand Down
Loading

0 comments on commit b84013f

Please sign in to comment.