UserStore.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. scoped: false,
  27. name: "-",
  28. email: "-",
  29. created: new Date(),
  30. project: {},
  31. roles: [],
  32. projects: null,
  33. token: null,
  34. settings: {
  35. notifications: true
  36. }
  37. }
  38. constructor() {
  39. super()
  40. this.listenables = UserActions
  41. this.state = {
  42. currentUser: this.user,
  43. loadingState: false
  44. }
  45. let token = cookie.load('token')
  46. let projectId = cookie.load('projectId')
  47. if (token && projectId) {
  48. UserActions.tokenLogin(token, projectId)
  49. }
  50. }
  51. onLogin() {
  52. Api.setDefaultHeader('X-Auth-Token', null)
  53. this.setState({ loadingState: true })
  54. }
  55. onLoginSuccess(response) {
  56. let token = response.headers['X-Subject-Token'] || response.headers['x-subject-token']
  57. Api.setDefaultHeader('X-Auth-Token', token)
  58. cookie.save('unscopedToken', token, { path: "/", expires: moment().add(1, 'hour').toDate() })
  59. UserActions.getScopedProjects(res => {
  60. if (res.data.projects) {
  61. let projectId = cookie.load('projectId')
  62. if (!projectId) {
  63. projectId = res.data.projects[0].id
  64. }
  65. UserActions.loginScope(token, projectId)
  66. } else {
  67. // TODO: Error case no scoped projects
  68. }
  69. })
  70. }
  71. onLoginScope() {
  72. let currentUser = this.state.currentUser
  73. currentUser.scoped = false
  74. this.setState({ currentUser: currentUser })
  75. }
  76. onLoginScopeSuccess(response) {
  77. this.setState({ loadingState: false })
  78. let currentUser = this.state.currentUser
  79. currentUser.id = response.data.token.user.id
  80. currentUser.name = response.data.token.user.name
  81. currentUser.token = response.headers['X-Subject-Token'] || response.headers['x-subject-token']
  82. currentUser.project = response.data.token.project
  83. currentUser.scoped = true
  84. cookie.save('token', currentUser.token, { path: "/", expires: moment().add(1, 'hour').toDate() })
  85. cookie.save('projectId', currentUser.project.id, { path: "/", expires: moment().add(1, 'months').toDate() })
  86. Api.setDefaultHeader('X-Auth-Token', currentUser.token)
  87. this.setState({ currentUser: currentUser })
  88. ConnectionsActions.loadProviders()
  89. ConnectionsActions.loadConnections()
  90. UserActions.getScopedProjects()
  91. UserActions.getUserInfo(currentUser.id)
  92. if (window.location.pathname == "/" || window.location.pathname == "/login") {
  93. Location.push('/replicas');
  94. }
  95. }
  96. onLoginScopeFailed(token) {
  97. // In case the scoping the project id from cookie didn't work, fallback to first project in list
  98. UserActions.loginScope(token, this.state.currentUser.projects[0].id, false)
  99. }
  100. onLoginFailed() {
  101. this.setState({ loadingState: false })
  102. }
  103. onLogout() {
  104. Api.sendAjaxRequest({
  105. url: servicesUrl.identity,
  106. method: "DELETE",
  107. headers: { 'X-Subject-Token': this.state.currentUser.token }
  108. })
  109. .then(() => {
  110. cookie.remove('token');
  111. window.location.href = "/"
  112. })
  113. .catch(() => {
  114. cookie.remove('token');
  115. window.location.href = "/"
  116. })
  117. Api.resetHeaders()
  118. }
  119. onGetUserInfoCompleted(response) {
  120. let currentUser = this.state.currentUser
  121. currentUser.email = response.data.user.email
  122. this.setState({ currentUser: currentUser })
  123. }
  124. onSetUserInfoSuccess() {
  125. }
  126. onTokenLoginFailed() {
  127. cookie.remove('token');
  128. cookie.remove('projectId');
  129. Api.resetHeaders()
  130. Location.push('/login');
  131. }
  132. onLogoutSuccess() {
  133. cookie.remove('token');
  134. cookie.remove('projectId');
  135. Location.push('/login');
  136. }
  137. onSetCurrentUser(userId) {
  138. this.state.users.forEach(user => {
  139. if (user.id == userId) {
  140. this.setState({ currentUser: user })
  141. }
  142. }, this)
  143. }
  144. onSwitchProject(projectId) {
  145. if (projectId) {
  146. let token = cookie.load('unscopedToken')
  147. Api.setDefaultHeader('X-Auth-Token', null)
  148. UserActions.loginScope(token, projectId)
  149. }
  150. }
  151. onGetScopedProjectsCompleted(response) {
  152. let currentUser = this.state.currentUser
  153. currentUser.projects = response.data.projects
  154. this.setState({ currentUser: currentUser })
  155. }
  156. onFederateToken(token) {
  157. Api.setDefaultHeader('X-Auth-Token', token)
  158. cookie.save('unscopedToken', token, { path: "/", expires: moment().add(1, 'hour').toDate() })
  159. UserActions.getScopedProjects(response => {
  160. if (response.data.projects) {
  161. UserActions.loginScope(token, response.data.projects[0].id)
  162. } else {
  163. // TODO: Error case no scoped projects
  164. }
  165. })
  166. }
  167. }
  168. UserStore.id = "userStore"
  169. export default UserStore;