portainer/app/portainer/views/init/admin/initAdminController.js

68 lines
2.0 KiB
JavaScript

angular.module('portainer.app')
.controller('InitAdminController', ['$scope', '$state', 'Notifications', 'Authentication', 'StateManager', 'UserService', 'EndpointService', 'ExtensionService',
function ($scope, $state, Notifications, Authentication, StateManager, UserService, EndpointService, ExtensionService) {
$scope.logo = StateManager.getState().application.logo;
$scope.formValues = {
Username: 'admin',
Password: '',
ConfirmPassword: ''
};
$scope.state = {
actionInProgress: false
};
async function retrieveAndSaveEnabledExtensions() {
try {
await ExtensionService.retrieveAndSaveEnabledExtensions();
} catch (err) {
Notifications.error('Failure', err, 'Unable to retrieve enabled extensions');
}
}
$scope.createAdminUser = function() {
var username = $scope.formValues.Username;
var password = $scope.formValues.Password;
$scope.state.actionInProgress = true;
UserService.initAdministrator(username, password)
.then(function success() {
return Authentication.login(username, password);
})
.then(function success() {
return retrieveAndSaveEnabledExtensions();
})
.then(function () {
return EndpointService.endpoints();
})
.then(function success(data) {
if (data.length === 0) {
$state.go('portainer.init.endpoint');
} else {
$state.go('portainer.home');
}
})
.catch(function error(err) {
Notifications.error('Failure', err, 'Unable to create administrator user');
})
.finally(function final() {
$scope.state.actionInProgress = false;
});
};
function createAdministratorFlow() {
UserService.administratorExists()
.then(function success(exists) {
if (exists) {
$state.go('portainer.home');
}
})
.catch(function error(err) {
Notifications.error('Failure', err, 'Unable to verify administrator account existence');
});
}
createAdministratorFlow();
}]);