2019-11-02 00:06:25 +01:00
|
|
|
const Dict = require('./dict').Dict;
|
2019-12-26 15:34:17 +01:00
|
|
|
const FoldDict = require('./fold_dict').FoldDict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_group_name_dict;
|
|
|
|
let user_group_by_id_dict;
|
2017-11-07 08:58:46 +01:00
|
|
|
|
|
|
|
// We have an init() function so that our automated tests
|
|
|
|
// can easily clear data.
|
|
|
|
exports.init = function () {
|
2019-12-26 15:34:17 +01:00
|
|
|
user_group_name_dict = new FoldDict();
|
2017-11-07 08:58:46 +01:00
|
|
|
user_group_by_id_dict = new Dict();
|
|
|
|
};
|
|
|
|
|
|
|
|
// WE INITIALIZE DATA STRUCTURES HERE!
|
|
|
|
exports.init();
|
|
|
|
|
2017-11-17 06:15:19 +01:00
|
|
|
exports.add = function (user_group) {
|
2020-01-16 20:12:06 +01:00
|
|
|
// Reformat the user group members structure to be a set.
|
|
|
|
user_group.members = new Set(user_group.members);
|
2017-11-07 08:58:46 +01:00
|
|
|
user_group_name_dict.set(user_group.name, user_group);
|
|
|
|
user_group_by_id_dict.set(user_group.id, user_group);
|
|
|
|
};
|
|
|
|
|
2017-12-06 20:00:23 +01:00
|
|
|
exports.remove = function (user_group) {
|
|
|
|
user_group_name_dict.del(user_group.name);
|
|
|
|
user_group_by_id_dict.del(user_group.id);
|
|
|
|
};
|
|
|
|
|
2019-02-15 23:50:28 +01:00
|
|
|
exports.get_user_group_from_id = function (group_id, suppress_errors) {
|
2017-11-07 08:58:46 +01:00
|
|
|
if (!user_group_by_id_dict.has(group_id)) {
|
2019-02-15 23:50:28 +01:00
|
|
|
if (suppress_errors === undefined) {
|
|
|
|
blueslip.error('Unknown group_id in get_user_group_from_id: ' + group_id);
|
|
|
|
}
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-11-07 08:58:46 +01:00
|
|
|
}
|
|
|
|
return user_group_by_id_dict.get(group_id);
|
|
|
|
};
|
|
|
|
|
2018-03-14 17:52:38 +01:00
|
|
|
exports.update = function (event) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const group = exports.get_user_group_from_id(event.group_id);
|
2018-03-14 17:52:38 +01:00
|
|
|
if (event.data.name !== undefined) {
|
|
|
|
group.name = event.data.name;
|
2018-03-15 15:48:48 +01:00
|
|
|
user_group_name_dict.del(group.name);
|
|
|
|
user_group_name_dict.set(group.name, group);
|
2018-03-14 17:52:38 +01:00
|
|
|
}
|
|
|
|
if (event.data.description !== undefined) {
|
|
|
|
group.description = event.data.description;
|
2018-03-15 15:48:48 +01:00
|
|
|
user_group_name_dict.del(group.name);
|
|
|
|
user_group_name_dict.set(group.name, group);
|
2018-03-14 17:52:38 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-07 08:58:46 +01:00
|
|
|
exports.get_user_group_from_name = function (name) {
|
|
|
|
return user_group_name_dict.get(name);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_realm_user_groups = function () {
|
2018-03-14 10:01:14 +01:00
|
|
|
return user_group_by_id_dict.values().sort(function (a, b) {
|
2018-06-06 18:19:09 +02:00
|
|
|
return a.id - b.id;
|
2018-03-14 10:01:14 +01:00
|
|
|
});
|
2017-11-07 08:58:46 +01:00
|
|
|
};
|
|
|
|
|
2017-11-23 07:32:30 +01:00
|
|
|
exports.is_member_of = function (user_group_id, user_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_group = user_group_by_id_dict.get(user_group_id);
|
2019-01-15 00:39:03 +01:00
|
|
|
if (user_group === undefined) {
|
|
|
|
blueslip.error("Could not find user group with ID " + user_group_id);
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-04 17:44:12 +01:00
|
|
|
return user_group.members.has(user_id);
|
2017-11-23 07:32:30 +01:00
|
|
|
};
|
|
|
|
|
2018-03-04 17:39:54 +01:00
|
|
|
exports.add_members = function (user_group_id, user_ids) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_group = user_group_by_id_dict.get(user_group_id);
|
2018-03-04 17:39:54 +01:00
|
|
|
_.each(user_ids, function (user_id) {
|
2020-01-16 20:12:06 +01:00
|
|
|
user_group.members.add(user_id);
|
2018-03-04 17:39:54 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.remove_members = function (user_group_id, user_ids) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_group = user_group_by_id_dict.get(user_group_id);
|
2018-03-04 17:39:54 +01:00
|
|
|
_.each(user_ids, function (user_id) {
|
2020-01-16 20:12:06 +01:00
|
|
|
user_group.members.delete(user_id);
|
2018-03-04 17:39:54 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-07 08:58:46 +01:00
|
|
|
exports.initialize = function () {
|
|
|
|
_.each(page_params.realm_user_groups, function (user_group) {
|
|
|
|
exports.add(user_group);
|
|
|
|
});
|
|
|
|
|
|
|
|
delete page_params.realm_user_groups; // We are the only consumer of this.
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.is_user_group = function (item) {
|
|
|
|
return item.hasOwnProperty('members');
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.user_groups = exports;
|