3 * Copyright (C) 2009-2020 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.tester;
22 import com.google.common.collect.HashMultimap;
23 import com.google.common.collect.ImmutableSet;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.HashSet;
28 import java.util.Optional;
30 import org.sonar.api.web.UserRole;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.permission.GlobalPermission;
33 import org.sonar.db.project.ProjectDto;
34 import org.sonar.server.user.AbstractUserSession;
36 import static com.google.common.base.Preconditions.checkArgument;
38 public abstract class AbstractMockUserSession<T extends AbstractMockUserSession> extends AbstractUserSession {
39 private static final Set<String> PUBLIC_PERMISSIONS = ImmutableSet.of(UserRole.USER, UserRole.CODEVIEWER); // FIXME to check with Simon
41 private final Class<T> clazz;
42 private HashMultimap<String, String> projectUuidByPermission = HashMultimap.create();
43 private final Set<GlobalPermission> permissions = new HashSet<>();
44 private Map<String, String> projectUuidByComponentUuid = new HashMap<>();
45 private Set<String> projectPermissions = new HashSet<>();
46 private boolean systemAdministrator = false;
48 protected AbstractMockUserSession(Class<T> clazz) {
52 public T addPermission(GlobalPermission permission) {
53 permissions.add(permission);
54 return clazz.cast(this);
58 protected boolean hasPermissionImpl(GlobalPermission permission) {
59 return permissions.contains(permission);
63 * Use this method to register public root component and non root components the UserSession must be aware of.
64 * (ie. this method can be used to emulate the content of the DB)
66 public T registerComponents(ComponentDto... components) {
67 Arrays.stream(components)
68 .forEach(component -> {
69 if (component.projectUuid().equals(component.uuid()) && !component.isPrivate()) {
70 this.projectUuidByPermission.put(UserRole.USER, component.uuid());
71 this.projectUuidByPermission.put(UserRole.CODEVIEWER, component.uuid());
72 this.projectPermissions.add(UserRole.USER);
73 this.projectPermissions.add(UserRole.CODEVIEWER);
75 this.projectUuidByComponentUuid.put(component.uuid(), component.projectUuid());
77 return clazz.cast(this);
80 public T registerProjects(ProjectDto... projects) {
81 Arrays.stream(projects)
83 if (!project.isPrivate()) {
84 this.projectUuidByPermission.put(UserRole.USER, project.getUuid());
85 this.projectUuidByPermission.put(UserRole.CODEVIEWER, project.getUuid());
86 this.projectPermissions.add(UserRole.USER);
87 this.projectPermissions.add(UserRole.CODEVIEWER);
89 this.projectUuidByComponentUuid.put(project.getUuid(), project.getUuid());
91 return clazz.cast(this);
94 public T addProjectPermission(String permission, ComponentDto... components) {
95 Arrays.stream(components).forEach(component -> {
97 component.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission),
98 "public component %s can't be granted public permission %s", component.uuid(), permission);
100 registerComponents(components);
101 this.projectPermissions.add(permission);
102 Arrays.stream(components)
103 .forEach(component -> this.projectUuidByPermission.put(permission, component.projectUuid()));
104 return clazz.cast(this);
107 public T addProjectPermission(String permission, ProjectDto... projects) {
108 Arrays.stream(projects).forEach(component -> {
110 component.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission),
111 "public component %s can't be granted public permission %s", component.getUuid(), permission);
113 registerProjects(projects);
114 this.projectPermissions.add(permission);
115 Arrays.stream(projects)
116 .forEach(component -> this.projectUuidByPermission.put(permission, component.getUuid()));
117 return clazz.cast(this);
121 protected Optional<String> componentUuidToProjectUuid(String componentUuid) {
122 return Optional.ofNullable(projectUuidByComponentUuid.get(componentUuid));
126 protected boolean hasProjectUuidPermission(String permission, String projectUuid) {
127 return projectPermissions.contains(permission) && projectUuidByPermission.get(permission).contains(projectUuid);
130 public T setSystemAdministrator(boolean b) {
131 this.systemAdministrator = b;
132 return clazz.cast(this);
136 public boolean isSystemAdministrator() {
137 return isRoot() || systemAdministrator;