login_ctrl.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import _ from 'lodash';
  2. import coreModule from '../core_module';
  3. import config from 'app/core/config';
  4. export class LoginCtrl {
  5. /** @ngInject */
  6. constructor($scope, backendSrv, contextSrv, $location) {
  7. $scope.formModel = {
  8. user: '',
  9. email: '',
  10. password: '',
  11. };
  12. $scope.command = {};
  13. $scope.result = '';
  14. contextSrv.sidemenu = false;
  15. $scope.oauth = config.oauth;
  16. $scope.oauthEnabled = _.keys(config.oauth).length > 0;
  17. $scope.ldapEnabled = config.ldapEnabled;
  18. $scope.authProxyEnabled = config.authProxyEnabled;
  19. $scope.disableLoginForm = config.disableLoginForm;
  20. $scope.disableUserSignUp = config.disableUserSignUp;
  21. $scope.loginHint = config.loginHint;
  22. $scope.loginMode = true;
  23. $scope.submitBtnText = 'Log in';
  24. $scope.init = function() {
  25. $scope.$watch('loginMode', $scope.loginModeChanged);
  26. if (config.loginError) {
  27. $scope.appEvent('alert-warning', ['Login Failed', config.loginError]);
  28. }
  29. };
  30. $scope.submit = function() {
  31. if ($scope.loginMode) {
  32. $scope.login();
  33. } else {
  34. $scope.signUp();
  35. }
  36. };
  37. $scope.changeView = function() {
  38. let loginView = document.querySelector('#login-view');
  39. let changePasswordView = document.querySelector('#change-password-view');
  40. loginView.className += ' add';
  41. setTimeout(() => {
  42. loginView.className += ' hidden';
  43. }, 250);
  44. setTimeout(() => {
  45. changePasswordView.classList.remove('hidden');
  46. }, 251);
  47. setTimeout(() => {
  48. changePasswordView.classList.remove('remove');
  49. }, 301);
  50. setTimeout(() => {
  51. document.getElementById('newPassword').focus();
  52. }, 400);
  53. };
  54. $scope.changePassword = function() {
  55. $scope.command.oldPassword = 'admin';
  56. if ($scope.command.newPassword !== $scope.command.confirmNew) {
  57. $scope.appEvent('alert-warning', ['New passwords do not match', '']);
  58. return;
  59. }
  60. backendSrv.put('/api/user/password', $scope.command).then(function() {
  61. $scope.toGrafana();
  62. });
  63. };
  64. $scope.skip = function() {
  65. $scope.toGrafana();
  66. };
  67. $scope.loginModeChanged = function(newValue) {
  68. $scope.submitBtnText = newValue ? 'Log in' : 'Sign up';
  69. };
  70. $scope.signUp = function() {
  71. if (!$scope.loginForm.$valid) {
  72. return;
  73. }
  74. backendSrv.post('/api/user/signup', $scope.formModel).then(function(result) {
  75. if (result.status === 'SignUpCreated') {
  76. $location.path('/signup').search({ email: $scope.formModel.email });
  77. } else {
  78. window.location.href = config.appSubUrl + '/';
  79. }
  80. });
  81. };
  82. $scope.login = function() {
  83. delete $scope.loginError;
  84. if (!$scope.loginForm.$valid) {
  85. return;
  86. }
  87. backendSrv.post('/login', $scope.formModel).then(function(result) {
  88. $scope.result = result;
  89. if ($scope.formModel.password !== 'admin' || $scope.ldapEnabled || $scope.authProxyEnabled) {
  90. $scope.toGrafana();
  91. return;
  92. }
  93. $scope.changeView();
  94. });
  95. };
  96. $scope.toGrafana = function() {
  97. var params = $location.search();
  98. if (params.redirect && params.redirect[0] === '/') {
  99. window.location.href = config.appSubUrl + params.redirect;
  100. } else if ($scope.result.redirectUrl) {
  101. window.location.href = $scope.result.redirectUrl;
  102. } else {
  103. window.location.href = config.appSubUrl + '/';
  104. }
  105. };
  106. $scope.init();
  107. }
  108. }
  109. coreModule.controller('LoginCtrl', LoginCtrl);