group: cannot set name of ldap group
This commit is contained in:
@@ -5,7 +5,9 @@ exports = module.exports = {
|
||||
remove,
|
||||
get,
|
||||
getByName,
|
||||
update,
|
||||
|
||||
setName,
|
||||
|
||||
getWithMembers,
|
||||
list,
|
||||
listWithMembers,
|
||||
@@ -73,7 +75,7 @@ async function add(group) {
|
||||
if (error && error.code === 'ER_DUP_ENTRY') throw new BoxError(BoxError.ALREADY_EXISTS, error);
|
||||
if (error) throw error;
|
||||
|
||||
return { id, name };
|
||||
return { id, name, source };
|
||||
}
|
||||
|
||||
async function remove(id) {
|
||||
@@ -200,34 +202,22 @@ async function isMember(groupId, userId) {
|
||||
return result.length !== 0;
|
||||
}
|
||||
|
||||
async function update(id, data) {
|
||||
assert.strictEqual(typeof id, 'string');
|
||||
assert(data && typeof data === 'object');
|
||||
async function setName(group, name) {
|
||||
assert.strictEqual(typeof group, 'object');
|
||||
assert.strictEqual(typeof name, 'string');
|
||||
|
||||
if ('name' in data) {
|
||||
assert.strictEqual(typeof data.name, 'string');
|
||||
const error = validateGroupname(data.name);
|
||||
if (error) throw error;
|
||||
}
|
||||
if (group.source === 'ldap') throw new BoxError(BoxError.BAD_STATE, 'Cannot set name of external group');
|
||||
|
||||
const args = [];
|
||||
const fields = [];
|
||||
for (const k in data) {
|
||||
if (k === 'name') {
|
||||
assert.strictEqual(typeof data.name, 'string');
|
||||
fields.push(k + ' = ?');
|
||||
args.push(data.name);
|
||||
}
|
||||
}
|
||||
args.push(id);
|
||||
name = name.toLowerCase(); // we store names in lowercase
|
||||
const error = validateGroupname(name);
|
||||
if (error) throw error;
|
||||
|
||||
const [updateError, result] = await safe(database.query('UPDATE userGroups SET ' + fields.join(', ') + ' WHERE id = ?', args));
|
||||
const [updateError, result] = await safe(database.query('UPDATE userGroups SET name = ? WHERE id = ?', [ name, group.id ]));
|
||||
if (updateError && updateError.code === 'ER_DUP_ENTRY' && updateError.sqlMessage.indexOf('userGroups_name') !== -1) throw new BoxError(BoxError.ALREADY_EXISTS, 'name already exists');
|
||||
if (updateError) throw updateError;
|
||||
if (result.affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'Group not found');
|
||||
}
|
||||
|
||||
|
||||
async function resetSource() {
|
||||
await database.query('UPDATE userGroups SET source = ?', [ '' ]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user