First version of users import and export feature
This commit is contained in:
@@ -85,6 +85,103 @@ angular.module('Application').controller('UsersController', ['$scope', '$locatio
|
||||
}
|
||||
};
|
||||
|
||||
$scope.userImport = {
|
||||
busy: false,
|
||||
done: false,
|
||||
error: null,
|
||||
percent: 0,
|
||||
success: 0,
|
||||
users: [],
|
||||
|
||||
reset: function () {
|
||||
$scope.userImport.busy = false;
|
||||
$scope.userImport.error = null;
|
||||
$scope.userImport.users = [];
|
||||
$scope.userImport.percent = 0;
|
||||
$scope.userImport.success = 0;
|
||||
$scope.userImport.done = false;
|
||||
},
|
||||
|
||||
handleFileChanged: function () {
|
||||
$scope.userImport.reset();
|
||||
|
||||
var fileInput = document.getElementById('userImportFileInput');
|
||||
if (!fileInput.files || !fileInput.files[0]) return;
|
||||
|
||||
var file = fileInput.files[0];
|
||||
if (file.type !== 'application/json') return console.log('Unsupported file type.');
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.addEventListener('load', function () {
|
||||
$scope.$apply(function () {
|
||||
try {
|
||||
$scope.userImport.users = JSON.parse(reader.result);
|
||||
} catch (e) {
|
||||
console.error('Failed to parse users.', e);
|
||||
$scope.userImport.error = { file: 'Imported file is not valid JSON' };
|
||||
}
|
||||
});
|
||||
}, false);
|
||||
reader.readAsText(file);
|
||||
},
|
||||
|
||||
show: function () {
|
||||
$scope.userImport.reset();
|
||||
|
||||
// named so no duplactes
|
||||
document.getElementById('userImportFileInput').addEventListener('change', $scope.userImport.handleFileChanged);
|
||||
|
||||
$('#userImportModal').modal('show');
|
||||
},
|
||||
|
||||
openFileInput: function () {
|
||||
$('#userImportFileInput').click();
|
||||
},
|
||||
|
||||
import: function () {
|
||||
$scope.userImport.percent = 0;
|
||||
$scope.userImport.success = 0;
|
||||
$scope.userImport.done = false;
|
||||
$scope.userImport.error = { import: [] };
|
||||
$scope.userImport.busy = true;
|
||||
|
||||
var processed = 0;
|
||||
|
||||
async.eachSeries($scope.userImport.users, function (user, callback) {
|
||||
Client.addUser(user, function (error) {
|
||||
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;
|
||||
|
||||
callback();
|
||||
});
|
||||
}, function (error) {
|
||||
if (error) return console.error(error);
|
||||
|
||||
$scope.userImport.busy = false;
|
||||
$scope.userImport.done = true;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
$scope.userExport = function () {
|
||||
Client.getUsers(function (error, result) {
|
||||
if (error) {
|
||||
Client.error('Failed to list users. Full error in the webinspector.');
|
||||
return console.error('Failed to list users.', error);
|
||||
}
|
||||
|
||||
var file = new Blob([ JSON.stringify(result, null, 2) ], { type: 'application/json' });
|
||||
var a = document.createElement('a');
|
||||
a.href = URL.createObjectURL(file);
|
||||
a.download = 'users.json';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
});
|
||||
};
|
||||
|
||||
$scope.userremove = {
|
||||
busy: false,
|
||||
error: null,
|
||||
|
||||
Reference in New Issue
Block a user