UserStore.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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: "-",
  27. email: "-",
  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'] || 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'] || 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. UserActions.getScopedProjects()
  84. UserActions.getUserInfo(currentUser.id)
  85. if (window.location.pathname == "/" || window.location.pathname == "/login") {
  86. Location.push('/replicas');
  87. }
  88. }
  89. onLoginScopeFailed(token) {
  90. // In case the scoping the project id from cookie didn't work, fallback to first project in list
  91. UserActions.loginScope(token, this.state.currentUser.projects[0].id, false)
  92. }
  93. onLoginFailed() {
  94. this.setState({ loadingState: false })
  95. }
  96. onLogout() {
  97. Api.sendAjaxRequest({
  98. url: servicesUrl.identity,
  99. method: "DELETE",
  100. headers: { 'X-Subject-Token': this.state.currentUser.token }
  101. })
  102. .then(() => {
  103. cookie.remove('token');
  104. window.location.href = "/"
  105. })
  106. .catch(() => {
  107. cookie.remove('token');
  108. window.location.href = "/"
  109. })
  110. Api.resetHeaders()
  111. }
  112. onGetUserInfoCompleted(response) {
  113. let currentUser = this.state.currentUser
  114. currentUser.email = response.data.user.email
  115. this.setState({ currentUser: currentUser })
  116. }
  117. onSetUserInfoSuccess() {
  118. }
  119. onTokenLoginFailed() {
  120. cookie.remove('token');
  121. cookie.remove('projectId');
  122. Api.resetHeaders()
  123. Location.push('/login');
  124. }
  125. onLogoutSuccess() {
  126. cookie.remove('token');
  127. cookie.remove('projectId');
  128. Location.push('/login');
  129. }
  130. onSetCurrentUser(userId) {
  131. this.state.users.forEach(user => {
  132. if (user.id == userId) {
  133. this.setState({ currentUser: user })
  134. }
  135. }, this)
  136. }
  137. onSwitchProject(projectId) {
  138. if (projectId) {
  139. let token = cookie.load('unscopedToken')
  140. if (token) {
  141. Api.setDefaultHeader('X-Auth-Token', null)
  142. UserActions.loginScope(token, projectId)
  143. } else {
  144. UserActions.logout()
  145. }
  146. }
  147. }
  148. onGetScopedProjectsCompleted(response) {
  149. let currentUser = this.state.currentUser
  150. currentUser.projects = response.data.projects
  151. this.setState({ currentUser: currentUser })
  152. }
  153. onFederateToken(token) {
  154. Api.setDefaultHeader('X-Auth-Token', token)
  155. cookie.save('unscopedToken', token, { path: "/", expires: moment().add(1, 'hour').toDate() })
  156. UserActions.getScopedProjects(response => {
  157. if (response.data.projects) {
  158. UserActions.loginScope(token, response.data.projects[0].id)
  159. } else {
  160. // TODO: Error case no scoped projects
  161. }
  162. })
  163. }
  164. }
  165. UserStore.id = "userStore"
  166. export default UserStore;