ProjectSource.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // @flow
  15. import Api from '../utils/ApiCaller'
  16. import UserSource from './UserSource'
  17. import configLoader from '../utils/Config'
  18. import type { Project, Role, RoleAssignment } from '../types/Project'
  19. import type { User } from '../types/User'
  20. class ProjectsSource {
  21. async getProjects(skipLog?: boolean): Promise<Project[]> {
  22. let response = await Api.send({
  23. url: `${configLoader.config.servicesUrls.keystone}/auth/projects`,
  24. skipLog,
  25. })
  26. if (response.data.projects) {
  27. let projects: Project[] = response.data.projects
  28. projects.sort((a, b) => a.name.localeCompare(b.name))
  29. return projects
  30. }
  31. return []
  32. }
  33. async getProjectDetails(projectId: string): Promise<Project> {
  34. let response = await Api.get(`${configLoader.config.servicesUrls.keystone}/projects/${projectId}`)
  35. return response.data.project
  36. }
  37. async getRoleAssignments(skipLog?: boolean): Promise<RoleAssignment[]> {
  38. let response = await Api.send({
  39. url: `${configLoader.config.servicesUrls.keystone}/role_assignments?include_names`,
  40. skipLog,
  41. })
  42. let assignments: RoleAssignment[] = response.data.role_assignments
  43. assignments.sort((a1, a2) => a1.role.name.localeCompare(a2.role.name))
  44. return assignments
  45. }
  46. async getUsers(projectId: string): Promise<User[]> {
  47. let assignments = await this.getRoleAssignments()
  48. const userIds: string[] = assignments
  49. .filter(a => a.scope.project && a.scope.project.id === projectId)
  50. .filter((a, i, arr) => arr.findIndex(e => a.user.id === e.user.id) === i)
  51. .map(a => a.user.id)
  52. let users: User[] = await Promise.all(userIds.map(async id => {
  53. let user: User = await UserSource.getUserInfo(id)
  54. return user
  55. }))
  56. users.sort((a, b) => a.name.localeCompare(b.name))
  57. return users
  58. }
  59. async removeUser(projectId: string, userId: string, roleIds: string[]): Promise<void> {
  60. await Promise.all(roleIds.map(async id => {
  61. await Api.send({
  62. url: `${configLoader.config.servicesUrls.keystone}/projects/${projectId}/users/${userId}/roles/${id}`,
  63. method: 'DELETE',
  64. })
  65. }))
  66. }
  67. async assignUser(projectId: string, userId: string, roleId: string): Promise<void> {
  68. await Api.send({
  69. url: `${configLoader.config.servicesUrls.keystone}/projects/${projectId}/users/${userId}/roles/${roleId}`,
  70. method: 'PUT',
  71. })
  72. }
  73. async getRoles(): Promise<Role[]> {
  74. let roles: Role[] = await UserSource.getRoles()
  75. return roles
  76. }
  77. async update(projectId: string, project: Project): Promise<Project> {
  78. let data = { project: {} }
  79. if (project.name != null) {
  80. data.project.name = project.name
  81. }
  82. if (project.description != null) {
  83. data.project.description = project.description
  84. }
  85. if (project.description != null) {
  86. data.project.enabled = project.enabled
  87. }
  88. let response = await Api.send({
  89. url: `${configLoader.config.servicesUrls.keystone}/projects/${projectId}`,
  90. method: 'PATCH',
  91. data,
  92. })
  93. return response.data.project
  94. }
  95. async delete(projectId: string): Promise<void> {
  96. await Api.send({
  97. url: `${configLoader.config.servicesUrls.keystone}/projects/${projectId}`,
  98. method: 'DELETE',
  99. })
  100. }
  101. async add(project: Project, userId: string): Promise<Project> {
  102. let data = { project: {} }
  103. data.project.name = project.name
  104. if (project.enabled != null) {
  105. data.project.enabled = project.enabled
  106. }
  107. if (project.description != null) {
  108. data.project.description = project.description
  109. }
  110. let response = await Api.send({
  111. url: `${configLoader.config.servicesUrls.keystone}/projects/`,
  112. method: 'POST',
  113. data,
  114. })
  115. let addedProject: Project = response.data.project
  116. let adminRoleId: string = await UserSource.getAdminRoleId()
  117. await UserSource.assignUserToProjectWithRole(userId, addedProject.id, adminRoleId)
  118. return addedProject
  119. }
  120. }
  121. export default new ProjectsSource()