fix: dont allow adding duplicates to privileges page

hightlight uid/group if it is added again from autocomplete
v1.18.x
Barış Soner Uşaklı 5 years ago
parent f259063f08
commit 12c8b1c2bc

@ -4,7 +4,7 @@
100% {background-color: white;} 100% {background-color: white;}
} }
[data-group-name].selected { [data-group-name].selected, [data-uid].selected {
animation-name: fadeOut; animation-name: fadeOut;
animation-duration: 5s; animation-duration: 5s;
animation-fill-mode: both; animation-fill-mode: both;

@ -147,7 +147,7 @@ define('admin/manage/privileges', [
$('.privilege-table-container').html(html); $('.privilege-table-container').html(html);
Privileges.exposeAssumedPrivileges(); Privileges.exposeAssumedPrivileges();
hightlightRowByGroupName(groupToHighlight); hightlightRowByDataAttr('data-group-name', groupToHighlight);
}); });
}); });
}); });
@ -215,29 +215,7 @@ define('admin/manage/privileges', [
inputEl.focus(); inputEl.focus();
autocomplete.user(inputEl, function (ev, ui) { autocomplete.user(inputEl, function (ev, ui) {
// Generate data for new row addUserToCategory(ui.item.user, function () {
var privilegeSet = ajaxify.data.privileges.keys.users.reduce(function (memo, cur) {
memo[cur] = false;
return memo;
}, {});
app.parseAndTranslate('admin/partials/privileges/' + (isNaN(cid) ? 'global' : 'category'), 'privileges.users', {
privileges: {
users: [
{
picture: ui.item.user.picture,
username: ui.item.user.username,
uid: ui.item.user.uid,
'icon:text': ui.item.user['icon:text'],
'icon:bgColor': ui.item.user['icon:bgColor'],
privileges: privilegeSet,
},
],
},
}, function (html) {
var tableEl = document.querySelectorAll('.privilege-table');
var rows = tableEl[1].querySelectorAll('tbody tr');
html.insertBefore(rows[rows.length - 1]);
modal.modal('hide'); modal.modal('hide');
}); });
}); });
@ -298,11 +276,12 @@ define('admin/manage/privileges', [
}); });
}; };
function hightlightRowByGroupName(groupName) { function hightlightRowByDataAttr(attrName, attrValue) {
if (groupName) { if (attrValue) {
var el = $('[data-group-name]').filter(function () { var el = $('[' + attrName + ']').filter(function () {
return $(this).attr('data-group-name') === groupName; return $(this).attr(attrName) === String(attrValue);
}); });
if (el.length) { if (el.length) {
el.addClass('selected'); el.addClass('selected');
return true; return true;
@ -313,7 +292,7 @@ define('admin/manage/privileges', [
function highlightRow() { function highlightRow() {
if (ajaxify.data.group) { if (ajaxify.data.group) {
if (hightlightRowByGroupName(ajaxify.data.group)) { if (hightlightRowByDataAttr('data-group-name', ajaxify.data.group)) {
return; return;
} }
addGroupToCategory(ajaxify.data.group); addGroupToCategory(ajaxify.data.group);
@ -321,6 +300,12 @@ define('admin/manage/privileges', [
} }
function addGroupToCategory(group, cb) { function addGroupToCategory(group, cb) {
cb = cb || function () {};
var groupRow = document.querySelector('.privilege-table [data-group-name="' + group + '"]');
if (groupRow) {
hightlightRowByDataAttr('data-group-name', group);
return cb();
}
// Generate data for new row // Generate data for new row
var privilegeSet = ajaxify.data.privileges.keys.groups.reduce(function (memo, cur) { var privilegeSet = ajaxify.data.privileges.keys.groups.reduce(function (memo, cur) {
memo[cur] = false; memo[cur] = false;
@ -342,10 +327,43 @@ define('admin/manage/privileges', [
var rows = tableEl.querySelectorAll('tbody tr'); var rows = tableEl.querySelectorAll('tbody tr');
html.insertBefore(rows[rows.length - 1]); html.insertBefore(rows[rows.length - 1]);
Privileges.exposeAssumedPrivileges(); Privileges.exposeAssumedPrivileges();
hightlightRowByDataAttr('data-group-name', group);
cb();
});
}
if (typeof cb === 'function') { function addUserToCategory(user, cb) {
cb(); cb = cb || function () {};
} var userRow = document.querySelector('.privilege-table [data-uid="' + user.uid + '"]');
if (userRow) {
hightlightRowByDataAttr('data-uid', user.uid);
return cb();
}
// Generate data for new row
var privilegeSet = ajaxify.data.privileges.keys.users.reduce(function (memo, cur) {
memo[cur] = false;
return memo;
}, {});
app.parseAndTranslate('admin/partials/privileges/' + (isNaN(cid) ? 'global' : 'category'), 'privileges.users', {
privileges: {
users: [
{
picture: user.picture,
username: user.username,
uid: user.uid,
'icon:text': user['icon:text'],
'icon:bgColor': user['icon:bgColor'],
privileges: privilegeSet,
},
],
},
}, function (html) {
var tableEl = document.querySelectorAll('.privilege-table');
var rows = tableEl[1].querySelectorAll('tbody tr');
html.insertBefore(rows[rows.length - 1]);
hightlightRowByDataAttr('data-uid', user.uid);
cb();
}); });
} }

Loading…
Cancel
Save