-
Notifications
You must be signed in to change notification settings - Fork 110
/
admin_grant_permissions.js
108 lines (103 loc) · 3.74 KB
/
admin_grant_permissions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { isObject, isString } from '../../utils/isType';
import { renderNotice, renderAlert, hideNotifications } from '../../utils/notificationHelper';
import { scrollTo } from '../../utils/scrollTo';
$(() => {
// Activate/Deactivate user account
$('body').on('click, change', '.activate-user input[type="checkbox"]', (e) => {
const form = $(e.target).closest('form');
hideNotifications();
form.submit();
});
$('body').on('ajax:success', '.activate-user', (e, data) => {
if (data.code === 1 && data.msg && data.msg !== '') {
renderNotice(data.msg);
} else {
renderAlert(data.msg);
}
});
$('body').on('ajax:error', '.activate-user', () => {
renderAlert('Unexpected error');
});
let currentPrivileges = null;
$('body').on('click', 'a[href$="admin_grant_permissions"]', (e) => {
e.preventDefault();
const target = $(e.target);
currentPrivileges = target.closest('td').siblings('td[data-descriptor="current_privileges"]');
$.ajax({
method: 'GET',
url: target.attr('href'),
}).done((data) => {
$('#modal-permissions').html(data.user.html);
if ($('.org_grant_privileges:checked').length === $('.org_grant_privileges').length) {
$('#org_admin_privileges').prop('checked', true);
}
if ($('.super_grant_privileges:checked').length === $('.super_grant_privileges').length) {
$('#super_admin_privileges').prop('checked', true);
}
}).fail((xhr) => {
const error = xhr.responseJSON;
if (isObject(error) && isString(error.msg)) {
$('#modal-permissions').html(error.msg);
} else {
$('#modal-permissions').html('Unexpected error');
}
}).always(() => {
// The modal is deferred until a successful response is got
$('#modal-permissions').modal('show');
});
});
// Event delegation handler after a successful response is obtained
$('body').on('ajax:success', '.admin_update_permissions', (e, data) => {
if (isObject(data)) {
if (isString(data.msg)) {
renderNotice(data.msg);
scrollTo('#notification-area');
}
if (isString(data.current_privileges) && currentPrivileges) {
currentPrivileges.html(data.current_privileges);
}
}
$('#modal-permissions').modal('hide');
});
// Event delegation handler after an error response is obtained
$('body').on('ajax:error', '.admin_update_permissions', (e, xhr) => {
const error = xhr.responseJSON;
if (isObject(error) && isString(error.msg)) {
renderAlert(error.msg);
scrollTo('#notification-area');
}
$('#modal-permissions').modal('hide');
});
});
$(() => {
$('body').on('click', '#org_admin_privileges', () => {
if ($('#org_admin_privileges').prop('checked')) {
$('.org_grant_privileges:checkbox').prop('checked', true);
} else {
$('.org_grant_privileges:checkbox').prop('checked', false);
}
});
$('body').on('change', '.org_grant_privileges', () => {
if ($('.org_grant_privileges:checked').length === $('.org_grant_privileges').length) {
$('#org_admin_privileges').prop('checked', true);
} else {
$('#org_admin_privileges').prop('checked', false);
}
});
});
$(() => {
$('body').on('click', '#super_admin_privileges', () => {
if ($('#super_admin_privileges').prop('checked')) {
$('.super_grant_privileges:checkbox').prop('checked', true);
} else {
$('.super_grant_privileges:checkbox').prop('checked', false);
}
});
$('body').on('change', '.super_grant_privileges', () => {
if ($('.super_grant_privileges:checked').length === $('.super_grant_privileges').length) {
$('#super_admin_privileges').prop('checked', true);
} else {
$('#super_admin_privileges').prop('checked', false);
}
});
});