* Switch to JSON/config.json based user groups, and user group membership

This commit is contained in:
Bryan Ashby 2015-08-20 22:29:16 -06:00
parent 6f3137d522
commit 150bd5778a
4 changed files with 28 additions and 95 deletions

View File

@ -76,7 +76,8 @@ function getDefaultConfig() {
requireActivation : true, // require SysOp activation?
invalidUsernames : [],
defaultGroups : [ 'users' ]
groups : [ 'users', 'sysops' ], // built in groups
defaultGroups : [ 'users' ] // default groups new users belong to
},
defaults : {

View File

@ -53,20 +53,11 @@ function createUserTables() {
');'
);
dbs.user.run(
'CREATE TABLE IF NOT EXISTS user_group (' +
' group_id INTEGER PRIMARY KEY,' +
' group_name VARCHAR NOT NULL,' +
' UNIQUE(group_name)' +
');'
);
dbs.user.run(
'CREATE TABLE IF NOT EXISTS user_group_member (' +
' group_id INTEGER NOT NULL,' +
' group_name VARCHAR NOT NULL,' +
' user_id INTEGER NOT NULL,' +
' UNIQUE(group_id, user_id),' +
' FOREIGN KEY(group_id) REFERENCES user_group(group_id) ON DELETE CASCADE' +
' UNIQUE(group_name, user_id)' +
');'
);
@ -151,13 +142,4 @@ function createInitialMessageValues() {
}
function createInitialUserValues() {
dbs.user.run(
'INSERT OR IGNORE INTO user_group ' +
'VALUES(1, "users");'
);
dbs.user.run(
'INSERT OR IGNORE INTO user_group ' +
'VALUES(2, "sysops");'
);
}

View File

@ -22,7 +22,7 @@ function User() {
this.userId = 0;
this.username = '';
this.properties = {}; // name:value
this.groups = {}; // id:name
this.groups = []; // group membership(s)
this.isValid = function() {
if(self.userId <= 0 || self.username.length < Config.users.usernameMin) {
@ -47,8 +47,8 @@ function User() {
this.isSysOp = this.isRoot; // alias
this.isGroupMember = function(groupIdOrName) {
return _.isString(self.groups[groupIdOrName]);
this.isGroupMember = function(groupName) {
return self.groups.indexOf(groupName) > -1;
};
this.getLegacySecurityLevel = function() {
@ -217,29 +217,13 @@ User.prototype.create = function(options, cb) {
});
},
function setInitialGroupMembership(callback) {
userGroup.getGroupsByName(Config.users.defaultGroups, function defaultGroups(err, groups) {
if(err) {
callback(err);
} else {
self.groups = groups;
callback(null);
}
});
},
function setInitialSysOpGroupMembership(callback) {
if(1 !== self.userId) {
callback(null);
return;
self.groups = Config.users.defaultGroups;
if(1 === self.userId) { // root/SysOp?
self.groups.push('sysops');
}
userGroup.getGroupsByName( [ 'sysops' ], function sysopGroups(err, groups) {
if(err) {
callback(err);
} else {
_.assign(self.groups, self.groups, groups);
callback(null);
}
});
callback(null);
},
function saveAll(callback) {
self.persist(false, function persisted(err) {

View File

@ -8,37 +8,24 @@ var async = require('async');
var _ = require('lodash');
exports.getGroupsForUser = getGroupsForUser;
exports.getGroupsByName = getGroupsByName;
exports.addUserToGroup = addUserToGroup;
exports.addUserToGroups = addUserToGroups;
exports.removeUserFromGroup = removeUserFromGroup;
//
// user_group
// group_id | group_name
//
//
// user_group_member
// group_id | user_id
//
//
function getGroupsForUser(userId, cb) {
var sql =
'SELECT g.group_id, g.group_name ' +
'FROM user_group g, user_group_member gm ' +
'WHERE g.group_id = gm.group_id AND gm.user_id = ?;';
'SELECT group_name ' +
'FROM user_group_member ' +
'WHERE user_id=?;';
var groups = {}; // id:name
var groups = [];
userDb.each(sql, [ userId ], function dbRow(err, row) {
userDb.each(sql, [ userId ], function rowData(err, row) {
if(err) {
cb(err);
return;
} else {
groups[row.group_id] = row.group_name;
groups.push(row.group_name);
}
},
function complete() {
@ -46,31 +33,11 @@ function getGroupsForUser(userId, cb) {
});
}
function getGroupsByName(groupNames, cb) {
var sql =
'SELECT group_id, group_name ' +
'FROM user_group ' +
'WHERE group_name IN ("' + groupNames.join('","') + '");';
userDb.all(sql, function allRows(err, rows) {
if(err) {
cb(err);
return;
} else {
var groups = {};
rows.forEach(function row(r) {
groups[r.group_id] = r.group_name;
});
cb(null, groups);
}
});
}
function addUserToGroup(userId, groupId, cb) {
function addUserToGroup(userId, groupName, cb) {
userDb.run(
'REPLACE INTO user_group_member (group_id, user_id) ' +
'REPLACE INTO user_group_member (group_name, user_id) ' +
'VALUES(?, ?);',
[ groupId, userId ],
[ groupName, userId ],
function complete(err) {
cb(err);
}
@ -78,20 +45,19 @@ function addUserToGroup(userId, groupId, cb) {
}
function addUserToGroups(userId, groups, cb) {
async.each(Object.keys(groups), function item(groupId, nextItem) {
addUserToGroup(userId, groupId, function added(err) {
nextItem(err);
});
async.each(groups, function item(groupName, next) {
addUserToGroup(userId, groupName, next);
}, function complete(err) {
cb(err);
});
}
function removeUserFromGroup(userId, groupId, cb) {
function removeUserFromGroup(userId, groupName, cb) {
userDb.run(
'DELETE FROM user_group_member ' +
'WHERE group_id = ? AND user_id = ?;',
[ groupId, userId ],
'WHERE group_name=? AND user_id=?;',
[ groupName, userId ],
function complete(err) {
cb(err);
}