Skip to content

Commit

Permalink
fixed roles page routes
Browse files Browse the repository at this point in the history
  • Loading branch information
shivendra-webkul committed Jul 22, 2024
1 parent 0d05af1 commit d96290d
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Webkul\Admin\Http\Controllers\Settings;

use Illuminate\Http\JsonResponse;
use Illuminate\View\View;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Event;
use Illuminate\View\View;
use Webkul\Admin\DataGrids\Setting\GroupDataGrid;
use Webkul\Admin\Http\Controllers\Controller;
use Webkul\User\Repositories\GroupRepository;
use Webkul\Admin\DataGrids\Setting\GroupDataGrid;

class GroupController extends Controller
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Webkul\Admin\Http\Controllers\Settings;

use Illuminate\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Event;
use Illuminate\View\View;
use Webkul\Admin\DataGrids\Setting\RoleDataGrid;
use Webkul\Admin\Http\Controllers\Controller;
use Webkul\User\Repositories\RoleRepository;
use Webkul\User\Repositories\UserRepository;
use Webkul\Admin\Http\Controllers\Controller;
use Webkul\Admin\DataGrids\Setting\RoleDataGrid;

class RoleController extends Controller
{
Expand All @@ -16,12 +17,13 @@ class RoleController extends Controller
*
* @return void
*/
public function __construct(protected RoleRepository $roleRepository) {}

public function __construct(
protected RoleRepository $roleRepository,
protected UserRepository $userRepository
) {}

/**
* Display a listing of the resource.
*
* @return \Illuminate\View\View
*/
public function index(): View|JsonResponse
{
Expand Down Expand Up @@ -52,34 +54,37 @@ public function store()
$this->validate(request(), [
'name' => 'required',
'permission_type' => 'required',
'description' => 'required',
]);

Event::dispatch('settings.role.create.before');

$roleData = request()->all();
$data = request()->only([
'name',
'description',
'permission_type',
'permissions',
]);

if ($roleData['permission_type'] == 'custom') {
if (! isset($roleData['permissions'])) {
$roleData['permissions'] = [];
if ($data['permission_type'] == 'custom') {
if (! isset($data['permissions'])) {
$data['permissions'] = [];
}
}

$role = $this->roleRepository->create($roleData);
$role = $this->roleRepository->create($data);

Event::dispatch('settings.role.create.after', $role);

session()->flash('success', trans('admin::app.settings.roles.create-success'));
session()->flash('success', trans('admin::app.settings.roles.index.create-success'));

return redirect()->route('admin.settings.roles.index');
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\View\View
*/
public function edit($id)
public function edit(int $id): View
{
$role = $this->roleRepository->findOrFail($id);

Expand All @@ -89,42 +94,41 @@ public function edit($id)
/**
* Update the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update($id)
public function update(int $id)
{
$this->validate(request(), [
'name' => 'required',
'permission_type' => 'required',
'permission_type' => 'required|in:all,custom',
'description' => 'required',
]);

Event::dispatch('settings.role.update.before', $id);

$roleData = request()->all();

if ($roleData['permission_type'] == 'custom') {
if (! isset($roleData['permissions'])) {
$roleData['permissions'] = [];
}
}
$data = array_merge(request()->only([
'name',
'description',
'permission_type',
]), [
'permissions' => request()->has('permissions') ? request('permissions') : [],
]);

$role = $this->roleRepository->update($roleData, $id);
$role = $this->roleRepository->update($data, $id);

Event::dispatch('settings.role.update.after', $role);

session()->flash('success', trans('admin::app.settings.roles.update-success'));
session()->flash('success', trans('admin::app.settings.roles.index.update-success'));

return redirect()->route('admin.settings.roles.index');
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
public function destroy(int $id)
{
$response = [
'responseCode' => 400,
Expand All @@ -133,25 +137,25 @@ public function destroy($id)
$role = $this->roleRepository->findOrFail($id);

if ($role->admins && $role->admins->count() >= 1) {
$response['message'] = trans('admin::app.settings.roles.being-used');
$response['message'] = trans('admin::app.settings.roles.index.being-used');

session()->flash('error', $response['message']);
} elseif ($this->roleRepository->count() == 1) {
$response['message'] = trans('admin::app.settings.roles.last-delete-error');
$response['message'] = trans('admin::app.settings.roles.index.last-delete-error');

session()->flash('error', $response['message']);
} else {
try {
Event::dispatch('settings.role.delete.before', $id);

if (auth()->guard('user')->user()->role_id == $id) {
$response['message'] = trans('admin::app.settings.roles.current-role-delete-error');
$response['message'] = trans('admin::app.settings.roles.index.current-role-delete-error');
} else {
$this->roleRepository->delete($id);

Event::dispatch('settings.role.delete.after', $id);

$message = trans('admin::app.settings.roles.delete-success');
$message = trans('admin::app.settings.roles.index.delete-success');

$response = [
'responseCode' => 200,
Expand Down
16 changes: 0 additions & 16 deletions packages/Webkul/Admin/src/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,6 @@

Route::get('', 'SettingController@index')->name('admin.settings.index');

// Roles Routes
Route::prefix('roles')->group(function () {
Route::get('', 'RoleController@index')->name('admin.settings.roles.index');

Route::get('create', 'RoleController@create')->name('admin.settings.roles.create');

Route::post('create', 'RoleController@store')->name('admin.settings.roles.store');

Route::get('edit/{id}', 'RoleController@edit')->name('admin.settings.roles.edit');

Route::put('edit/{id}', 'RoleController@update')->name('admin.settings.roles.update');

Route::delete('{id}', 'RoleController@destroy')->name('admin.settings.roles.delete');
});

// Users Routes
Route::prefix('users')->group(function () {
Route::get('', 'UserController@index')->name('admin.settings.users.index');
Expand Down Expand Up @@ -337,7 +322,6 @@
Route::delete('{id}', 'SourceController@destroy')->name('admin.settings.sources.delete');
});


// Email Templates Routes
Route::prefix('email-templates')->group(function () {
Route::get('', 'EmailTemplateController@index')->name('admin.settings.email_templates.index');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Illuminate\Foundation\AliasLoader;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Webkul\Admin\Http\Middleware\Locale;
use Illuminate\Support\Facades\Route;

class AdminServiceProvider extends ServiceProvider
{
Expand Down
16 changes: 13 additions & 3 deletions packages/Webkul/Admin/src/Resources/lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
'index' => [
'title' => 'Products',
'create-btn' => 'Create Product',

'datagrid' => [
'allocated' => 'Allocated',
'delete' => 'Delete',
Expand Down Expand Up @@ -167,9 +168,18 @@

'roles' => [
'index' => [
'create-btn' => 'Create Roles',
'title' => 'Roles',
'settings' => 'Settings',
'being-used' => 'Role can not be deleted, as this is being used in admin user.',
'create-btn' => 'Create Roles',
'create-success' => 'Role created successfully.',
'current-role-delete-error' => 'Can not delete role assigned to the current user.',
'delete-failed' => 'Role can not be deleted.',
'delete-success' => 'Role deleted successfully.',
'last-delete-error' => 'At least one role is required.',
'settings' => 'Settings',
'title' => 'Roles',
'update-success' => 'Role updated successfully.',
'user-define-error' => 'Can not delete system role.',

'datagrid' => [
'all' => 'All',
'custom' => 'Custom',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
@lang('admin::app.settings.roles.create.title')
</x-slot>

{!! view_render_event('krayin.settings.roles.create.before') !!}
{!! view_render_event('krayin.admin.settings.roles.create.before') !!}

<!-- Create Form -->
<x-admin::form :action="route('admin.settings.roles.store')">

{!! view_render_event('krayin.settings.roles.create.create_form_controls.before') !!}
{!! view_render_event('krayin.admin.settings.roles.create.form_controls.before') !!}

<div class="flex items-center justify-between">
<p class="text-xl font-bold text-gray-800 dark:text-white">
Expand Down Expand Up @@ -38,8 +39,7 @@ class="primary-button"
<div class="mt-3.5 flex gap-2.5 max-xl:flex-wrap">
<!-- Left sub-component -->
<div class="flex flex-1 flex-col gap-2 max-xl:flex-auto">

{!! view_render_event('krayin.settings.roles.create.card.access_control.before') !!}
{!! view_render_event('krayin.admin.settings.roles.create.card.access_control.before') !!}

<!-- Access Control Input Fields -->
<div class="box-shadow rounded bg-white p-4 dark:bg-gray-900">
Expand All @@ -57,14 +57,13 @@ class="primary-button"
</v-access-control>
</div>

{!! view_render_event('krayin.settings.roles.create.card.access_control.after') !!}

{!! view_render_event('krayin.admin.settings.roles.create.card.access_control.after') !!}
</div>

<!-- Right sub-component -->
<div class="flex w-[360px] max-w-full flex-col gap-2 max-sm:w-full">

{!! view_render_event('krayin.settings.roles.create.card.accordion.general.before') !!}
{!! view_render_event('krayin.admin.settings.roles.create.card.accordion.general.before') !!}

<x-admin::accordion>
<x-slot:header>
Expand Down Expand Up @@ -116,16 +115,14 @@ class="primary-button"
</x-slot>
</x-admin::accordion>

{!! view_render_event('krayin.settings.roles.create.card.accordion.general.after') !!}

{!! view_render_event('krayin.admin.settings.roles.create.card.accordion.general.after') !!}
</div>
</div>

{!! view_render_event('krayin.settings.roles.create.create_form_controls.after') !!}

{!! view_render_event('krayin.admin.settings.roles.create.create_form_controls.after') !!}
</x-admin::form>

{!! view_render_event('krayin.settings.roles.create.after') !!}
{!! view_render_event('krayin.admin.settings.roles.create.after') !!}

@pushOnce('scripts')
<script
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
@lang('admin::app.settings.roles.edit.title')
</x-slot>

{!! view_render_event('krayin.settings.roles.edit.before', ['role' => $role]) !!}
{!! view_render_event('krayin.admin.settings.roles.edit.before', ['role' => $role]) !!}

<x-admin::form
method="PUT"
:action="route('admin.settings.roles.update', $role->id)"
>

{!! view_render_event('krayin.settings.roles.edit.edit_form_controls.before', ['role' => $role]) !!}
{!! view_render_event('krayin.admin.settings.roles.edit.form_controls.before', ['role' => $role]) !!}

<div class="flex items-center justify-between">
<p class="text-xl font-bold text-gray-800 dark:text-white">
Expand Down Expand Up @@ -42,7 +42,7 @@ class="primary-button"
<!-- Left sub-component -->
<div class="flex flex-1 flex-col gap-2 max-xl:flex-auto">

{!! view_render_event('krayin.settings.roles.edit.card.access-control.before', ['role' => $role]) !!}
{!! view_render_event('krayin.admin.settings.roles.edit.card.access_control.before', ['role' => $role]) !!}

<!-- Access Control Input Fields -->
<div class="box-shadow rounded bg-white p-4 dark:bg-gray-900">
Expand All @@ -64,14 +64,12 @@ class="primary-button"
</v-access-control>
</div>

{!! view_render_event('krayin.settings.roles.edit.card.access-control.after', ['role' => $role]) !!}

{!! view_render_event('krayin.admin.settings.roles.edit.card.access_control.after', ['role' => $role]) !!}
</div>

<!-- Right sub-component -->
<div class="flex w-[360px] max-w-full flex-col gap-2 max-sm:w-full">

{!! view_render_event('krayin.settings.roles.edit.card.accordion.general.before', ['role' => $role]) !!}
{!! view_render_event('krayin.admin.settings.roles.edit.card.accordion.general.before', ['role' => $role]) !!}

<x-admin::accordion>
<x-slot:header>
Expand Down Expand Up @@ -123,16 +121,15 @@ class="primary-button"
</x-slot>
</x-admin::accordion>

{!! view_render_event('krayin.settings.roles.edit.card.accordion.general.after', ['role' => $role]) !!}

{!! view_render_event('krayin.admin.settings.roles.edit.card.accordion.general.after', ['role' => $role]) !!}
</div>
</div>

{!! view_render_event('krayin.settings.roles.edit.edit_form_controls.after', ['role' => $role]) !!}
{!! view_render_event('krayin..admin.settings.roles.edit.form_controls.after', ['role' => $role]) !!}

</x-admin::form>

{!! view_render_event('krayin.settings.roles.edit.after', ['role' => $role]) !!}
{!! view_render_event('krayin.admin.settings.roles.edit.after', ['role' => $role]) !!}

@pushOnce('scripts')
<script
Expand Down
Loading

0 comments on commit d96290d

Please sign in to comment.