3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.usergroups.ws;
22 import java.util.Optional;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.user.ExternalGroupDto;
28 import org.sonar.db.user.GroupDto;
30 public class ExternalGroupService {
32 private static final Logger LOG = LoggerFactory.getLogger(ExternalGroupService.class);
34 private final DbClient dbClient;
35 private final GroupService groupService;
37 public ExternalGroupService(DbClient dbClient, GroupService groupService) {
38 this.dbClient = dbClient;
39 this.groupService = groupService;
42 public void createOrUpdateExternalGroup(DbSession dbSession, GroupRegistration groupRegistration) {
43 Optional<GroupDto> groupDto = retrieveGroupByExternalInformation(dbSession, groupRegistration);
44 if (groupDto.isPresent()) {
45 updateExternalGroup(dbSession, groupDto.get(), groupRegistration.name());
47 createOrMatchExistingLocalGroup(dbSession, groupRegistration);
51 private Optional<GroupDto> retrieveGroupByExternalInformation(DbSession dbSession, GroupRegistration groupRegistration) {
52 Optional<ExternalGroupDto> externalGroupDto = dbClient.externalGroupDao().selectByExternalIdAndIdentityProvider(dbSession, groupRegistration.externalId(),
53 groupRegistration.externalIdentityProvider());
54 return externalGroupDto.flatMap(existingExternalGroupDto -> Optional.ofNullable(dbClient.groupDao().selectByUuid(dbSession, existingExternalGroupDto.groupUuid())));
57 private void updateExternalGroup(DbSession dbSession, GroupDto groupDto, String newName) {
58 LOG.debug("Updating external group: {} with new name {}", groupDto.getName(), newName);
59 groupService.updateGroup(dbSession, groupDto, newName);
62 private void createOrMatchExistingLocalGroup(DbSession dbSession, GroupRegistration groupRegistration) {
63 GroupDto groupDto = findOrCreateLocalGroup(dbSession, groupRegistration);
64 createExternalGroup(dbSession, groupDto.getUuid(), groupRegistration);
67 private GroupDto findOrCreateLocalGroup(DbSession dbSession, GroupRegistration groupRegistration) {
68 Optional<GroupDto> groupDto = groupService.findGroup(dbSession, groupRegistration.name());
69 if (groupDto.isPresent()) {
70 LOG.debug("Marking existing local group {} as managed group.", groupDto.get().getName());
71 return groupDto.get();
73 LOG.debug("Creating new group {}", groupRegistration.name());
74 return groupService.createGroup(dbSession, groupRegistration.name(), null);
78 private void createExternalGroup(DbSession dbSession, String groupUuid, GroupRegistration groupRegistration) {
79 dbClient.externalGroupDao().insert(dbSession, new ExternalGroupDto(groupUuid, groupRegistration.externalId(), groupRegistration.externalIdentityProvider()));