Files
grafana/public/app/core/controllers/login_ctrl.ts
T

140 lines
3.6 KiB
TypeScript
Raw Normal View History

2017-12-20 12:33:33 +01:00
import _ from 'lodash';
import coreModule from '../core_module';
import config from 'app/core/config';
export class LoginCtrl {
/** @ngInject */
constructor($scope, backendSrv, contextSrv, $location) {
2015-01-22 11:51:44 +01:00
$scope.formModel = {
2017-12-20 12:33:33 +01:00
user: '',
email: '',
password: '',
};
2018-05-23 11:34:22 +02:00
$scope.command = {};
$scope.result = '';
$scope.loggingIn = false;
2018-05-23 11:34:22 +02:00
contextSrv.sidemenu = false;
2016-09-28 15:10:50 +02:00
$scope.oauth = config.oauth;
$scope.oauthEnabled = _.keys(config.oauth).length > 0;
2018-05-23 11:34:22 +02:00
$scope.ldapEnabled = config.ldapEnabled;
$scope.authProxyEnabled = config.authProxyEnabled;
2016-09-28 15:10:50 +02:00
$scope.disableLoginForm = config.disableLoginForm;
2015-01-29 15:46:36 +01:00
$scope.disableUserSignUp = config.disableUserSignUp;
2017-12-19 16:06:54 +01:00
$scope.loginHint = config.loginHint;
$scope.loginMode = true;
2017-12-20 12:33:33 +01:00
$scope.submitBtnText = 'Log in';
2015-01-22 11:51:44 +01:00
$scope.init = function() {
2017-12-20 12:33:33 +01:00
$scope.$watch('loginMode', $scope.loginModeChanged);
if (config.loginError) {
2017-12-20 12:33:33 +01:00
$scope.appEvent('alert-warning', ['Login Failed', config.loginError]);
}
2015-01-22 11:51:44 +01:00
};
2014-08-12 15:06:54 +02:00
$scope.submit = function() {
if ($scope.loginMode) {
$scope.login();
} else {
$scope.signUp();
}
};
2018-05-23 11:34:22 +02:00
$scope.changeView = function() {
let loginView = document.querySelector('#login-view');
let changePasswordView = document.querySelector('#change-password-view');
loginView.className += ' add';
setTimeout(() => {
loginView.className += ' hidden';
}, 250);
setTimeout(() => {
changePasswordView.classList.remove('hidden');
}, 251);
setTimeout(() => {
changePasswordView.classList.remove('remove');
}, 301);
setTimeout(() => {
document.getElementById('newPassword').focus();
}, 400);
};
$scope.changePassword = function() {
$scope.command.oldPassword = 'admin';
if ($scope.command.newPassword !== $scope.command.confirmNew) {
$scope.appEvent('alert-warning', ['New passwords do not match', '']);
return;
}
backendSrv.put('/api/user/password', $scope.command).then(function() {
$scope.toGrafana();
});
};
$scope.skip = function() {
$scope.toGrafana();
};
2015-01-22 11:51:44 +01:00
$scope.loginModeChanged = function(newValue) {
2017-12-20 12:33:33 +01:00
$scope.submitBtnText = newValue ? 'Log in' : 'Sign up';
};
$scope.signUp = function() {
if (!$scope.loginForm.$valid) {
return;
}
backendSrv.post('/api/user/signup', $scope.formModel).then(function(result) {
if (result.status === 'SignUpCreated') {
$location.path('/signup').search({ email: $scope.formModel.email });
} else {
window.location.href = config.appSubUrl + '/';
}
});
};
2014-08-12 15:06:54 +02:00
$scope.login = function() {
delete $scope.loginError;
2014-08-16 21:54:05 +02:00
2014-08-12 15:06:54 +02:00
if (!$scope.loginForm.$valid) {
return;
}
2017-12-20 12:33:33 +01:00
backendSrv.post('/login', $scope.formModel).then(function(result) {
2018-05-23 11:34:22 +02:00
$scope.result = result;
2018-05-23 11:34:22 +02:00
if ($scope.formModel.password !== 'admin' || $scope.ldapEnabled || $scope.authProxyEnabled) {
$scope.loggingIn = true;
2018-05-23 11:34:22 +02:00
$scope.toGrafana();
return;
} else {
$scope.changeView();
}
2014-08-12 15:06:54 +02:00
});
};
2018-05-23 11:34:22 +02:00
$scope.toGrafana = function() {
var params = $location.search();
if (params.redirect && params.redirect[0] === '/') {
window.location.href = config.appSubUrl + params.redirect;
} else if ($scope.result.redirectUrl) {
window.location.href = $scope.result.redirectUrl;
} else {
window.location.href = config.appSubUrl + '/';
}
};
2014-08-16 21:54:05 +02:00
$scope.init();
}
}
2017-12-20 12:33:33 +01:00
coreModule.controller('LoginCtrl', LoginCtrl);