Skip to content

Commit

Permalink
fix(accounts): import failing due to role changes
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Mar 20, 2019
1 parent 10c36fa commit 893af61
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 83 deletions.
81 changes: 0 additions & 81 deletions src/controllers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions src/socketio/accountImportSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -313,7 +329,7 @@ events.onImportLDAP = function (socket) {
fullname: lu.displayName,
email: lu.mail,
title: lu.title,
role: 'user',
role: defaultUserRole,
password: 'Password1!'
})

Expand Down

0 comments on commit 893af61

Please sign in to comment.