From 893af61034a5fa606a92dc81757d1b34c089fced Mon Sep 17 00:00:00 2001 From: Chris Brame Date: Wed, 20 Mar 2019 00:31:59 -0400 Subject: [PATCH] fix(accounts): import failing due to role changes --- src/controllers/api.js | 81 ----------------------------- src/socketio/accountImportSocket.js | 20 ++++++- 2 files changed, 18 insertions(+), 83 deletions(-) diff --git a/src/controllers/api.js b/src/controllers/api.js index ac8201fd7..b4cd45475 100644 --- a/src/controllers/api.js +++ b/src/controllers/api.js @@ -57,87 +57,6 @@ apiController.reports = apiReportsV1 apiController.settings = apiSettingsV1 apiController.plugins = apiPluginsV1 -apiController.import = function (req, res) { - var fs = require('fs') - var path = require('path') - var UserModal = require('../models/user') - var groupModel = require('../models/group') - - var array = fs - .readFileSync(path.join(__dirname, '..', 'import.csv')) - .toString() - .split('\n') - var clean = array.filter(function (e) { - return e - }) - - async.eachSeries( - clean, - function (item, cb) { - winston.info(item) - - var fields = item.split(',') - var fullname = fields[0].toString().replace('.', ' ') - var k = fullname.split(' ') - var kCap = _.capitalize(k[0]) - var kCap1 = _.capitalize(k[1]) - fullname = kCap + ' ' + kCap1 - - var groupName = fields[2].replace('\\r', '') - groupName = _.trim(groupName) - var User = new UserModal({ - username: fields[0], - password: 'Password123', - email: fields[1], - fullname: fullname, - role: 'user' - }) - - async.series( - [ - function (next) { - User.save(function (err) { - if (err) return next(err) - - next() - }) - }, - function (next) { - winston.debug('Getting Group "' + groupName + '"') - groupModel.getGroupByName(groupName, function (err, group) { - if (err) return next(err) - - if (_.isUndefined(group) || _.isNull(group)) { - return next('no group found = ' + groupName) - } - - group.addMember(User._id, function (err) { - if (err) return next(err) - - group.save(function (err) { - if (err) return next(err) - - next() - }) - }) - }) - } - ], - function (err) { - if (err) return cb(err) - - cb() - } - ) - }, - function (err) { - if (err) return res.status(500).send(err) - - res.status(200).send('Imported ' + _.size(clean)) - } - ) -} - /** * Redirects to login page * @param {object} req Express Request diff --git a/src/socketio/accountImportSocket.js b/src/socketio/accountImportSocket.js index ec4fb34a7..92b46bb08 100644 --- a/src/socketio/accountImportSocket.js +++ b/src/socketio/accountImportSocket.js @@ -288,10 +288,26 @@ events.onImportLDAP = function (socket) { var addedUsers = data.addedUsers var updatedUsers = data.updatedUsers - + var defaultUserRole = null var completedCount = 0 + async.series( [ + function (next) { + var settingSchema = require('../models/setting') + settingSchema.getSetting('role:user:default', function (err, setting) { + if (err || !setting) { + utils.sendToSelf(socket, '$trudesk:accounts:import:error', { + error: 'Default user role not set. Please contact an Administrator.' + }) + + return next('Default user role not set. Please contact an Administrator') + } + + defaultUserRole = setting.value + return next() + }) + }, function (next) { async.eachSeries( addedUsers, @@ -313,7 +329,7 @@ events.onImportLDAP = function (socket) { fullname: lu.displayName, email: lu.mail, title: lu.title, - role: 'user', + role: defaultUserRole, password: 'Password1!' })