Fix user import
This commit is contained in:
@@ -109,6 +109,7 @@ angular.module('Application').controller('UsersController', ['$scope', '$locatio
|
||||
percent: 0,
|
||||
success: 0,
|
||||
users: [],
|
||||
sendInvite: false,
|
||||
|
||||
reset: function () {
|
||||
$scope.userImport.busy = false;
|
||||
@@ -117,6 +118,7 @@ angular.module('Application').controller('UsersController', ['$scope', '$locatio
|
||||
$scope.userImport.percent = 0;
|
||||
$scope.userImport.success = 0;
|
||||
$scope.userImport.done = false;
|
||||
$scope.userImport.sendInvite = false;
|
||||
},
|
||||
|
||||
handleFileChanged: function () {
|
||||
@@ -131,30 +133,45 @@ angular.module('Application').controller('UsersController', ['$scope', '$locatio
|
||||
const reader = new FileReader();
|
||||
reader.addEventListener('load', function () {
|
||||
$scope.$apply(function () {
|
||||
$scope.userImport.users = [];
|
||||
var users = [];
|
||||
if (file.type === 'text/csv') {
|
||||
$scope.userImport.users = [];
|
||||
|
||||
var lines = reader.result.split('\n');
|
||||
if (lines.length === 0) return $scope.userImport.error = { file: 'Imported file has no lines' };
|
||||
|
||||
lines.forEach(function (line) {
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
var line = lines[i].trim();
|
||||
if (!line) continue;
|
||||
var items = line.split(',');
|
||||
if (items.length !== 4) return console.log('Wrong column count for:', line);
|
||||
$scope.userImport.users.push({
|
||||
id: items[0].trim(),
|
||||
username: items[1].trim(),
|
||||
email: items[2].trim(),
|
||||
displayName: items[3].trim()
|
||||
if (items.length !== 5) {
|
||||
$scope.userImport.error = { file: 'Line ' + (i+1) + ' has wrong column count. Expecting 5' };
|
||||
break;
|
||||
}
|
||||
users.push({
|
||||
username: items[0].trim(),
|
||||
email: items[1].trim(),
|
||||
fallbackEmail: items[2].trim(),
|
||||
displayName: items[3].trim(),
|
||||
role: items[4].trim()
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
$scope.userImport.users = JSON.parse(reader.result);
|
||||
users = JSON.parse(reader.result).map(function (user) {
|
||||
return {
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
fallbackEmail: user.fallbackEmail,
|
||||
displayName: user.displayName,
|
||||
role: user.role
|
||||
};
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('Failed to parse users.', e);
|
||||
$scope.userImport.error = { file: 'Imported file is not valid JSON' };
|
||||
$scope.userImport.error = { file: 'Imported file is not valid JSON:' + e.message };
|
||||
}
|
||||
}
|
||||
$scope.userImport.users = users;
|
||||
});
|
||||
}, false);
|
||||
reader.readAsText(file);
|
||||
@@ -183,13 +200,20 @@ angular.module('Application').controller('UsersController', ['$scope', '$locatio
|
||||
var processed = 0;
|
||||
|
||||
async.eachSeries($scope.userImport.users, function (user, callback) {
|
||||
Client.addUser(user, function (error) {
|
||||
Client.addUser(user, function (error, userId) {
|
||||
if (error) $scope.userImport.error.import.push({ error: error, user: user });
|
||||
else ++$scope.userImport.success;
|
||||
|
||||
++processed;
|
||||
$scope.userImport.percent = 100 * processed / $scope.userImport.users.length;
|
||||
|
||||
if (!error && $scope.userImport.sendInvite) {
|
||||
console.log('sending', userId, user.email);
|
||||
Client.sendInviteEmail(userId, user.email, function (error) {
|
||||
if (error) console.error('Failed to send invite.', error);
|
||||
});
|
||||
}
|
||||
|
||||
callback();
|
||||
});
|
||||
}, function (error) {
|
||||
@@ -197,6 +221,10 @@ angular.module('Application').controller('UsersController', ['$scope', '$locatio
|
||||
|
||||
$scope.userImport.busy = false;
|
||||
$scope.userImport.done = true;
|
||||
if ($scope.userImport.success) {
|
||||
refresh();
|
||||
refreshAllUsers();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -233,7 +261,7 @@ angular.module('Application').controller('UsersController', ['$scope', '$locatio
|
||||
var file = new Blob([ content ], { type: 'application/json' });
|
||||
var a = document.createElement('a');
|
||||
a.href = URL.createObjectURL(file);
|
||||
a.download = 'users.json';
|
||||
a.download = type === 'json' ? 'users.json' : 'users.csv';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user