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 8674692 commit 60104d9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
11 changes: 6 additions & 5 deletions app/Http/Controllers/Campaign/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ class RoleController extends Controller
{
protected string $view = 'campaigns.roles';

protected PermissionService $service;

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

/**
Expand Down Expand Up @@ -111,7 +108,11 @@ public function store(StoreCampaignRole $request, Campaign $campaign)
$data = $request->all() + ['campaign_id' => $campaign->id];
$role = CampaignRole::create($data);
if ($request->has('duplicate') && $request->get('duplicate') != 0) {
$this->service->role($role)->duplicate($request->get('role_id'));
/** @var CampaignRole $copy */
$copy = CampaignRole::where('id', $request->get('role_id'))->first();
if ($copy) {
$copy->duplicate($role);
}
}
return redirect()->route('campaign_roles.index', $campaign)
->with('success_raw', __($this->view . '.create.success', ['name' => $role->name]));
Expand Down
10 changes: 10 additions & 0 deletions app/Models/CampaignRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,14 @@ public function url(string $sub): string
{
return 'campaign_roles.' . $sub;
}

public function duplicate(CampaignRole $campaignRole): self
{
foreach ($this->permissions as $permission) {
$newPermission = $permission->replicate(['campaign_role_id']);
$newPermission->campaign_role_id = $campaignRole->id;
$newPermission->save();
}
return $this;
}
}
22 changes: 2 additions & 20 deletions app/Services/PermissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public function campaignPermissions(): array
$key = "campaign_{$action}";
$permissions['campaign'][] = [
'action' => $action,
//'table' => $table,
'key' => $key,
'icon' => Arr::first($icons[$action]),
'label' => Arr::last($icons[$action]),
Expand Down Expand Up @@ -222,7 +221,6 @@ public function galleryPermissions(): array
$key = "campaign_{$action}";
$permissions['campaign'][] = [
'action' => $action,
//'table' => $table,
'key' => $key,
'icon' => Arr::first($icons[$action]),
'label' => Arr::last($icons[$action]),
Expand Down Expand Up @@ -362,10 +360,8 @@ public function change($request, Entity $entity, bool $override = true): void
if ($action == 'allow') {
if (empty($permissions['role'][$roleId][$perm])) {
CampaignPermission::create([
//'key' => $entity->type() . '_' . $perm . '_' . $entity->child->id,
'campaign_role_id' => $roleId,
//'campaign_id' => $entity->campaign_id,
//'entity_type_id' => $entity->type_id,
'campaign_id' => $entity->campaign_id,
'entity_id' => $entity->id,
'misc_id' => $entity->child->id,
'action' => $perm,
Expand All @@ -383,11 +379,8 @@ public function change($request, Entity $entity, bool $override = true): void
} elseif ($action === 'deny') {
if (empty($permissions['role'][$roleId][$perm])) {
CampaignPermission::create([
//'key' => $entity->type() . '_' . $perm . '_' . $entity->child->id,
'campaign_role_id' => $roleId,
//'campaign_id' => $entity->campaign_id,
//'table_name' => $entity->pluralType(),
//'entity_type_id' => $entity->type_id,
'campaign_id' => $entity->campaign_id,
'entity_id' => $entity->id,
'misc_id' => $entity->child->id,
'action' => $perm,
Expand Down Expand Up @@ -583,15 +576,4 @@ public function users()
->get();
return $this->users;
}

public function duplicate(int $roleId): void
{
$oldRole = CampaignRole::where('id', $roleId)->first();
foreach ($oldRole->permissions as $permission) {
/** @var CampaignPermission $newPermission */
$newPermission = $permission->replicate(['campaign_role_id']);
$newPermission->campaign_role_id = $this->role->id;
$newPermission->save();
}
}
}

0 comments on commit 60104d9

Please sign in to comment.