ProfileCtrl.ts 3.1 KB

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