You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

users.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { throwGlobalError } from '../helpers/error';
  21. import {
  22. deleteJSON,
  23. getJSON,
  24. HttpStatus,
  25. parseJSON,
  26. post,
  27. postJSON,
  28. postJSONBody,
  29. } from '../helpers/request';
  30. import { IdentityProvider, Paging } from '../types/types';
  31. import {
  32. ChangePasswordResults,
  33. CurrentUser,
  34. HomePage,
  35. NoticeType,
  36. RestUserBase,
  37. User,
  38. } from '../types/users';
  39. export function getCurrentUser(): Promise<CurrentUser> {
  40. return getJSON('/api/users/current', undefined, true);
  41. }
  42. export function dismissNotice(notice: NoticeType) {
  43. return post('/api/users/dismiss_notice', { notice }).catch(throwGlobalError);
  44. }
  45. export function changePassword(data: {
  46. login: string;
  47. password: string;
  48. previousPassword?: string;
  49. }) {
  50. return post('/api/users/change_password', data).catch(async (response) => {
  51. if (response.status === HttpStatus.BadRequest) {
  52. const { result } = await parseJSON(response);
  53. return Promise.reject<ChangePasswordResults>(result);
  54. }
  55. return throwGlobalError(response);
  56. });
  57. }
  58. export interface UserGroup {
  59. default: boolean;
  60. description: string;
  61. id: number;
  62. name: string;
  63. selected: boolean;
  64. }
  65. export function getUserGroups(data: {
  66. login: string;
  67. p?: number;
  68. ps?: number;
  69. q?: string;
  70. selected?: string;
  71. }): Promise<{ paging: Paging; groups: UserGroup[] }> {
  72. return getJSON('/api/users/groups', data);
  73. }
  74. export function getIdentityProviders(): Promise<{ identityProviders: IdentityProvider[] }> {
  75. return getJSON('/api/users/identity_providers').catch(throwGlobalError);
  76. }
  77. export function getUsers<T extends RestUserBase>(data: {
  78. q: string;
  79. active?: boolean;
  80. managed?: boolean;
  81. sonarQubeLastConnectionDateFrom?: string;
  82. sonarQubeLastConnectionDateTo?: string;
  83. sonarLintLastConnectionDateFrom?: string;
  84. sonarLintLastConnectionDateTo?: string;
  85. pageSize?: number;
  86. pageIndex?: number;
  87. }): Promise<{ page: Paging; users: T[] }> {
  88. return getJSON('/api/v2/users', data).catch(throwGlobalError);
  89. }
  90. export function postUser(data: {
  91. email?: string;
  92. login: string;
  93. name: string;
  94. password?: string;
  95. scmAccounts: string[];
  96. }): Promise<void | Response> {
  97. return postJSONBody('/api/v2/users', data);
  98. }
  99. type UpdateUserArg =
  100. | {
  101. email?: string;
  102. login: string;
  103. name?: string;
  104. scmAccount: string[];
  105. }
  106. | { login: string; scmAccount: string[] };
  107. export function updateUser(data: UpdateUserArg): Promise<{ user: User }> {
  108. return postJSON('/api/users/update', {
  109. ...data,
  110. scmAccount: data.scmAccount.length > 0 ? data.scmAccount : '',
  111. });
  112. }
  113. export function deleteUser({
  114. login,
  115. anonymize,
  116. }: {
  117. login: string;
  118. anonymize?: boolean;
  119. }): Promise<void | Response> {
  120. return deleteJSON(`/api/v2/users/${login}`, { anonymize }).catch(throwGlobalError);
  121. }
  122. export function setHomePage(homepage: HomePage): Promise<void | Response> {
  123. return post('/api/users/set_homepage', homepage).catch(throwGlobalError);
  124. }