240 lines
6.4 KiB
JavaScript
240 lines
6.4 KiB
JavaScript
'use strict';
|
|
|
|
exports = module.exports = {
|
|
create: create,
|
|
remove: remove,
|
|
get: get,
|
|
update: update,
|
|
getWithMembers: getWithMembers,
|
|
getAll: getAll,
|
|
getAllWithMembers: getAllWithMembers,
|
|
|
|
getMembers: getMembers,
|
|
addMember: addMember,
|
|
setMembers: setMembers,
|
|
removeMember: removeMember,
|
|
isMember: isMember,
|
|
|
|
getGroups: getGroups,
|
|
|
|
setMembership: setMembership,
|
|
getMembership: getMembership,
|
|
|
|
count: count
|
|
};
|
|
|
|
var assert = require('assert'),
|
|
BoxError = require('./boxerror.js'),
|
|
constants = require('./constants.js'),
|
|
groupdb = require('./groupdb.js'),
|
|
uuid = require('uuid'),
|
|
_ = require('underscore');
|
|
|
|
// keep this in sync with validateUsername
|
|
function validateGroupname(name) {
|
|
assert.strictEqual(typeof name, 'string');
|
|
|
|
if (name.length < 1) return new BoxError(BoxError.BAD_FIELD, 'name must be atleast 1 char', { field: 'name' });
|
|
if (name.length >= 200) return new BoxError(BoxError.BAD_FIELD, 'name too long', { field: 'name' });
|
|
|
|
if (constants.RESERVED_NAMES.indexOf(name) !== -1) return new BoxError(BoxError.BAD_FIELD, 'name is reserved', { field: name });
|
|
|
|
// need to consider valid LDAP characters here (e.g '+' is reserved)
|
|
if (/[^a-zA-Z0-9.-]/.test(name)) return new BoxError(BoxError.BAD_FIELD, 'name can only contain alphanumerals, hyphen and dot', { field: 'name' });
|
|
|
|
return null;
|
|
}
|
|
|
|
function create(name, callback) {
|
|
assert.strictEqual(typeof name, 'string');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
// we store names in lowercase
|
|
name = name.toLowerCase();
|
|
|
|
var error = validateGroupname(name);
|
|
if (error) return callback(error);
|
|
|
|
var id = 'gid-' + uuid.v4();
|
|
groupdb.add(id, name, function (error) {
|
|
if (error) return callback(error);
|
|
|
|
callback(null, { id: id, name: name });
|
|
});
|
|
}
|
|
|
|
function remove(id, callback) {
|
|
assert.strictEqual(typeof id, 'string');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.del(id, function (error) {
|
|
if (error) return callback(error);
|
|
|
|
callback(null);
|
|
});
|
|
}
|
|
|
|
function get(id, callback) {
|
|
assert.strictEqual(typeof id, 'string');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.get(id, function (error, result) {
|
|
if (error) return callback(error);
|
|
|
|
return callback(null, result);
|
|
});
|
|
}
|
|
|
|
function getWithMembers(id, callback) {
|
|
assert.strictEqual(typeof id, 'string');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.getWithMembers(id, function (error, result) {
|
|
if (error) return callback(error);
|
|
|
|
return callback(null, result);
|
|
});
|
|
}
|
|
|
|
function getAll(callback) {
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.getAll(function (error, result) {
|
|
if (error) return callback(error);
|
|
|
|
return callback(null, result);
|
|
});
|
|
}
|
|
|
|
function getAllWithMembers(callback) {
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.getAllWithMembers(function (error, result) {
|
|
if (error) return callback(error);
|
|
|
|
return callback(null, result);
|
|
});
|
|
}
|
|
|
|
function getMembers(groupId, callback) {
|
|
assert.strictEqual(typeof groupId, 'string');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.getMembers(groupId, function (error, result) {
|
|
if (error) return callback(error);
|
|
|
|
return callback(null, result);
|
|
});
|
|
}
|
|
|
|
function getMembership(userId, callback) {
|
|
assert.strictEqual(typeof userId, 'string');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.getMembership(userId, function (error, result) {
|
|
if (error) return callback(error);
|
|
|
|
return callback(null, result);
|
|
});
|
|
}
|
|
|
|
function setMembership(userId, groupIds, callback) {
|
|
assert.strictEqual(typeof userId, 'string');
|
|
assert(Array.isArray(groupIds));
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.setMembership(userId, groupIds, function (error) {
|
|
if (error) return callback(error);
|
|
|
|
return callback(null);
|
|
});
|
|
}
|
|
|
|
function addMember(groupId, userId, callback) {
|
|
assert.strictEqual(typeof groupId, 'string');
|
|
assert.strictEqual(typeof userId, 'string');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.addMember(groupId, userId, function (error) {
|
|
if (error) return callback(error);
|
|
|
|
return callback(null);
|
|
});
|
|
}
|
|
|
|
function setMembers(groupId, userIds, callback) {
|
|
assert.strictEqual(typeof groupId, 'string');
|
|
assert(Array.isArray(userIds));
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.setMembers(groupId, userIds, function (error) {
|
|
if (error) return callback(error);
|
|
|
|
return callback(null);
|
|
});
|
|
}
|
|
|
|
function removeMember(groupId, userId, callback) {
|
|
assert.strictEqual(typeof groupId, 'string');
|
|
assert.strictEqual(typeof userId, 'string');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.removeMember(groupId, userId, function (error) {
|
|
if (error) return callback(error);
|
|
|
|
return callback(null);
|
|
});
|
|
}
|
|
|
|
function isMember(groupId, userId, callback) {
|
|
assert.strictEqual(typeof groupId, 'string');
|
|
assert.strictEqual(typeof userId, 'string');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.isMember(groupId, userId, function (error, result) {
|
|
if (error) return callback(error);
|
|
|
|
return callback(null, result);
|
|
});
|
|
}
|
|
|
|
function update(groupId, data, callback) {
|
|
assert.strictEqual(typeof groupId, 'string');
|
|
assert(data && typeof data === 'object');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
let error;
|
|
if ('name' in data) {
|
|
assert.strictEqual(typeof data.name, 'string');
|
|
error = validateGroupname(data.name);
|
|
if (error) return callback(error);
|
|
}
|
|
|
|
groupdb.update(groupId, _.pick(data, 'name'), function (error) {
|
|
if (error) return callback(error);
|
|
|
|
callback(null);
|
|
});
|
|
}
|
|
|
|
function getGroups(userId, callback) {
|
|
assert.strictEqual(typeof userId, 'string');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.getGroups(userId, function (error, results) {
|
|
if (error) return callback(error);
|
|
|
|
callback(null, results);
|
|
});
|
|
}
|
|
|
|
function count(callback) {
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
groupdb.count(function (error, count) {
|
|
if (error) return callback(error);
|
|
|
|
callback(null, count);
|
|
});
|
|
}
|