profile_ctrl.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import config from 'app/core/config';
  2. import { coreModule } from 'app/core/core';
  3. export class ProfileCtrl {
  4. user: any;
  5. old_theme: any;
  6. orgs: any = [];
  7. userForm: any;
  8. showOrgsList = false;
  9. readonlyLoginFields = config.disableLoginForm;
  10. navModel: any;
  11. /** @ngInject **/
  12. constructor(
  13. private backendSrv,
  14. private contextSrv,
  15. private $location,
  16. navModelSrv
  17. ) {
  18. this.getUser();
  19. this.getUserOrgs();
  20. this.navModel = navModelSrv.getNav('profile', 'profile-settings', 0);
  21. }
  22. getUser() {
  23. this.backendSrv.get('/api/user').then(user => {
  24. this.user = user;
  25. this.user.theme = user.theme || 'dark';
  26. });
  27. }
  28. getUserOrgs() {
  29. this.backendSrv.get('/api/user/orgs').then(orgs => {
  30. this.orgs = orgs;
  31. this.showOrgsList = orgs.length > 1;
  32. });
  33. }
  34. setUsingOrg(org) {
  35. this.backendSrv.post('/api/user/using/' + org.orgId).then(() => {
  36. window.location.href = config.appSubUrl + '/profile';
  37. });
  38. }
  39. update() {
  40. if (!this.userForm.$valid) {
  41. return;
  42. }
  43. this.backendSrv.put('/api/user/', this.user).then(() => {
  44. this.contextSrv.user.name = this.user.name || this.user.login;
  45. if (this.old_theme !== this.user.theme) {
  46. window.location.href = config.appSubUrl + this.$location.path();
  47. }
  48. });
  49. }
  50. }
  51. coreModule.controller('ProfileCtrl', ProfileCtrl);