Skip to content

Commit 1d2e602

Browse files
Merge pull request #2206 from suraj-webkul/issue#2141
issue #2141 has been fixed.
2 parents 5035e32 + 3ae2281 commit 1d2e602

File tree

5 files changed

+166
-75
lines changed

5 files changed

+166
-75
lines changed

packages/Webkul/Admin/src/Http/Controllers/Settings/UserController.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Http\JsonResponse;
66
use Illuminate\Http\Resources\Json\JsonResource;
7+
use Illuminate\Support\Arr;
78
use Illuminate\Support\Facades\Event;
89
use Illuminate\Support\Facades\Mail;
910
use Illuminate\View\View;
@@ -58,25 +59,24 @@ public function store(): View|JsonResponse
5859
'password' => 'nullable',
5960
'confirm_password' => 'nullable|required_with:password|same:password',
6061
'role_id' => 'required',
62+
'status' => 'boolean|in:0,1',
63+
'view_permission' => 'string|in:global,group,individual',
6164
]);
6265

6366
$data = request()->all();
6467

65-
if (isset($data['password']) && $data['password']) {
68+
if (
69+
isset($data['password'])
70+
&& $data['password']
71+
) {
6672
$data['password'] = bcrypt($data['password']);
6773
}
6874

69-
$data['status'] = $data['status'] ? 1 : 0;
70-
7175
Event::dispatch('settings.user.create.before');
7276

7377
$admin = $this->userRepository->create($data);
7478

75-
$admin->view_permission = $data['view_permission'];
76-
77-
$admin->save();
78-
79-
$admin->groups()->sync(request('groups') ?? []);
79+
$admin->groups()->sync($data['groups'] ?? []);
8080

8181
try {
8282
Mail::queue(new UserCreatedNotification($admin));
@@ -111,33 +111,33 @@ public function update(int $id): JsonResponse
111111
{
112112
$this->validate(request(), [
113113
'email' => 'required|email|unique:users,email,'.$id,
114-
'name' => 'required',
115-
'password' => 'nullable',
114+
'name' => 'required|string',
115+
'password' => 'nullable|string|min:6',
116116
'confirm_password' => 'nullable|required_with:password|same:password',
117-
'role_id' => 'required',
117+
'role_id' => 'required|integer|exists:roles,id',
118+
'status' => 'nullable|boolean|in:0,1',
119+
'view_permission' => 'required|string|in:global,group,individual',
118120
]);
119121

120122
$data = request()->all();
121123

122-
if (! $data['password']) {
123-
unset($data['password'], $data['confirm_password']);
124+
if (empty($data['password'])) {
125+
$data = Arr::except($data, ['password', 'confirm_password']);
124126
} else {
125127
$data['password'] = bcrypt($data['password']);
126128
}
127129

128-
if (auth()->guard('user')->user()->id != $id) {
129-
$data['status'] = $data['status'] ? 1 : 0;
130+
$authUser = auth()->guard('user')->user();
131+
132+
if ($authUser->id == $id) {
133+
$data['status'] = true;
130134
}
131135

132136
Event::dispatch('settings.user.update.before', $id);
133137

134138
$admin = $this->userRepository->update($data, $id);
135139

136-
$admin->view_permission = $data['view_permission'];
137-
138-
$admin->save();
139-
140-
$admin->groups()->sync(request()->input('groups') ?? []);
140+
$admin->groups()->sync($data['groups'] ?? []);
141141

142142
Event::dispatch('settings.user.update.after', $admin);
143143

packages/Webkul/Admin/src/Resources/lang/en/app.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,24 +1243,25 @@
12431243
],
12441244

12451245
'create' => [
1246-
'confirm-password' => 'Confirm Password',
1247-
'email' => 'Email',
1248-
'general' => 'General',
1249-
'global' => 'Global',
1250-
'group' => 'Group',
1251-
'individual' => 'Individual',
1252-
'name' => 'Name',
1253-
'password' => 'Password',
1254-
'permission' => 'Permission',
1255-
'role' => 'Role',
1256-
'save-btn' => 'Save User',
1257-
'status' => 'Status',
1258-
'title' => 'Create User',
1259-
'view-permission' => 'View Permission',
1246+
'confirm-password' => 'Confirm Password',
1247+
'email' => 'Email',
1248+
'general' => 'General',
1249+
'global' => 'Global',
1250+
'group' => 'Group',
1251+
'individual' => 'Individual',
1252+
'name' => 'Name',
1253+
'password' => 'Password',
1254+
'permission' => 'Permission',
1255+
'role' => 'Role',
1256+
'save-btn' => 'Save User',
1257+
'status' => 'Status',
1258+
'title' => 'Create User',
1259+
'view-permission' => 'View Permission',
1260+
'select-at-lest-one-group' => 'Select at least one group',
12601261
],
12611262

12621263
'edit' => [
1263-
'title' => 'Edit User',
1264+
'title' => 'Edit User',
12641265
],
12651266
],
12661267
],

packages/Webkul/Admin/src/Resources/views/settings/users/index.blade.php

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ class="cursor-pointer rounded-md p-1.5 text-2xl transition-all hover:bg-gray-200
369369
name="view_permission"
370370
rules="required"
371371
v-model="user.view_permission"
372+
value="global"
372373
:label="trans('admin::app.settings.users.index.create.view-permission')"
373374
>
374375
<!-- Default Option -->
@@ -393,37 +394,39 @@ class="cursor-pointer rounded-md p-1.5 text-2xl transition-all hover:bg-gray-200
393394
394395
{!! view_render_event('admin.settings.users.index.form.role_id.before') !!}
395396
396-
<!-- Group -->
397-
<x-admin::form.control-group>
398-
<x-admin::form.control-group.label class="required">
399-
@lang('admin::app.settings.users.index.create.group')
400-
</x-admin::form.control-group.label>
397+
<template v-if="user.view_permission === 'group'">
398+
<!-- Group -->
399+
<x-admin::form.control-group>
400+
<x-admin::form.control-group.label class="required">
401+
@lang('admin::app.settings.users.index.create.group')
402+
</x-admin::form.control-group.label>
401403
402-
<v-field
403-
name="groups[]"
404-
rules="required"
405-
label="@lang('admin::app.settings.users.index.create.group')"
406-
multiple
407-
v-model="user.groups"
408-
>
409-
<select
404+
<v-field
410405
name="groups[]"
411-
class="flex min-h-[39px] w-full rounded-md border px-3 py-2 text-sm text-gray-600 transition-all hover:border-gray-400 focus:border-gray-400 dark:border-gray-800 dark:bg-gray-900 dark:text-gray-300 dark:hover:border-gray-400 dark:focus:border-gray-400"
412-
:class="[errors['groups[]'] ? 'border !border-red-600 hover:border-red-600' : '']"
406+
label="@lang('admin::app.settings.users.index.create.group')"
413407
multiple
414408
v-model="user.groups"
409+
rules="required"
415410
>
416-
<option
417-
v-for="group in groups"
418-
:value="group.id"
419-
:text="group.name"
411+
<select
412+
name="groups[]"
413+
class="flex min-h-[39px] w-full rounded-md border px-3 py-2 text-sm text-gray-600 transition-all hover:border-gray-400 focus:border-gray-400 dark:border-gray-800 dark:bg-gray-900 dark:text-gray-300 dark:hover:border-gray-400 dark:focus:border-gray-400"
414+
:class="[errors['groups[]'] ? 'border !border-red-600 hover:border-red-600' : '']"
415+
multiple
416+
v-model="user.groups"
420417
>
421-
</option>
422-
</select>
423-
</v-field>
418+
<option
419+
v-for="group in groups"
420+
:value="group.id"
421+
:text="group.name"
422+
>
423+
</option>
424+
</select>
425+
</v-field>
424426
425-
<x-admin::form.control-group.error name="groups[]" />
426-
</x-admin::form.control-group>
427+
<x-admin::form.control-group.error name="groups[]" />
428+
</x-admin::form.control-group>
429+
</template>
427430
428431
{!! view_render_event('admin.settings.users.index.form.role_id.after') !!}
429432
@@ -487,7 +490,9 @@ class="primary-button justify-center"
487490
488491
groups: @json($groups),
489492
490-
user: {},
493+
user: {
494+
view_permission: 'global',
495+
},
491496
};
492497
},
493498
@@ -545,23 +550,28 @@ class="primary-button justify-center"
545550
546551
this.isProcessing = true;
547552
548-
this.$axios.post(params.id ? `{{ route('admin.settings.users.update', '') }}/${params.id}` : "{{ route('admin.settings.users.store') }}", userForm).then(response => {
549-
this.isProcessing = false;
553+
this.$axios.post(
554+
params.id
555+
? `{{ route('admin.settings.users.update', '') }}/${params.id}`
556+
: "{{ route('admin.settings.users.store') }}", userForm
557+
)
558+
.then(response => {
559+
this.isProcessing = false;
550560
551-
this.$refs.userUpdateAndCreateModal.toggle();
561+
this.$refs.userUpdateAndCreateModal.toggle();
552562
553-
this.$emitter.emit('add-flash', { type: 'success', message: response.data.message });
563+
this.$emitter.emit('add-flash', { type: 'success', message: response.data.message });
554564
555-
this.$refs.datagrid.get();
565+
this.$refs.datagrid.get();
556566
557-
resetForm();
558-
}).catch(error => {
559-
this.isProcessing = false;
567+
resetForm();
568+
}).catch(error => {
569+
this.isProcessing = false;
560570
561-
if (error.response.status === 422) {
562-
setErrors(error.response.data.errors);
563-
}
564-
});
571+
if (error.response.status === 422) {
572+
setErrors(error.response.data.errors);
573+
}
574+
});
565575
},
566576
567577
editModal(url) {

packages/Webkul/Admin/tests/e2e-pw/tests/settings/user/users.spec.ts

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,47 @@ async function createGroup(adminPage) {
4242
}
4343

4444
test.describe("user management", () => {
45-
test("should create a user", async ({ adminPage }) => {
45+
test("should create a user with global permission", async ({ adminPage }) => {
46+
/**
47+
* Reaching to the user listing page.
48+
*/
49+
await adminPage.goto("admin/settings/users");
50+
51+
/**
52+
* Opening create user form in modal.
53+
*/
54+
await adminPage.getByRole("button", { name: "Create User" }).click();
55+
56+
/**
57+
* Filling the form with user details.
58+
*/
59+
await adminPage.locator('input[name="name"]').fill(generateFullName());
60+
await adminPage.locator('input[name="email"]').fill(generateEmail());
61+
await adminPage.locator('input[name="password"]').fill("admin123");
62+
await adminPage
63+
.locator('input[name="confirm_password"]')
64+
.fill("admin123");
65+
await adminPage.locator('select[name="role_id"]').selectOption("1");
66+
await adminPage
67+
.locator('select[name="view_permission"]')
68+
.selectOption("global");
69+
70+
/**
71+
* Clicking on the status toggler to make the user active.
72+
*/
73+
await adminPage.click('label[for="status"]');
74+
75+
/**
76+
* Save user and close the modal.
77+
*/
78+
await adminPage.getByRole("button", { name: "Save User" }).click();
79+
80+
await expect(
81+
adminPage.getByText("User created successfully.")
82+
).toBeVisible();
83+
});
84+
85+
test("should create a user with group permission", async ({ adminPage }) => {
4686
/**
4787
* Creating a group to assign to the user.
4888
*/
@@ -70,8 +110,7 @@ test.describe("user management", () => {
70110
await adminPage.locator('select[name="role_id"]').selectOption("1");
71111
await adminPage
72112
.locator('select[name="view_permission"]')
73-
.selectOption("global");
74-
// await adminPage.locator('select[name="groups[]"]').selectOption("1");
113+
.selectOption("group");
75114
await adminPage.getByRole('listbox').selectOption({ label: name.groupName });
76115

77116
/**
@@ -89,6 +128,46 @@ test.describe("user management", () => {
89128
).toBeVisible();
90129
});
91130

131+
test("should create a user with individual permission", async ({ adminPage }) => {
132+
/**
133+
* Reaching to the user listing page.
134+
*/
135+
await adminPage.goto("admin/settings/users");
136+
137+
/**
138+
* Opening create user form in modal.
139+
*/
140+
await adminPage.getByRole("button", { name: "Create User" }).click();
141+
142+
/**
143+
* Filling the form with user details.
144+
*/
145+
await adminPage.locator('input[name="name"]').fill(generateFullName());
146+
await adminPage.locator('input[name="email"]').fill(generateEmail());
147+
await adminPage.locator('input[name="password"]').fill("admin123");
148+
await adminPage
149+
.locator('input[name="confirm_password"]')
150+
.fill("admin123");
151+
await adminPage.locator('select[name="role_id"]').selectOption("1");
152+
await adminPage
153+
.locator('select[name="view_permission"]')
154+
.selectOption("individual");
155+
156+
/**
157+
* Clicking on the status toggler to make the user active.
158+
*/
159+
await adminPage.click('label[for="status"]');
160+
161+
/**
162+
* Save user and close the modal.
163+
*/
164+
await adminPage.getByRole("button", { name: "Save User" }).click();
165+
166+
await expect(
167+
adminPage.getByText("User created successfully.")
168+
).toBeVisible();
169+
});
170+
92171
test("should edit a users", async ({ adminPage }) => {
93172
/**
94173
* Generating new name and email for the user.

packages/Webkul/User/src/Models/User.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class User extends Authenticatable implements UserContract
2525
'api_token',
2626
'role_id',
2727
'status',
28+
'view_permission',
2829
];
2930

3031
/**

0 commit comments

Comments
 (0)