-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
UserPermissionDao.java
156 lines (136 loc) · 6.37 KB
/
UserPermissionDao.java
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
/*
* SonarQube
* Copyright (C) 2009-2018 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.
*/
package org.sonar.db.permission;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.Dao;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentMapper;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Collections.emptyList;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;
public class UserPermissionDao implements Dao {
/**
* List of user permissions ordered by alphabetical order of user names.
* Pagination is NOT applied.
* No sort is done.
*
* @param query non-null query including optional filters.
* @param userIds Filter on user ids, including disabled users. Must not be empty and maximum size is {@link DatabaseUtils#PARTITION_SIZE_FOR_ORACLE}.
*/
public List<UserPermissionDto> selectUserPermissionsByQuery(DbSession dbSession, PermissionQuery query, Collection<Integer> userIds) {
if (userIds.isEmpty()) {
return emptyList();
}
checkArgument(userIds.size() <= DatabaseUtils.PARTITION_SIZE_FOR_ORACLE, "Maximum 1'000 users are accepted");
return mapper(dbSession).selectUserPermissionsByQueryAndUserIds(query, userIds);
}
public List<Integer> selectUserIdsByQuery(DbSession dbSession, PermissionQuery query) {
return mapper(dbSession).selectUserIdsByQuery(query)
.stream()
// Pagination is done in Java because it's too complex to use SQL pagination in Oracle and MsSQL with the distinct
.skip(query.getPageOffset())
.limit(query.getPageSize())
.collect(MoreCollectors.toArrayList());
}
public int countUsersByQuery(DbSession dbSession, PermissionQuery query) {
return mapper(dbSession).countUsersByQuery(query);
}
/**
* Count the number of users per permission for a given list of projects
*
* @param projectIds a non-null list of project ids to filter on. If empty then an empty list is returned.
*/
public List<CountPerProjectPermission> countUsersByProjectPermission(DbSession dbSession, Collection<Long> projectIds) {
return executeLargeInputs(projectIds, mapper(dbSession)::countUsersByProjectPermission);
}
/**
* Gets all the global permissions granted to user for the specified organization.
*
* @return the global permissions. An empty list is returned if user or organization do not exist.
*/
public List<String> selectGlobalPermissionsOfUser(DbSession dbSession, int userId, String organizationUuid) {
return mapper(dbSession).selectGlobalPermissionsOfUser(userId, organizationUuid);
}
/**
* Gets all the project permissions granted to user for the specified project.
*
* @return the project permissions. An empty list is returned if project or user do not exist.
*/
public List<String> selectProjectPermissionsOfUser(DbSession dbSession, int userId, long projectId) {
return mapper(dbSession).selectProjectPermissionsOfUser(userId, projectId);
}
public Set<Integer> selectUserIdsWithPermissionOnProjectBut(DbSession session, long projectId, String permission) {
return mapper(session).selectUserIdsWithPermissionOnProjectBut(projectId, permission);
}
public void insert(DbSession dbSession, UserPermissionDto dto) {
ensureComponentPermissionConsistency(dbSession, dto);
mapper(dbSession).insert(dto);
}
private static void ensureComponentPermissionConsistency(DbSession dbSession, UserPermissionDto dto) {
if (dto.getComponentId() == null) {
return;
}
ComponentMapper componentMapper = dbSession.getMapper(ComponentMapper.class);
checkArgument(
componentMapper.countComponentByOrganizationAndId(dto.getOrganizationUuid(), dto.getComponentId()) == 1,
"Can't insert permission '%s' for component with id '%s' in organization with uuid '%s' because this component does not belong to organization with uuid '%s'",
dto.getPermission(), dto.getComponentId(), dto.getOrganizationUuid(), dto.getOrganizationUuid());
}
/**
* Removes a single global permission from user
*/
public void deleteGlobalPermission(DbSession dbSession, int userId, String permission, String organizationUuid) {
mapper(dbSession).deleteGlobalPermission(userId, permission, organizationUuid);
}
/**
* Removes a single project permission from user
*/
public void deleteProjectPermission(DbSession dbSession, int userId, String permission, long projectId) {
mapper(dbSession).deleteProjectPermission(userId, permission, projectId);
}
/**
* Deletes all the permissions defined on a project
*/
public void deleteProjectPermissions(DbSession dbSession, long projectId) {
mapper(dbSession).deleteProjectPermissions(projectId);
}
/**
* Deletes the specified permission on the specified project for any user.
*/
public int deleteProjectPermissionOfAnyUser(DbSession dbSession, long projectId, String permission) {
return mapper(dbSession).deleteProjectPermissionOfAnyUser(projectId, permission);
}
public void deleteByOrganization(DbSession dbSession, String organizationUuid) {
mapper(dbSession).deleteByOrganization(organizationUuid);
}
public void deleteOrganizationMemberPermissions(DbSession dbSession, String organizationUuid, int userId) {
mapper(dbSession).deleteOrganizationMemberPermissions(organizationUuid, userId);
}
public void deleteByUserId(DbSession dbSession, int userId) {
mapper(dbSession).deleteByUserId(userId);
}
private static UserPermissionMapper mapper(DbSession dbSession) {
return dbSession.getMapper(UserPermissionMapper.class);
}
}