Skip to content

Commit

Permalink
admin controller cleaned
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebacker committed Apr 10, 2020
1 parent cd51ba8 commit 8c3a3b7
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace TypiCMS\Modules\Roles\Http\Controllers;

use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Arr;
use Illuminate\View\View;
use Spatie\Permission\Contracts\Permission;
use TypiCMS\Modules\Core\Http\Controllers\BaseAdminController;
use TypiCMS\Modules\Roles\Http\Requests\FormRequest;
use TypiCMS\Modules\Roles\Models\Role;
Expand Down Expand Up @@ -35,27 +35,34 @@ public function edit(Role $role, $child = null): View

public function store(FormRequest $request): RedirectResponse
{
$data = $request->all();
$roleData = Arr::except($data, ['exit', 'permissions']);
$role = Role::create($roleData);
$permissions = $request->input('permissions', []);
$data = $request->except(['exit', 'permissions']);

if ($role) {
$permissions = isset($data['permissions']) ? $data['permissions'] : [];
$role->syncPermissions($permissions);
}
$this->storeNewPermissions($permissions);

$role = Role::create($data);
$role->syncPermissions($permissions);

return $this->redirect($request, $role);
}

public function update(Role $role, FormRequest $request): RedirectResponse
{
$data = $request->all();
$roleData = Arr::except($data, ['exit', 'permissions']);
$permissions = isset($data['permissions']) ? $data['permissions'] : [];
$permissions = $request->input('permissions', []);
$data = $request->except(['exit', 'permissions']);
$role->update($data);

$this->storeNewPermissions($permissions);
$role->syncPermissions($permissions);
$role->update($roleData);
$role->forgetCachedPermissions();

return $this->redirect($request, $role);
}

private function storeNewPermissions($permissions)
{
foreach ($permissions as $name) {
app(Permission::class)->firstOrCreate(['name' => $name])->id;
}
}
}

0 comments on commit 8c3a3b7

Please sign in to comment.