aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/api/users.ts
blob: 02c631b1a722659199c2b23e3174e2596324edfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import axios from 'axios';
import { throwGlobalError } from '~sonar-aligned/helpers/error';
import { getJSON } from '~sonar-aligned/helpers/request';
import { HttpStatus, axiosToCatch, parseJSON, post } from '../helpers/request';
import { IdentityProvider, Paging } from '../types/types';
import {
  ChangePasswordResults,
  CurrentUser,
  HomePage,
  NoticeType,
  RestUserBase,
  RestUserDetailed,
} from '../types/users';

const USERS_ENDPOINT = '/api/v2/users-management/users';

export function getCurrentUser(): Promise<CurrentUser> {
  return getJSON('/api/users/current', undefined, { bypassRedirect: true });
}

export function dismissNotice(notice: NoticeType) {
  return post('/api/users/dismiss_notice', { notice }).catch(throwGlobalError);
}

export function changePassword(data: {
  login: string;
  password: string;
  previousPassword?: string;
}) {
  return post('/api/users/change_password', data).catch(async (response) => {
    if (response.status === HttpStatus.BadRequest) {
      const { result } = await parseJSON(response);
      return Promise.reject<ChangePasswordResults>(result);
    }

    return throwGlobalError(response);
  });
}

export function getIdentityProviders(): Promise<{ identityProviders: IdentityProvider[] }> {
  return getJSON('/api/users/identity_providers').catch(throwGlobalError);
}

export function getUsers<T extends RestUserBase>(data: {
  active?: boolean;
  groupId?: string;
  'groupId!'?: string;
  managed?: boolean;
  pageIndex?: number;
  pageSize?: number;
  q: string;
  sonarLintLastConnectionDateFrom?: string;
  sonarLintLastConnectionDateTo?: string;
  sonarQubeLastConnectionDateFrom?: string;
  sonarQubeLastConnectionDateTo?: string;
}) {
  return axios.get<{ page: Paging; users: T[] }>(USERS_ENDPOINT, {
    params: data,
  });
}

export function postUser(data: {
  email?: string;
  login: string;
  name: string;
  password?: string;
  scmAccounts: string[];
}) {
  return axiosToCatch.post<RestUserDetailed>(USERS_ENDPOINT, data);
}

export function updateUser(
  id: string,
  data: Partial<Pick<RestUserDetailed, 'email' | 'name' | 'scmAccounts'>>,
) {
  return axiosToCatch.patch<RestUserDetailed>(`${USERS_ENDPOINT}/${id}`, data);
}

export function deleteUser({ id, anonymize }: { anonymize?: boolean; id: string }) {
  return axios.delete(`${USERS_ENDPOINT}/${id}`, { params: { anonymize } });
}

export function setHomePage(homepage: HomePage): Promise<void | Response> {
  return post('/api/users/set_homepage', homepage).catch(throwGlobalError);
}