UserStore.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import Reflux from 'reflux';
  15. import UserActions from '../../actions/UserActions';
  16. import ConnectionsActions from '../../actions/ConnectionsActions'
  17. import Location from '../../core/Location';
  18. import Api from '../../components/ApiCaller';
  19. import cookie from 'react-cookie';
  20. import moment from 'moment';
  21. import { servicesUrl } from '../../config'
  22. class UserStore extends Reflux.Store
  23. {
  24. user = {
  25. id: null,
  26. name: "Pat Bentar",
  27. email: "pat.bentar@coriolis.ui",
  28. created: new Date(),
  29. project: {},
  30. roles: [],
  31. projects: null,
  32. token: null,
  33. settings: {
  34. notifications: true
  35. }
  36. }
  37. constructor() {
  38. super()
  39. this.listenables = UserActions
  40. this.state = {
  41. currentUser: this.user,
  42. loadingState: false
  43. }
  44. let token = cookie.load('token')
  45. let projectId = cookie.load('projectId')
  46. if (token && projectId) {
  47. UserActions.tokenLogin(token, projectId)
  48. }
  49. }
  50. onLogin() {
  51. Api.setDefaultHeader('X-Auth-Token', null)
  52. this.setState({ loadingState: true })
  53. }
  54. onLoginSuccess(response) {
  55. let token = response.headers['X-Subject-Token']
  56. Api.setDefaultHeader('X-Auth-Token', token)
  57. cookie.save('unscopedToken', token, { path: "/", expires: moment().add(1, 'hour').toDate() })
  58. UserActions.getScopedProjects(res => {
  59. if (res.data.projects) {
  60. let projectId = cookie.load('projectId')
  61. if (!projectId) {
  62. projectId = res.data.projects[0].id
  63. }
  64. UserActions.loginScope(token, projectId)
  65. } else {
  66. // TODO: Error case no scoped projects
  67. }
  68. })
  69. }
  70. onLoginScopeSuccess(response) {
  71. this.setState({ loadingState: false })
  72. let currentUser = this.state.currentUser
  73. currentUser.id = response.data.token.user.id
  74. currentUser.name = response.data.token.user.name
  75. currentUser.token = response.headers['X-Subject-Token']
  76. currentUser.project = response.data.token.project
  77. cookie.save('token', currentUser.token, { path: "/", expires: moment().add(1, 'hour').toDate() })
  78. cookie.save('projectId', currentUser.project.id, { path: "/", expires: moment().add(1, 'months').toDate() })
  79. Api.setDefaultHeader('X-Auth-Token', currentUser.token)
  80. this.setState({ currentUser: currentUser })
  81. ConnectionsActions.loadProviders()
  82. ConnectionsActions.loadConnections()
  83. if (window.location.pathname == "/" || window.location.pathname == "/login") {
  84. Location.push('/replicas');
  85. }
  86. UserActions.getScopedProjects()
  87. }
  88. onLoginScopeFailed(token) {
  89. // In case the scoping the project id from cookie didn't work, fallback to first project in list
  90. UserActions.loginScope(token, this.state.currentUser.projects[0].id, false)
  91. }
  92. onLoginFailed() {
  93. this.setState({ loadingState: false })
  94. }
  95. onLogout() {
  96. Api.sendAjaxRequest({
  97. url: servicesUrl.identity,
  98. method: "DELETE",
  99. headers: { 'X-Subject-Token': this.state.currentUser.token }
  100. })
  101. .then(() => {
  102. cookie.remove('token');
  103. window.location.href = "/"
  104. })
  105. .catch(() => {
  106. cookie.remove('token');
  107. window.location.href = "/"
  108. })
  109. Api.resetHeaders()
  110. }
  111. onTokenLoginFailed() {
  112. cookie.remove('token');
  113. cookie.remove('projectId');
  114. Api.resetHeaders()
  115. Location.push('/login');
  116. }
  117. onLogoutSuccess() {
  118. cookie.remove('token');
  119. cookie.remove('projectId');
  120. Location.push('/login');
  121. }
  122. onSetCurrentUser(userId) {
  123. this.state.users.forEach(user => {
  124. if (user.id == userId) {
  125. this.setState({ currentUser: user })
  126. }
  127. }, this)
  128. }
  129. onSwitchProject(projectId) {
  130. if (projectId) {
  131. let token = cookie.load('unscopedToken')
  132. Api.setDefaultHeader('X-Auth-Token', null)
  133. UserActions.loginScope(token, projectId)
  134. }
  135. }
  136. onGetScopedProjectsCompleted(response) {
  137. let currentUser = this.state.currentUser
  138. currentUser.projects = response.data.projects
  139. this.setState({ currentUser: currentUser })
  140. }
  141. onFederateToken(token) {
  142. Api.setDefaultHeader('X-Auth-Token', token)
  143. cookie.save('unscopedToken', token, { path: "/", expires: moment().add(1, 'hour').toDate() })
  144. UserActions.getScopedProjects(response => {
  145. if (response.data.projects) {
  146. UserActions.loginScope(token, response.data.projects[0].id)
  147. } else {
  148. // TODO: Error case no scoped projects
  149. }
  150. })
  151. }
  152. }
  153. UserStore.id = "userStore"
  154. export default UserStore;