2016-12-15 03:33:47 +00:00
|
|
|
angular.module('auth', [])
|
2017-03-12 16:24:15 +00:00
|
|
|
.controller('AuthenticationController', ['$scope', '$state', '$stateParams', '$window', '$timeout', '$sanitize', 'Config', 'Authentication', 'Users', 'EndpointService', 'StateManager', 'EndpointProvider', 'Messages',
|
|
|
|
function ($scope, $state, $stateParams, $window, $timeout, $sanitize, Config, Authentication, Users, EndpointService, StateManager, EndpointProvider, Messages) {
|
2016-12-15 03:33:47 +00:00
|
|
|
|
|
|
|
$scope.authData = {
|
|
|
|
username: 'admin',
|
|
|
|
password: '',
|
|
|
|
error: ''
|
|
|
|
};
|
|
|
|
$scope.initPasswordData = {
|
|
|
|
password: '',
|
|
|
|
password_confirmation: '',
|
|
|
|
error: false
|
|
|
|
};
|
|
|
|
|
2017-02-01 09:13:48 +00:00
|
|
|
if (!$scope.applicationState.application.authentication) {
|
2017-03-12 16:24:15 +00:00
|
|
|
EndpointService.endpoints()
|
|
|
|
.then(function success(data) {
|
|
|
|
if (data.length > 0) {
|
|
|
|
endpointID = EndpointProvider.endpointID();
|
|
|
|
if (!endpointID) {
|
|
|
|
endpointID = data[0].Id;
|
|
|
|
EndpointProvider.setEndpointID(endpointID);
|
|
|
|
}
|
|
|
|
StateManager.updateEndpointState(true)
|
|
|
|
.then(function success() {
|
|
|
|
$state.go('dashboard');
|
|
|
|
}, function error(err) {
|
|
|
|
Messages.error("Failure", err, 'Unable to connect to the Docker endpoint');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2017-02-01 09:13:48 +00:00
|
|
|
$state.go('endpointInit');
|
2017-03-12 16:24:15 +00:00
|
|
|
}
|
|
|
|
}, function error(err) {
|
|
|
|
Messages.error("Failure", err, 'Unable to retrieve endpoints');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Users.checkAdminUser({}, function () {},
|
|
|
|
function (e) {
|
|
|
|
if (e.status === 404) {
|
|
|
|
$scope.initPassword = true;
|
2017-02-01 09:13:48 +00:00
|
|
|
} else {
|
2017-03-12 16:24:15 +00:00
|
|
|
Messages.error("Failure", e, 'Unable to verify administrator account existence');
|
2017-02-01 09:13:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-15 03:33:47 +00:00
|
|
|
if ($stateParams.logout) {
|
|
|
|
Authentication.logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stateParams.error) {
|
|
|
|
$scope.authData.error = $stateParams.error;
|
|
|
|
Authentication.logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Authentication.isAuthenticated()) {
|
|
|
|
$state.go('dashboard');
|
|
|
|
}
|
|
|
|
|
|
|
|
Config.$promise.then(function (c) {
|
|
|
|
$scope.logo = c.logo;
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.createAdminUser = function() {
|
|
|
|
var password = $sanitize($scope.initPasswordData.password);
|
|
|
|
Users.initAdminUser({password: password}, function (d) {
|
|
|
|
$scope.initPassword = false;
|
|
|
|
$timeout(function() {
|
|
|
|
var element = $window.document.getElementById('password');
|
|
|
|
if(element) {
|
|
|
|
element.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, function (e) {
|
|
|
|
$scope.initPassword.error = true;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.authenticateUser = function() {
|
|
|
|
$scope.authenticationError = false;
|
|
|
|
var username = $sanitize($scope.authData.username);
|
|
|
|
var password = $sanitize($scope.authData.password);
|
2017-03-12 16:24:15 +00:00
|
|
|
Authentication.login(username, password)
|
|
|
|
.then(function success(data) {
|
|
|
|
return EndpointService.endpoints();
|
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
var userDetails = Authentication.getUserDetails();
|
|
|
|
if (data.length > 0) {
|
|
|
|
endpointID = EndpointProvider.endpointID();
|
|
|
|
if (!endpointID) {
|
|
|
|
endpointID = data[0].Id;
|
|
|
|
EndpointProvider.setEndpointID(endpointID);
|
|
|
|
}
|
2017-01-22 23:14:34 +00:00
|
|
|
StateManager.updateEndpointState(true)
|
|
|
|
.then(function success() {
|
|
|
|
$state.go('dashboard');
|
|
|
|
}, function error(err) {
|
|
|
|
Messages.error("Failure", err, 'Unable to connect to the Docker endpoint');
|
|
|
|
});
|
2017-03-12 16:24:15 +00:00
|
|
|
}
|
|
|
|
else if (data.length === 0 && userDetails.role === 1) {
|
|
|
|
$state.go('endpointInit');
|
|
|
|
} else if (data.length === 0 && userDetails.role === 2) {
|
|
|
|
$scope.authData.error = 'User not allowed. Please contact your administrator.';
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
$scope.authData.error = 'Authentication error';
|
2016-12-15 03:33:47 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}]);
|