/
PermissionDependenciesFinder.java
101 lines (84 loc) · 3.44 KB
/
PermissionDependenciesFinder.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
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.server.permission.ws;
import com.google.common.base.Optional;
import javax.annotation.CheckForNull;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.permission.PermissionTemplateDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.ComponentFinder;
import static java.lang.String.format;
import static org.sonar.api.security.DefaultGroups.ANYONE;
import static org.sonar.api.security.DefaultGroups.isAnyone;
import static org.sonar.server.ws.WsUtils.checkFound;
public class PermissionDependenciesFinder {
private final DbClient dbClient;
private final ComponentFinder componentFinder;
public PermissionDependenciesFinder(DbClient dbClient, ComponentFinder componentFinder) {
this.dbClient = dbClient;
this.componentFinder = componentFinder;
}
/**
* @throws org.sonar.server.exceptions.NotFoundException if a project identifier is provided but it's not found
*/
Optional<ComponentDto> searchProject(DbSession dbSession, PermissionRequest request) {
if (!request.hasProject()) {
return Optional.absent();
}
return Optional.of(componentFinder.getProjectByUuidOrKey(dbSession, request.projectUuid(), request.projectKey()));
}
String getGroupName(DbSession dbSession, PermissionRequest request) {
GroupDto group = getGroup(dbSession, request.group());
return group == null ? ANYONE : group.getName();
}
/**
*
* @return null if it's the anyone group
*/
@CheckForNull
GroupDto getGroup(DbSession dbSession, WsGroup group) {
Long groupId = group.id();
String groupName = group.name();
if (isAnyone(groupName)) {
return null;
}
GroupDto groupDto = null;
if (groupId != null) {
groupDto = checkFound(dbClient.groupDao().selectById(dbSession, groupId),
format("Group with id '%d' is not found", groupId));
}
if (groupName != null) {
groupDto = checkFound(dbClient.groupDao().selectByName(dbSession, groupName),
format("Group with name '%s' is not found", groupName));
}
return groupDto;
}
UserDto getUser(DbSession dbSession, String userLogin) {
return checkFound(dbClient.userDao().selectActiveUserByLogin(dbSession, userLogin),
format("User with login '%s' is not found'", userLogin));
}
PermissionTemplateDto getTemplate(String templateKey) {
return checkFound(dbClient.permissionTemplateDao().selectTemplateByKey(templateKey),
format("Permission template with key '%s' is not found", templateKey));
}
}