Files
cloudron-box/src/groups.js

240 lines
6.4 KiB
JavaScript
Raw Normal View History

2016-02-07 20:25:08 -08:00
'use strict';
exports = module.exports = {
create: create,
remove: remove,
2016-02-08 09:41:21 -08:00
get: get,
2018-06-18 13:41:27 -07:00
update: update,
2016-02-09 15:26:34 -08:00
getWithMembers: getWithMembers,
2016-02-09 13:33:30 -08:00
getAll: getAll,
getAllWithMembers: getAllWithMembers,
2016-02-08 09:41:21 -08:00
getMembers: getMembers,
addMember: addMember,
setMembers: setMembers,
2016-02-08 10:53:01 -08:00
removeMember: removeMember,
2016-02-08 09:26:52 -08:00
isMember: isMember,
2018-06-18 14:10:29 -07:00
getGroups: getGroups,
setMembership: setMembership,
2019-07-03 13:47:12 +02:00
getMembership: getMembership,
count: count
2016-02-07 20:25:08 -08:00
};
var assert = require('assert'),
2019-10-22 16:34:17 -07:00
BoxError = require('./boxerror.js'),
2016-09-20 15:07:11 -07:00
constants = require('./constants.js'),
2016-02-07 20:34:05 -08:00
groupdb = require('./groupdb.js'),
uuid = require('uuid'),
_ = require('underscore');
2016-02-07 20:25:08 -08:00
// keep this in sync with validateUsername
2016-02-07 20:25:08 -08:00
function validateGroupname(name) {
assert.strictEqual(typeof name, 'string');
2019-10-22 16:34:17 -07:00
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' });
2016-02-07 20:25:08 -08:00
2019-10-22 16:34:17 -07:00
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)
2019-10-22 16:34:17 -07:00
if (/[^a-zA-Z0-9.-]/.test(name)) return new BoxError(BoxError.BAD_FIELD, 'name can only contain alphanumerals, hyphen and dot', { field: 'name' });
2016-09-21 11:55:53 -07:00
2016-02-07 20:25:08 -08:00
return null;
}
2018-07-26 10:20:19 -07:00
function create(name, callback) {
2016-02-07 20:25:08 -08:00
assert.strictEqual(typeof name, 'string');
assert.strictEqual(typeof callback, 'function');
// we store names in lowercase
name = name.toLowerCase();
2016-02-07 20:25:08 -08:00
var error = validateGroupname(name);
if (error) return callback(error);
2016-09-30 09:18:41 -07:00
var id = 'gid-' + uuid.v4();
2018-07-26 10:20:19 -07:00
groupdb.add(id, name, function (error) {
if (error) return callback(error);
2016-02-07 20:25:08 -08:00
callback(null, { id: id, name: name });
2016-02-07 20:25:08 -08:00
});
}
function remove(id, callback) {
assert.strictEqual(typeof id, 'string');
assert.strictEqual(typeof callback, 'function');
2018-04-04 20:08:52 -07:00
groupdb.del(id, function (error) {
if (error) return callback(error);
2016-02-07 20:25:08 -08:00
2018-04-04 20:08:52 -07:00
callback(null);
2016-02-07 20:25:08 -08:00
});
}
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);
2016-02-07 20:25:08 -08:00
return callback(null, result);
});
}
2016-02-09 15:26:34 -08:00
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);
2016-02-09 15:26:34 -08:00
return callback(null, result);
});
}
2016-02-09 13:33:30 -08:00
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);
2016-02-09 13:33:30 -08:00
return callback(null, result);
});
}
2016-02-08 09:41:21 -08:00
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);
2016-02-08 09:41:21 -08:00
return callback(null, result);
});
}
function getMembership(userId, callback) {
2016-02-08 20:38:50 -08:00
assert.strictEqual(typeof userId, 'string');
assert.strictEqual(typeof callback, 'function');
groupdb.getMembership(userId, function (error, result) {
if (error) return callback(error);
2016-02-08 20:38:50 -08:00
return callback(null, result);
});
}
function setMembership(userId, groupIds, callback) {
2016-02-09 15:47:02 -08:00
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);
2016-02-09 15:47:02 -08:00
2016-02-11 10:52:31 +01:00
return callback(null);
2016-02-09 15:47:02 -08:00
});
}
2016-02-08 09:41:21 -08:00
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);
2016-02-08 09:41:21 -08:00
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);
});
}
2016-02-08 09:41:21 -08:00
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);
2016-02-08 09:41:21 -08:00
return callback(null);
});
}
2016-02-08 10:53:01 -08:00
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);
2016-02-08 10:53:01 -08:00
return callback(null, result);
});
}
2018-06-18 13:41:27 -07:00
function update(groupId, data, callback) {
assert.strictEqual(typeof groupId, 'string');
2018-06-18 13:41:27 -07:00
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);
}
2018-06-18 18:26:50 -07:00
2018-07-26 10:20:19 -07:00
groupdb.update(groupId, _.pick(data, 'name'), function (error) {
if (error) return callback(error);
callback(null);
});
}
2018-06-16 00:29:56 -07:00
2018-06-18 14:10:29 -07:00
function getGroups(userId, callback) {
2018-06-16 00:29:56 -07:00
assert.strictEqual(typeof userId, 'string');
assert.strictEqual(typeof callback, 'function');
2018-06-18 14:10:29 -07:00
groupdb.getGroups(userId, function (error, results) {
if (error) return callback(error);
2018-06-16 00:29:56 -07:00
2018-06-18 14:10:29 -07:00
callback(null, results);
2018-06-16 00:29:56 -07:00
});
}
2019-07-03 13:47:12 +02:00
function count(callback) {
assert.strictEqual(typeof callback, 'function');
groupdb.count(function (error, count) {
if (error) return callback(error);
2019-07-03 13:47:12 +02:00
callback(null, count);
});
}