From 24e18b5a7eeaffd0e0fe370d8e708839d72c4169 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Mon, 9 Sep 2019 21:35:12 -0600 Subject: [PATCH] Clean up defualt mesage/file conf/areas at login if user no longer has read access --- core/user_login.js | 82 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/core/user_login.js b/core/user_login.js index bc23e3b3..627fb5ae 100644 --- a/core/user_login.js +++ b/core/user_login.js @@ -16,6 +16,16 @@ const UserProps = require('./user_property.js'); const SysProps = require('./system_property.js'); const SystemLogKeys = require('./system_log.js'); const User = require('./user.js'); +const { + getMessageConferenceByTag, + getMessageAreaByTag, + getDefaultMessageConferenceTag, + getDefaultMessageAreaTagByConfTag, +} = require('./message_area.js'); +const { + getFileAreaByTag, + getDefaultFileAreaTag, +} = require('./file_base_area.js'); // deps const async = require('async'); @@ -101,15 +111,77 @@ function userLogin(client, username, password, options, cb) { Events.emit(Events.getSystemEvents().UserLogin, { user } ); setClientTheme(client, user.properties[UserProps.ThemeId]); - if(user.authenticated) { - return recordLogin(client, cb); - } - // recordLogin() must happen after 2FA! - return cb(null); + postLoginPrep(client, err => { + if(err) { + return cb(err); + } + + if(user.authenticated) { + return recordLogin(client, cb); + } + + // recordLogin() must happen after 2FA! + return cb(null); + }); }); } +function postLoginPrep(client, cb) { + + const defaultMsgAreaTag = (confTag) => { + return getDefaultMessageAreaTagByConfTag(client, confTag) || + getDefaultMessageAreaTagByConfTag(client, getDefaultMessageConferenceTag(client)) || + ''; + }; + + async.series( + [ + (callback) => { + // + // User may (no longer) have read (view) rights to their current + // message, conferences and/or areas. Move them out if so. + // + let confTag = client.user.getProperty(UserProps.MessageConfTag); + const conf = getMessageConferenceByTag(confTag) || {}; + const area = getMessageAreaByTag(client.user.getProperty(UserProps.MessageAreaTag), confTag) || {}; + + if(!client.acs.hasMessageConfRead(conf)) { + confTag = getDefaultMessageConferenceTag(client) || ''; + client.user.persistProperties({ + [ UserProps.MessageConfTag ] : confTag, + [ UserProps.MessageAreaTag ] : defaultMsgAreaTag(confTag), + }, + err => { + return callback(err); + }); + } else if (!client.acs.hasMessageAreaRead(area)) { + client.user.persistProperty(UserProps.MessageAreaTag, defaultMsgAreaTag(confTag), err => { + return callback(err); + }); + } else { + return callback(null); + } + }, + (callback) => { + // Likewise for file areas + const area = getFileAreaByTag(client.user.getProperty(UserProps.FileAreaTag)) || {}; + if(!client.acs.hasFileAreaRead(area)) { + const areaTag = getDefaultFileAreaTag(client) || ''; + client.user.persistProperty(UserProps.FileAreaTag, areaTag, err => { + return callback(err); + }); + } else { + return callback(null); + } + } + ], + err => { + return cb(err); + } + ); +} + function recordLogin(client, cb) { assert(client.user.authenticated); // don't get in situations where this isn't true