aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/api/permissions.js
blob: 80a77e565392f73f8d79f744bc97974c95bcc744 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact 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 $ from 'jquery';
import _ from 'underscore';
import { getJSON, post } from '../helpers/request';

const PAGE_SIZE = 100;

function request (options) {
  return $.ajax(options);
}

export function getPermissionUsers (data) {
  const url = '/api/permissions/users';
  return getJSON(url, data);
}

export function grantPermissionToUser (projectKey, login, permission) {
  const url = '/api/permissions/add_user';
  const data = { login, permission };
  if (projectKey) {
    data.projectKey = projectKey;
  }
  return post(url, data);
}

export function revokePermissionFromUser (projectKey, login, permission) {
  const url = '/api/permissions/remove_user';
  const data = { login, permission };
  if (projectKey) {
    data.projectKey = projectKey;
  }
  return post(url, data);
}

export function getPermissionGroups (data) {
  const url = '/api/permissions/groups';
  return getJSON(url, data);
}

export function grantPermissionToGroup (projectKey, groupName, permission) {
  const url = '/api/permissions/add_group';
  const data = { groupName, permission };
  if (projectKey) {
    data.projectKey = projectKey;
  }
  return post(url, data);
}

export function revokePermissionFromGroup (projectKey, groupName, permission) {
  const url = '/api/permissions/remove_group';
  const data = { groupName, permission };
  if (projectKey) {
    data.projectKey = projectKey;
  }
  return post(url, data);
}

/**
 * Get list of permission templates
 * @returns {Promise}
 */
export function getPermissionTemplates () {
  const url = '/api/permissions/search_templates';
  return getJSON(url);
}

export function createPermissionTemplate (options) {
  const url = window.baseUrl + '/api/permissions/create_template';
  return request(_.extend({ type: 'POST', url }, options));
}

export function updatePermissionTemplate (options) {
  const url = window.baseUrl + '/api/permissions/update_template';
  return request(_.extend({ type: 'POST', url }, options));
}

export function deletePermissionTemplate (options) {
  const url = window.baseUrl + '/api/permissions/delete_template';
  return request(_.extend({ type: 'POST', url }, options));
}

/**
 * Set default permission template for a given qualifier
 * @param {string} templateName
 * @param {string} qualifier
 * @returns {Promise}
 */
export function setDefaultPermissionTemplate (templateName, qualifier) {
  const url = '/api/permissions/set_default_template';
  const data = { templateName, qualifier };
  return post(url, data);
}

export function applyTemplateToProject (data) {
  const url = '/api/permissions/apply_template';
  return post(url, data);
}

export function bulkApplyTemplateToProject (options) {
  const url = window.baseUrl + '/api/permissions/bulk_apply_template';
  return request(_.extend({ type: 'POST', url }, options));
}

export function addProjectCreatorToTemplate (templateName, permission) {
  const url = '/api/permissions/add_project_creator_to_template';
  const data = { templateName, permission };
  return post(url, data);
}

export function removeProjectCreatorFromTemplate (templateName, permission) {
  const url = '/api/permissions/remove_project_creator_from_template';
  const data = { templateName, permission };
  return post(url, data);
}

export function getPermissionsUsersForComponent (projectKey, query = '', permission = null) {
  const url = '/api/permissions/users';
  const data = { projectKey, ps: PAGE_SIZE };
  if (query) {
    data.q = query;
  }
  if (permission) {
    data.permission = permission;
  }
  return getJSON(url, data).then(r => r.users);
}

export function getPermissionsGroupsForComponent (projectKey, query = '', permission = null) {
  const url = '/api/permissions/groups';
  const data = { projectKey, ps: PAGE_SIZE };
  if (query) {
    data.q = query;
  }
  if (permission) {
    data.permission = permission;
  }
  return getJSON(url, data).then(r => r.groups);
}

export function getGlobalPermissionsUsers (query = '', permission = null) {
  const url = '/api/permissions/users';
  const data = { ps: PAGE_SIZE };
  if (query) {
    data.q = query;
  }
  if (permission) {
    data.permission = permission;
  }
  return getJSON(url, data).then(r => r.users);
}

export function getGlobalPermissionsGroups (query = '', permission = null) {
  const url = '/api/permissions/groups';
  const data = { ps: PAGE_SIZE };
  if (query) {
    data.q = query;
  }
  if (permission) {
    data.permission = permission;
  }
  return getJSON(url, data).then(r => r.groups);
}