ProfileCtrl.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import config from 'app/core/config';
  2. import { coreModule } from 'app/core/core';
  3. import { dateTime } from '@grafana/ui';
  4. import { UserSession } from 'app/types';
  5. export class ProfileCtrl {
  6. user: any;
  7. oldTheme: any;
  8. teams: any = [];
  9. orgs: any = [];
  10. sessions: object[] = [];
  11. userForm: any;
  12. showTeamsList = false;
  13. showOrgsList = false;
  14. readonlyLoginFields = config.disableLoginForm;
  15. navModel: any;
  16. /** @ngInject */
  17. constructor(private backendSrv, private contextSrv, private $location, navModelSrv) {
  18. this.getUser();
  19. this.getUserSessions();
  20. this.getUserTeams();
  21. this.getUserOrgs();
  22. this.navModel = navModelSrv.getNav('profile', 'profile-settings', 0);
  23. }
  24. getUser() {
  25. this.backendSrv.get('/api/user').then(user => {
  26. this.user = user;
  27. this.user.theme = user.theme || 'dark';
  28. });
  29. }
  30. getUserSessions() {
  31. this.backendSrv.get('/api/user/auth-tokens').then((sessions: UserSession[]) => {
  32. sessions.reverse();
  33. const found = sessions.findIndex((session: UserSession) => {
  34. return session.isActive;
  35. });
  36. if (found) {
  37. const now = sessions[found];
  38. sessions.splice(found, found);
  39. sessions.unshift(now);
  40. }
  41. this.sessions = sessions.map((session: UserSession) => {
  42. return {
  43. id: session.id,
  44. isActive: session.isActive,
  45. seenAt: dateTime(session.seenAt).fromNow(),
  46. createdAt: dateTime(session.createdAt).format('MMMM DD, YYYY'),
  47. clientIp: session.clientIp,
  48. browser: session.browser,
  49. browserVersion: session.browserVersion,
  50. os: session.os,
  51. osVersion: session.osVersion,
  52. device: session.device,
  53. };
  54. });
  55. });
  56. }
  57. revokeUserSession(tokenId: number) {
  58. this.backendSrv
  59. .post('/api/user/revoke-auth-token', {
  60. authTokenId: tokenId,
  61. })
  62. .then(() => {
  63. this.sessions = this.sessions.filter((session: UserSession) => {
  64. if (session.id === tokenId) {
  65. return false;
  66. }
  67. return true;
  68. });
  69. });
  70. }
  71. getUserTeams() {
  72. this.backendSrv.get('/api/user/teams').then(teams => {
  73. this.teams = teams;
  74. this.showTeamsList = this.teams.length > 0;
  75. });
  76. }
  77. getUserOrgs() {
  78. this.backendSrv.get('/api/user/orgs').then(orgs => {
  79. this.orgs = orgs;
  80. this.showOrgsList = orgs.length > 1;
  81. });
  82. }
  83. setUsingOrg(org) {
  84. this.backendSrv.post('/api/user/using/' + org.orgId).then(() => {
  85. window.location.href = config.appSubUrl + '/profile';
  86. });
  87. }
  88. update() {
  89. if (!this.userForm.$valid) {
  90. return;
  91. }
  92. this.backendSrv.put('/api/user/', this.user).then(() => {
  93. this.contextSrv.user.name = this.user.name || this.user.login;
  94. if (this.oldTheme !== this.user.theme) {
  95. window.location.href = config.appSubUrl + this.$location.path();
  96. }
  97. });
  98. }
  99. }
  100. coreModule.controller('ProfileCtrl', ProfileCtrl);