profile_ctrl.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. teams: any = [];
  7. orgs: any = [];
  8. userForm: any;
  9. showTeamsList = false;
  10. showOrgsList = false;
  11. readonlyLoginFields = config.disableLoginForm;
  12. navModel: any;
  13. /** @ngInject **/
  14. constructor(private backendSrv, private contextSrv, private $location, navModelSrv) {
  15. this.getUser();
  16. this.getUserTeams();
  17. this.getUserOrgs();
  18. this.navModel = navModelSrv.getNav('profile', 'profile-settings', 0);
  19. }
  20. getUser() {
  21. this.backendSrv.get('/api/user').then(user => {
  22. this.user = user;
  23. this.user.theme = user.theme || 'dark';
  24. });
  25. }
  26. getUserTeams() {
  27. this.backendSrv.get('/api/user').then(teams => {
  28. this.user.teams = [
  29. { name: 'Backend', email: 'backend@grafana.com', members: 5 },
  30. { name: 'Frontend', email: 'frontend@grafana.com', members: 4 },
  31. { name: 'Ops', email: 'ops@grafana.com', members: 6 },
  32. ];
  33. this.showTeamsList = this.user.teams.length > 1;
  34. });
  35. }
  36. getUserOrgs() {
  37. this.backendSrv.get('/api/user/orgs').then(orgs => {
  38. this.orgs = orgs;
  39. this.showOrgsList = orgs.length > 1;
  40. });
  41. }
  42. setUsingOrg(org) {
  43. this.backendSrv.post('/api/user/using/' + org.orgId).then(() => {
  44. window.location.href = config.appSubUrl + '/profile';
  45. });
  46. }
  47. update() {
  48. if (!this.userForm.$valid) {
  49. return;
  50. }
  51. this.backendSrv.put('/api/user/', this.user).then(() => {
  52. this.contextSrv.user.name = this.user.name || this.user.login;
  53. if (this.old_theme !== this.user.theme) {
  54. window.location.href = config.appSubUrl + this.$location.path();
  55. }
  56. });
  57. }
  58. }
  59. coreModule.controller('ProfileCtrl', ProfileCtrl);