Skip to content

Commit 7ac838e

Browse files
Added role copying
1 parent fbe10f9 commit 7ac838e

File tree

3 files changed

+116
-25
lines changed

3 files changed

+116
-25
lines changed

src/controllers/UserController.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,42 @@ public function actionDeleteAvatar()
478478
return $this->redirect(['/user/user/profile']);
479479
}
480480

481+
public function actionPasteRoles()
482+
{
483+
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
484+
485+
if ($this->request->isPost) {
486+
$from = Yii::$app->request->post('from');
487+
$to = Yii::$app->request->post('to');
488+
489+
$fromUsers = User::findOne(['id' => $from]);
490+
$toUsers = User::findOne(['id' => $to]);
491+
492+
if (!$fromUsers || !$toUsers) {
493+
return ['result' => 'error', 'massage' => 'Wrong users!'];
494+
}
495+
496+
$rolesToCopy = \ZakharovAndrew\user\models\UserRoles::find()->where(['user_id' => $fromUsers->id])->all();
497+
498+
foreach($rolesToCopy as $role) {
499+
$userRole = new \ZakharovAndrew\user\models\UserRoles([
500+
'user_id' => $toUsers->id,
501+
'role_id' => $role->role_id,
502+
'note' => $role->note,
503+
'subject_id' => $role->subject_id,
504+
]);
505+
506+
$userRole->save();
507+
unset($userRole);
508+
}
509+
510+
return ['result' => 'ok'];
511+
}
512+
513+
return ['result' => 'error'];
514+
}
515+
516+
481517
public function actionUsersUpdate()
482518
{
483519
if (!Yii::$app->user->identity->hasRole('admin')) {

src/messages/ru/user.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
'Successful Attempt' => 'Успешная попытка',
7676
'Too many unsuccessful attempts. Please wait an hour before trying again.' => 'Слишком много неудачных попыток. Пожалуйста, подождите час перед следующей попыткой.',
7777
'Incorrect username or password.' => 'Неверное имя пользователя или пароль.',
78+
'Are you sure you want to delete this user?' => 'Вы уверены, что хотите удалить этого пользователя?',
79+
'Roles Copied' => 'Роли скопированы',
7880
// actions
7981
'Create User' => 'Создать пользователя',
8082
'Save' => 'Сохранить',

src/views/user/index.php

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
$columnVisibility = \ZakharovAndrew\user\models\User::getColumnVisibility();
2828

2929
$toggleUrl = Url::to(['/user/user/toggle-column-visibility']);
30+
$pasteRolesUrl = Url::to(['/user/user/paste-roles']);
3031
$language = \Yii::$app->language;
3132
$waitMessage = Module::t('Processing, please wait..');
3233
$rolesCopiedMessage = Module::t('Roles Copied');
34+
$copyRolesdMessage = Module::t('Copy Roles');
3335

3436
$script = <<< JS
3537
$('#submit-reset-password').on('click', function() {
@@ -104,29 +106,55 @@ function updateSelectedCount() {
104106
105107
106108
$(document).on('click', '.copy-roles', function(e) {
107-
const userId = $(this).data('id');
108-
console.log('Copy roles for user ID:', userId);
109+
localStorage.setItem('copyRolesUserId', $(this).data('id'));
110+
loadCopiedUserId();
109111
110-
localStorage.setItem('copyRolesUserId', userId);
111-
112-
$(this).attr('disabled', true).text('$rolesCopiedMessage');
113-
114-
$('.paste-roles').show();
115-
$(this).parent().find('.paste-roles').hide();
116112
e.preventDefault();
117113
});
118-
119-
let copiedUserId = localStorage.getItem('copyRolesUserId');
120-
if (copiedUserId) {
121-
$('.paste-roles').show();
122-
}
114+
115+
loadCopiedUserId();
123116
124117
$(document).on('click', '.paste-roles', function() {
125-
copiedUserId = localStorage.getItem('copyRolesUserId');
118+
// check disabled
119+
let disabled = $(this).attr("disabled");
120+
if (disabled === 'disabled') {
121+
return false;
122+
}
123+
// copying
124+
let copiedUserId = loadCopiedUserId();
125+
let userId = $(this).data('id');
126126
if (copiedUserId) {
127127
console.log(copiedUserId);
128+
$.ajax({
129+
type: "POST",
130+
url: "$pasteRolesUrl",
131+
data: {
132+
from: copiedUserId,
133+
to: userId
134+
},
135+
success: function(data) {
136+
window.location.reload();
137+
}
138+
});
128139
}
129140
});
141+
142+
function loadCopiedUserId() {
143+
let copiedUserId = localStorage.getItem('copyRolesUserId');
144+
145+
console.log(copiedUserId);
146+
147+
if (copiedUserId) {
148+
console.log(copiedUserId);
149+
$('.paste-roles').show();
150+
$('.copy-roles').attr('disabled', false).text('$copyRolesdMessage');
151+
$('.copy-roles-'+copiedUserId).attr('disabled', true).text('$rolesCopiedMessage');
152+
$('.copy-roles-'+copiedUserId).parent().find('.paste-roles').hide();
153+
}
154+
155+
return copiedUserId;
156+
}
157+
130158
131159
132160
JS;
@@ -257,8 +285,15 @@ function updateSelectedCount() {
257285
background-color: #f06445;color:#fff;
258286
}
259287
.dropdown-menu-action .dropdown-menu {
260-
right: 0;
261-
left: auto;
288+
right: 0 !important;
289+
left: auto !important;
290+
}
291+
.dropdown-menu-action .paste-roles {
292+
display:none;
293+
}
294+
.dropdown-menu-action a[disabled="disabled"] {
295+
pointer-events: none;
296+
color: #898989;
262297
}
263298
</style>
264299
<div class="user-index">
@@ -413,22 +448,40 @@ function updateSelectedCount() {
413448
'items' => [
414449
['label' => Module::t('Profile'), 'url' => Url::toRoute(['profile', 'id' => $model->id])],
415450
['label' => Module::t('Edit'), 'url' => Url::toRoute(['edit-profile', 'id' => $model->id])],
416-
['label' => Module::t('Delete'), 'url' => Url::toRoute(['delete', 'id' => $model->id])],
451+
[
452+
'label' => Module::t('Delete'),
453+
'url' => Url::toRoute(['delete', 'id' => $model->id]),
454+
'linkOptions' => [
455+
'data' => [
456+
'method' => 'post',
457+
'confirm' => Module::t('Are you sure you want to delete this user?'),
458+
],
459+
],
460+
],
417461
'<div class="dropdown-divider divider"></div>',
418462
['label' => Module::t('Appreciation'), 'url' => Url::toRoute(['/user/thanks/view', 'id' => $model->id])],
419463
['label' => Module::t('Reset password'), 'url' => Url::toRoute(['admin-reset-password', 'id' => $model->id])],
420464
'<div class="dropdown-divider divider"></div>',
421465
// roles
422-
['label' => Module::t('Copy Roles'), 'url' => '#', 'options' => [
423-
'class' => 'copy-roles',
424-
'data-id' => $model->id,
425-
]],
426-
['label' => Module::t('Paste Roles'), 'url' => '#', 'options' => [
427-
'class' => 'paste-roles',
428-
'style' => 'display: none;'
429-
]],
466+
[
467+
'label' => Module::t('Copy Roles'),
468+
'url' => '#',
469+
'linkOptions' => [
470+
'class' => 'copy-roles copy-roles-'.$model->id,
471+
'data-id' => $model->id,
472+
],
473+
],
474+
[
475+
'label' => Module::t('Paste Roles'),
476+
'url' => '#',
477+
'linkOptions' => [
478+
'class' => 'paste-roles',
479+
'data-id' => $model->id,
480+
]
481+
],
430482
],
431483
],
484+
432485
]);
433486
},
434487
'contentOptions' => ['class' => 'dropdown-menu-action'],

0 commit comments

Comments
 (0)