From 150bd5778a0e97894656f58d2615b94a9e7170eb Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Thu, 20 Aug 2015 22:29:16 -0600 Subject: [PATCH] * Switch to JSON/config.json based user groups, and user group membership --- core/config.js | 5 ++-- core/database.js | 22 ++-------------- core/user.js | 32 ++++++----------------- core/user_group.js | 64 +++++++++++----------------------------------- 4 files changed, 28 insertions(+), 95 deletions(-) diff --git a/core/config.js b/core/config.js index 616ee438..49ff5b0d 100644 --- a/core/config.js +++ b/core/config.js @@ -75,8 +75,9 @@ function getDefaultConfig() { passwordMax : 128, requireActivation : true, // require SysOp activation? invalidUsernames : [], - - defaultGroups : [ 'users' ] + + groups : [ 'users', 'sysops' ], // built in groups + defaultGroups : [ 'users' ] // default groups new users belong to }, defaults : { diff --git a/core/database.js b/core/database.js index 7ddade02..2240607a 100644 --- a/core/database.js +++ b/core/database.js @@ -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");' - ); } \ No newline at end of file diff --git a/core/user.js b/core/user.js index cfddcb18..55b5f489 100644 --- a/core/user.js +++ b/core/user.js @@ -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) { diff --git a/core/user_group.js b/core/user_group.js index febac904..2fcaacf3 100644 --- a/core/user_group.js +++ b/core/user_group.js @@ -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); }