diff --git a/app/Http/Controllers/Campaign/RoleController.php b/app/Http/Controllers/Campaign/RoleController.php index dd37678da7..3ea21cde1a 100644 --- a/app/Http/Controllers/Campaign/RoleController.php +++ b/app/Http/Controllers/Campaign/RoleController.php @@ -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; } /** @@ -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])); diff --git a/app/Models/CampaignRole.php b/app/Models/CampaignRole.php index ac12b53ff4..66330f8b64 100644 --- a/app/Models/CampaignRole.php +++ b/app/Models/CampaignRole.php @@ -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; + } } diff --git a/app/Services/PermissionService.php b/app/Services/PermissionService.php index e17c21ddce..8f1d417649 100644 --- a/app/Services/PermissionService.php +++ b/app/Services/PermissionService.php @@ -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]), @@ -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]), @@ -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, @@ -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, @@ -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(); - } - } }