refactor: using cid 0 in admin privs

This commit is contained in:
Julian Lam
2020-06-02 11:53:22 -04:00
parent f14eaf5c2f
commit 7ac966aa8d
8 changed files with 20 additions and 134 deletions

View File

@@ -77,17 +77,12 @@ define('admin/manage/privileges', [
};
Privileges.refreshPrivilegeTable = function () {
socket.emit('admin.categories.getPrivilegeSettings', cid, function (err, privileges) {
socket.emit('admin.categories.getPrivilegeSettings', ajaxify.data.admin ? 'admin' : cid, function (err, privileges) {
if (err) {
return app.alertError(err.message);
}
var tpl;
if (cid !== 'admin') {
tpl = cid ? 'admin/partials/privileges/category' : 'admin/partials/privileges/global';
} else {
tpl = 'admin/partials/privileges/admin';
}
var tpl = cid ? 'admin/partials/privileges/category' : 'admin/partials/privileges/global';
Benchpress.parse(tpl, {
privileges: privileges,
}, function (html) {
@@ -150,7 +145,7 @@ define('admin/manage/privileges', [
autocomplete.user(inputEl, function (ev, ui) {
var defaultPrivileges;
if (cid === 'admin') {
if (ajaxify.url === '/admin/manage/privileges/admin') {
defaultPrivileges = ['manage:categories'];
} else {
defaultPrivileges = cid ? ['find', 'read', 'topics:read'] : ['chat'];
@@ -184,7 +179,7 @@ define('admin/manage/privileges', [
autocomplete.group(inputEl, function (ev, ui) {
var defaultPrivileges;
if (cid === 'admin') {
if (ajaxify.url === '/admin/manage/privileges/admin') {
defaultPrivileges = ['groups:manage:categories'];
} else {
defaultPrivileges = cid ? ['groups:find', 'groups:read', 'groups:topics:read'] : ['groups:chat'];

View File

@@ -6,23 +6,14 @@ const privileges = require('../../privileges');
const privilegesController = module.exports;
privilegesController.get = async function (req, res) {
const cid = req.params.cid ? parseInt(req.params.cid, 10) || 'admin' : 0;
const cid = req.params.cid ? parseInt(req.params.cid, 10) || 0 : 0;
const isAdminPriv = req.params.cid === 'admin';
let method;
const type = {
global: false,
admin: false,
cid: false,
};
if (cid > 0) {
method = privileges.categories.list.bind(null, cid);
type.cid = true;
} else if (cid === 0) {
method = privileges.global.list;
type.global = true;
} else {
method = privileges.admin.list;
type.admin = true;
method = isAdminPriv ? privileges.admin.list : privileges.global.list;
}
const [privilegesData, categoriesData] = await Promise.all([
@@ -34,9 +25,8 @@ privilegesController.get = async function (req, res) {
cid: 0,
name: '[[admin/manage/privileges:global]]',
icon: 'fa-list',
});
categoriesData.unshift({
cid: 'admin',
}, {
cid: 'admin', // what do?
name: '[[admin/manage/privileges:admin]]',
icon: 'fa-lock',
});
@@ -53,10 +43,10 @@ privilegesController.get = async function (req, res) {
});
res.render('admin/manage/privileges', {
type: type,
privileges: privilegesData,
categories: categoriesData,
selectedCategory: selectedCategory,
cid: cid,
admin: isAdminPriv,
});
};

View File

@@ -73,7 +73,6 @@ module.exports = function (Groups) {
}
Groups.validateGroupName = function (name) {
console.log(name);
if (!name) {
throw new Error('[[error:group-name-too-short]]');
}

View File

@@ -46,7 +46,7 @@ Groups.removeEphemeralGroups = function (groups) {
return groups;
};
var isPrivilegeGroupRegex = /^cid:(?:\d+|admin):privileges:[\w:]+$/;
var isPrivilegeGroupRegex = /^cid:\d+:privileges:[\w:]+$/;
Groups.isPrivilegeGroup = function (groupName) {
return isPrivilegeGroupRegex.test(groupName);
};

View File

@@ -31,8 +31,8 @@ module.exports = function (privileges) {
}
const payload = await utils.promiseParallel({
labels: getLabels(),
users: helpers.getUserPrivileges('admin', 'filter:privileges.admin.list', privileges.admin.userPrivilegeList),
groups: helpers.getGroupPrivileges('admin', 'filter:privileges.admin.groups.list', privileges.admin.groupPrivilegeList),
users: helpers.getUserPrivileges(0, 'filter:privileges.admin.list', privileges.admin.userPrivilegeList),
groups: helpers.getGroupPrivileges(0, 'filter:privileges.admin.groups.list', privileges.admin.groupPrivilegeList),
});
// This is a hack because I can't do {labels.users.length} to echo the count in templates.js
payload.columnCount = payload.labels.users.length + 2;

View File

@@ -61,15 +61,9 @@ Categories.setPrivilege = async function (socket, data) {
throw new Error('[[error:no-user-or-group]]');
}
if (isNaN(parseInt(data.cid, 10))) {
await privileges[data.cid][data.set ? 'give' : 'rescind'](
Array.isArray(data.privilege) ? data.privilege : [data.privilege], data.member
);
} else {
await privileges.categories[data.set ? 'give' : 'rescind'](
Array.isArray(data.privilege) ? data.privilege : [data.privilege], data.cid, data.member
);
}
await privileges.categories[data.set ? 'give' : 'rescind'](
Array.isArray(data.privilege) ? data.privilege : [data.privilege], data.cid, data.member
);
await events.log({
uid: socket.uid,

View File

@@ -11,15 +11,11 @@
</div>
<div class="privilege-table-container">
{{{ if type.global }}}
<!-- IMPORT admin/partials/privileges/global.tpl -->
{{{ end }}}
{{{ if type.admin }}}
<!-- IMPORT admin/partials/privileges/admin.tpl -->
{{{ end }}}
{{{ if type.cid }}}
{{{ if cid }}}
<!-- IMPORT admin/partials/privileges/category.tpl -->
{{{ end }}}
{{{ else }}}
<!-- IMPORT admin/partials/privileges/global.tpl -->
{{{ endif }}}
</div>
</div>
</form>

View File

@@ -1,88 +0,0 @@
<label>[[admin/manage/privileges:group-privileges]]</label>
<table class="table table-striped privilege-table">
<thead>
<tr class="privilege-table-header">
<th colspan="15"></th>
</tr><tr><!-- zebrastripe reset --></tr>
<tr>
<th colspan="2">[[admin/manage/categories:privileges.section-group]]</th>
<!-- BEGIN privileges.labels.groups -->
<th class="text-center">{privileges.labels.groups.name}</th>
<!-- END privileges.labels.groups -->
</tr>
</thead>
<tbody>
<!-- BEGIN privileges.groups -->
<tr data-group-name="{privileges.groups.nameEscaped}" data-private="<!-- IF privileges.groups.isPrivate -->1<!-- ELSE -->0<!-- ENDIF privileges.groups.isPrivate -->">
<td>
<!-- IF privileges.groups.isPrivate -->
<i class="fa fa-lock text-muted" title="[[admin/manage/categories:privileges.group-private]]"></i>
<!-- ENDIF privileges.groups.isPrivate -->
{privileges.groups.name}
</td>
<td></td>
{function.spawnPrivilegeStates, privileges.groups.name, ../privileges}
</tr>
<!-- END privileges.groups -->
<tr>
<td colspan="{privileges.columnCount}">
<div class="btn-toolbar">
<button type="button" class="btn btn-primary pull-right" data-ajaxify="false" data-action="search.group">
[[admin/manage/categories:privileges.search-group]]
</button>
</div>
</td>
</tr>
</tbody>
</table>
<div class="help-block">
[[admin/manage/categories:privileges.inherit]]
</div>
<hr/>
<label>[[admin/manage/privileges:user-privileges]]</label>
<table class="table table-striped privilege-table">
<thead>
<tr class="privilege-table-header">
<th colspan="15"></th>
</tr><tr><!-- zebrastripe reset --></tr>
<tr>
<th colspan="2">[[admin/manage/categories:privileges.section-user]]</th>
<!-- BEGIN privileges.labels.users -->
<th class="text-center">{privileges.labels.users.name}</th>
<!-- END privileges.labels.users -->
</tr>
</thead>
<tbody>
<!-- IF privileges.users.length -->
<!-- BEGIN privileges.users -->
<tr data-uid="{privileges.users.uid}">
<td>
<!-- IF ../picture -->
<img class="avatar avatar-sm" src="{privileges.users.picture}" title="{privileges.users.username}" />
<!-- ELSE -->
<div class="avatar avatar-sm" style="background-color: {../icon:bgColor};">{../icon:text}</div>
<!-- ENDIF ../picture -->
</td>
<td>{privileges.users.username}</td>
{function.spawnPrivilegeStates, privileges.users.username, ../privileges}
</tr>
<!-- END privileges.users -->
<tr>
<td colspan="{privileges.columnCount}">
<button type="button" class="btn btn-primary pull-right" data-ajaxify="false" data-action="search.user">
[[admin/manage/categories:privileges.search-user]]
</button>
</td>
</tr>
<!-- ELSE -->
<tr>
<td colspan="{privileges.columnCount}">
[[admin/manage/privileges:global.no-users]]
<button type="button" class="btn btn-primary pull-right" data-ajaxify="false" data-action="search.user">
[[admin/manage/categories:privileges.search-user]]
</button>
</td>
</tr>
<!-- ENDIF privileges.users.length -->
</tbody>
</table>