UserActions.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 Api from '../../components/ApiCaller';
  16. import { servicesUrl, defaultDomain } from '../../config';
  17. let UserAction = Reflux.createActions({
  18. login: { children: ["success", "failed"] },
  19. loginGoogle: { children: ["success", "failed"] },
  20. loginScope: { children: ["success", "failed"] },
  21. logout: {},
  22. tokenLogin: { children: ["failed"] },
  23. setCurrentUser: {},
  24. switchProject: {},
  25. getScopedProjects: { children: ["completed", "failed"] },
  26. loadProjects: { children: ["completed", "failed"] },
  27. federateToken: { },
  28. getUserInfo: { children: ["completed", "failed"] },
  29. setUserInfo: { children: ["success", "failed"] }
  30. })
  31. UserAction.login.listen((userData, callback = null) => {
  32. let auth = {
  33. auth: {
  34. identity: {
  35. methods: [
  36. "password"
  37. ],
  38. password: {
  39. user: {
  40. name: userData.name,
  41. domain: {
  42. name: userData.domain ? userData.domain : defaultDomain
  43. },
  44. password: userData.password
  45. }
  46. }
  47. },
  48. scope: "unscoped"
  49. }
  50. }
  51. Api.setDefaultHeader({ "X-Auth-Token": null })
  52. Api.sendAjaxRequest({
  53. url: servicesUrl.identity,
  54. method: "POST",
  55. data: auth
  56. })
  57. .then((response) => {
  58. UserAction.login.success(response)
  59. }, () => {
  60. UserAction.login.failed()
  61. if (typeof callback == "function") {
  62. callback()
  63. }
  64. })
  65. })
  66. UserAction.loginScope.listen((token, projectId, fallback = true) => {
  67. let auth = {
  68. auth: {
  69. identity: {
  70. methods: [
  71. "token"
  72. ],
  73. token: {
  74. id: token
  75. }
  76. },
  77. scope: {
  78. project: {
  79. id: projectId
  80. }
  81. }
  82. }
  83. }
  84. Api.setDefaultHeader({ "X-Auth-Token": null })
  85. Api.sendAjaxRequest({
  86. url: servicesUrl.identity,
  87. method: "POST",
  88. data: auth
  89. })
  90. .then((response) => {
  91. UserAction.loginScope.success(response)
  92. }, () => {
  93. if (fallback) {
  94. UserAction.loginScope.failed(token)
  95. }
  96. })
  97. })
  98. UserAction.tokenLogin.listen((token) => {
  99. Api.sendAjaxRequest({
  100. url: servicesUrl.identity,
  101. method: "GET",
  102. headers: { 'X-Subject-Token': token }
  103. })
  104. .then(UserAction.login.success, UserAction.tokenLogin.failed)
  105. .catch((response) => {
  106. UserAction.tokenLogin.failed(response)
  107. });
  108. })
  109. UserAction.getScopedProjects.listen((callback) => {
  110. Api.sendAjaxRequest({
  111. url: servicesUrl.projects,
  112. method: "GET"
  113. })
  114. .then(
  115. (response) => {
  116. if (callback) {
  117. callback(response)
  118. }
  119. UserAction.getScopedProjects.completed(response)
  120. }, UserAction.getScopedProjects.failed)
  121. .catch((response) => {
  122. UserAction.getScopedProjects.failed(response)
  123. });
  124. })
  125. UserAction.getUserInfo.listen((userId) => {
  126. Api.sendAjaxRequest({
  127. url: servicesUrl.users + "/" + userId,
  128. method: "GET"
  129. })
  130. .then(
  131. (response) => {
  132. UserAction.getUserInfo.completed(response)
  133. }, UserAction.getUserInfo.failed)
  134. .catch((response) => {
  135. UserAction.getUserInfo.failed(response)
  136. });
  137. })
  138. UserAction.setUserInfo.listen((userId, userObject) => {
  139. Api.sendAjaxRequest({
  140. url: servicesUrl.users + "/" + userId,
  141. method: "PATCH",
  142. data: {
  143. user: userObject
  144. }
  145. })
  146. .then(
  147. (response) => {
  148. UserAction.setUserInfo.completed(response)
  149. }, UserAction.setUserInfo.failed)
  150. .catch((response) => {
  151. UserAction.setUserInfo.failed(response)
  152. });
  153. })
  154. export default UserAction;