UserStore.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 { servicesUrl } from '../../config'
  21. class UserStore extends Reflux.Store
  22. {
  23. user = {
  24. id: null,
  25. name: "Pat Bentar",
  26. email: "pat.bentar@coriolis.ui",
  27. created: new Date(),
  28. project: {},
  29. roles: [],
  30. projects: null,
  31. token: null,
  32. settings: {
  33. notifications: true
  34. }
  35. }
  36. constructor() {
  37. super()
  38. this.listenables = UserActions
  39. this.state = {
  40. currentUser: this.user,
  41. loadingState: false
  42. }
  43. let token = cookie.load('token')
  44. let projectId = cookie.load('projectId')
  45. if (token && projectId) {
  46. UserActions.tokenLogin(token, projectId)
  47. }
  48. }
  49. onLogin() {
  50. Api.setDefaultHeader('X-Auth-Token', null)
  51. this.setState({ loadingState: true })
  52. }
  53. onLoginSuccess(response) {
  54. let token = response.headers['X-Subject-Token']
  55. Api.setDefaultHeader('X-Auth-Token', token)
  56. cookie.save('unscopedToken', token, { path: "/" })
  57. UserActions.getScopedProjects(response => {
  58. if (response.data.projects) {
  59. let projectId = cookie.load('projectId')
  60. if (!projectId) {
  61. projectId = response.data.projects[0].id
  62. }
  63. UserActions.loginScope(token, projectId)
  64. } else {
  65. // TODO: Error case no scoped projects
  66. }
  67. })
  68. }
  69. onLoginScopeSuccess(response) {
  70. this.setState({ loadingState: false })
  71. let currentUser = this.state.currentUser
  72. currentUser.id = response.data.token.user.id
  73. currentUser.name = response.data.token.user.name
  74. currentUser.token = response.headers['X-Subject-Token']
  75. currentUser.project = response.data.token.project
  76. cookie.save('token', currentUser.token, { path: "/" })
  77. cookie.save('projectId', currentUser.project.id, { path: "/" })
  78. Api.setDefaultHeader('X-Auth-Token', currentUser.token)
  79. this.setState({currentUser: currentUser})
  80. ConnectionsActions.loadProviders()
  81. ConnectionsActions.loadConnections()
  82. if (window.location.pathname == "/" || window.location.pathname == "/login") {
  83. Location.push('/replicas');
  84. }
  85. UserActions.getScopedProjects()
  86. }
  87. onLoginScopeFailed(token) {
  88. // In case the scoping the project id from cookie didn't work, fallback to first project in list
  89. UserActions.loginScope(token, this.state.currentUser.projects[0].id, false)
  90. }
  91. onLoginFailed(response) {
  92. this.setState({ loadingState: false })
  93. }
  94. onLogout() {
  95. Api.sendAjaxRequest({
  96. url: servicesUrl.identity,
  97. method: "DELETE",
  98. headers: { 'X-Subject-Token': this.state.currentUser.token }
  99. })
  100. .then(() => {
  101. cookie.remove('token');
  102. window.location.href = "/"
  103. })
  104. .catch(() => {
  105. cookie.remove('token');
  106. window.location.href = "/"
  107. })
  108. Api.resetHeaders()
  109. }
  110. onTokenLoginFailed() {
  111. cookie.remove('token');
  112. cookie.remove('projectId');
  113. Api.resetHeaders()
  114. Location.push('/login');
  115. }
  116. onLogoutSuccess(response) {
  117. cookie.remove('token');
  118. cookie.remove('projectId');
  119. Location.push('/login');
  120. }
  121. onSetCurrentUser(userId) {
  122. this.state.users.forEach(user => {
  123. if (user.id == userId) {
  124. this.setState({ currentUser: user })
  125. }
  126. }, this)
  127. }
  128. onSwitchProject(projectId) {
  129. console.log("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: "/" })
  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;