-
Notifications
You must be signed in to change notification settings - Fork 133
/
GroupRepository.java
244 lines (195 loc) · 9.51 KB
/
GroupRepository.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*-
* ---license-start
* keycloak-config-cli
* ---
* Copyright (C) 2017 - 2021 adorsys GmbH & Co. KG @ https://adorsys.com
* ---
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---license-end
*/
package de.adorsys.keycloak.config.repository;
import de.adorsys.keycloak.config.exception.ImportProcessingException;
import org.keycloak.admin.client.CreatedResponseUtil;
import org.keycloak.admin.client.resource.GroupResource;
import org.keycloak.admin.client.resource.GroupsResource;
import org.keycloak.admin.client.resource.RoleMappingResource;
import org.keycloak.admin.client.resource.RoleScopeResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.ManagementPermissionRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import jakarta.ws.rs.core.Response;
@Service
public class GroupRepository {
private final RealmRepository realmRepository;
private final RoleRepository roleRepository;
private final ClientRepository clientRepository;
private final UserRepository userRepository;
@Autowired
public GroupRepository(
RealmRepository realmRepository,
RoleRepository roleRepository,
ClientRepository clientRepository,
UserRepository userRepository) {
this.realmRepository = realmRepository;
this.roleRepository = roleRepository;
this.clientRepository = clientRepository;
this.userRepository = userRepository;
}
public List<GroupRepresentation> getAll(String realmName) {
GroupsResource groupsResource = realmRepository.getResource(realmName)
.groups();
return groupsResource.groups();
}
public List<GroupRepresentation> findGroupsByGroupPath(String realmName, List<String> groupPaths) {
List<GroupRepresentation> groups = new ArrayList<>();
for (String groupPath : groupPaths) {
try {
GroupRepresentation group = getGroupByPath(realmName, groupPath);
groups.add(group);
} catch (Exception e) {
throw new ImportProcessingException(
String.format("Could not find group '%s' in realm '%s'!", groupPath, realmName)
);
}
}
return groups;
}
public Optional<GroupRepresentation> searchByName(String realmName, String groupName) {
GroupsResource groupsResource = realmRepository.getResource(realmName)
.groups();
return groupsResource.groups()
.stream()
.filter(g -> Objects.equals(g.getName(), groupName))
.findFirst();
}
public void createGroup(String realmName, GroupRepresentation group) {
GroupsResource groupsResource = realmRepository.getResource(realmName).groups();
try (Response response = groupsResource.add(group)) {
CreatedResponseUtil.getCreatedId(response);
}
}
public void addSubGroup(String realmName, String parentGroupId, GroupRepresentation subGroup) {
GroupResource groupResource = getResourceById(realmName, parentGroupId);
try (Response response = groupResource.subGroup(subGroup)) {
CreatedResponseUtil.getCreatedId(response);
}
}
public GroupRepresentation getSubGroupByName(String realmName, String parentGroupId, String name) {
return getSubGroups(realmName, parentGroupId)
.stream()
.filter(subgroup -> Objects.equals(subgroup.getName(), name))
.findFirst()
.orElse(null);
}
public List<GroupRepresentation> getSubGroups(String realmName, String parentGroupId) {
var groupResource = getResourceById(realmName, parentGroupId);
return groupResource.getSubGroups(null, null, false);
}
public void addRealmRoles(String realmName, String groupId, List<String> roleNames) {
GroupResource groupResource = getResourceById(realmName, groupId);
RoleMappingResource groupRoles = groupResource.roles();
RoleScopeResource groupRealmRoles = groupRoles.realmLevel();
List<RoleRepresentation> existingRealmRoles = roleNames.stream()
.map(realmRole -> roleRepository.getRealmRole(realmName, realmRole))
.toList();
groupRealmRoles.add(existingRealmRoles);
}
public void removeRealmRoles(String realmName, String groupId, List<String> roleNames) {
GroupResource groupResource = getResourceById(realmName, groupId);
RoleMappingResource groupRoles = groupResource.roles();
RoleScopeResource groupRealmRoles = groupRoles.realmLevel();
List<RoleRepresentation> existingRealmRoles = roleNames.stream()
.map(realmRole -> roleRepository.getRealmRole(realmName, realmRole))
.toList();
groupRealmRoles.remove(existingRealmRoles);
}
public void deleteGroup(String realmName, String id) {
GroupResource groupResource = getResourceById(realmName, id);
groupResource.remove();
}
public void addGroupsToUser(String realmName, String username, List<GroupRepresentation> groups) {
UserResource userResource = userRepository.getResource(realmName, username);
for (GroupRepresentation group : groups) {
userResource.joinGroup(group.getId());
}
}
public void removeGroupsFromUser(String realmName, String username, List<GroupRepresentation> groups) {
UserResource userResource = userRepository.getResource(realmName, username);
for (GroupRepresentation group : groups) {
userResource.leaveGroup(group.getId());
}
}
public void addClientRoles(String realmName, String groupId, String clientId, List<String> roleNames) {
GroupResource groupResource = getResourceById(realmName, groupId);
RoleMappingResource rolesResource = groupResource.roles();
ClientRepresentation client = clientRepository.getByClientId(realmName, clientId);
RoleScopeResource groupClientRolesResource = rolesResource.clientLevel(client.getId());
List<RoleRepresentation> clientRoles = roleRepository.getClientRolesByName(realmName, clientId, roleNames);
groupClientRolesResource.add(clientRoles);
}
public void removeClientRoles(String realmName, String groupId, String clientId, List<String> roleNames) {
GroupResource groupResource = getResourceById(realmName, groupId);
RoleMappingResource rolesResource = groupResource.roles();
ClientRepresentation client = clientRepository.getByClientId(realmName, clientId);
RoleScopeResource groupClientRolesResource = rolesResource.clientLevel(client.getId());
List<RoleRepresentation> clientRoles = roleRepository.getClientRolesByName(realmName, clientId, roleNames);
groupClientRolesResource.remove(clientRoles);
}
public void update(String realmName, GroupRepresentation group) {
GroupResource groupResource = getResourceById(realmName, group.getId());
groupResource.update(group);
}
public GroupRepresentation getGroupByName(String realmName, String groupName) {
GroupResource groupResource = getResourceByName(realmName, groupName);
if (groupResource == null) {
return null;
}
return groupResource.toRepresentation();
}
public GroupRepresentation getGroupById(String realmName, String groupId) {
GroupResource groupResource = getResourceById(realmName, groupId);
return groupResource.toRepresentation();
}
public GroupRepresentation getGroupByPath(String realmName, String groupPath) {
return realmRepository.getResource(realmName).getGroupByPath(groupPath);
}
public void enablePermission(String realmName, String id) {
GroupResource groupResource = getResourceById(realmName, id);
groupResource.setPermissions(new ManagementPermissionRepresentation(true));
}
public boolean isPermissionEnabled(String realmName, String id) {
GroupResource groupResource = getResourceById(realmName, id);
return groupResource.getPermissions().isEnabled();
}
private GroupResource getResourceByName(String realmName, String groupName) {
Optional<GroupRepresentation> maybeGroup = searchByName(realmName, groupName);
GroupRepresentation existingGroup = maybeGroup.orElse(null);
if (existingGroup == null) {
return null;
}
return getResourceById(realmName, existingGroup.getId());
}
private GroupResource getResourceById(String realmName, String groupId) {
return realmRepository.getResource(realmName)
.groups()
.group(groupId);
}
}