From 8616cc4ea5bfa390180ccc041e7b51e10cf6556a Mon Sep 17 00:00:00 2001 From: psychobunny Date: Sat, 28 May 2016 06:08:28 -0400 Subject: [PATCH] upgrade script: give topics:read privs to any group that has topics:create privs --- src/upgrade.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/src/upgrade.js b/src/upgrade.js index 1874db129e..6e689b5206 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -10,7 +10,7 @@ var db = require('./database'), schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - latestSchema = Date.UTC(2016, 3, 29); + latestSchema = Date.UTC(2016, 4, 28); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { @@ -494,6 +494,75 @@ Upgrade.upgrade = function(callback) { winston.info('[2016/04/29] Dismiss flags from deleted topics skipped!'); next(); } + }, + function(next) { + thisSchemaDate = Date.UTC(2016, 4, 28); + + if (schemaDate < thisSchemaDate) { + updatesMade = true; + winston.info('[2016/05/28] Giving topics:read privs to any group that has topics:create selected'); + + var groupsAPI = require('./groups'); + var privilegesAPI = require('./privileges'); + + db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { + async.eachSeries(cids, function(cid, next) { + privilegesAPI.categories.list(cid, function(err, data) { + var groups = data.groups; + var users = data.users; + + async.waterfall([ + function(next) { + async.each(groups, function(group, next) { + if (group.privileges['groups:topics:create']) { + return groupsAPI.join('cid:' + cid + ':privileges:groups:topics:read', group.name, function(err) { + if (!err) { + winston.info('cid:' + cid + ':privileges:groups:topics:read granted to gid: ' + group.name); + } + + return next(err); + }); + } + + next(null); + }, next); + }, + function(next) { + async.each(users, function(user, next) { + if (user.privileges['topics:create']) { + return groupsAPI.join('cid:' + cid + ':privileges:topics:read', user.uid, function(err) { + if (!err) { + winston.info('cid:' + cid + ':privileges:topics:read granted to uid: ' + user.uid); + } + + return next(err); + }); + } + + next(null); + }, next); + } + ], function(err) { + if (!err) { + winston.info('-- cid ' + cid + ' upgraded'); + } + + next(err); + }); + }); + }, function(err) { + if (err) { + return next(err); + } + + winston.info('[2016/05/28] Giving topics:read privs to any group that has topics:create selected - done'); + Upgrade.update(thisSchemaDate, next); + }); + }); + } else { + winston.info('[2016/05/28] Giving topics:read privs to any group that has topics:create selected - skipped!'); + next(); + } } // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!!