use of jetbrains.buildServer.serverSide.auth.Permissions in project teamcity-rest by JetBrains.
the class PermissionAssignmentFinder method getPermissions.
@NotNull
private FinderDataBinding.ItemHolder<PermissionAssignmentData> getPermissions(@NotNull final TypedFinderBuilder.DimensionObjects dimensions, @NotNull final AuthorityHolder authorityHolder, @NotNull final ServiceLocator serviceLocator) {
/* The rest of the code in this method is mostly performance optimization producing the same results (with possibly changed sorting).
if (true) {
List<Permission> globalPermissions = authorityHolder.getGlobalPermissions().toList();
Set<Permission> globalPermissionsSet = new HashSet<>(globalPermissions); //TeamCity API issue: this set is used to exclude global permissions from project-level ones
return FinderDataBinding.getItemHolder(Stream.concat(
globalPermissions.stream().map(p -> new PermissionAssignmentData(p)),
authorityHolder.getProjectsPermissions().entrySet().stream().flatMap(
entry -> entry.getValue().toList().stream().filter(p -> !globalPermissionsSet.contains(p)).map(p -> new PermissionAssignmentData(p, entry.getKey())))));
}
*/
List<Permission> permissions_raw = dimensions.get(PERMISSION);
List<List<SProject>> projects_raw = dimensions.get(PROJECT);
if (projects_raw != null && !projects_raw.isEmpty() && projects_raw.size() > 1) {
throw new BadRequestException("Multiple projects dimensions are not supported");
}
@Nullable List<SProject> projects = projects_raw == null || projects_raw.isEmpty() ? null : projects_raw.get(0);
if (permissions_raw != null && !permissions_raw.isEmpty() && permissions_raw.size() > 1) {
throw new BadRequestException("Multiple permissions dimensions are not supported");
}
// permissions_raw is ANDed, permissions is ORed, but so far it is not supported: todo implement
List<Permission> permissions = permissions_raw;
Stream<PermissionAssignmentData> result = Stream.empty();
List<Boolean> global_raw = dimensions.get(GLOBAL);
if (global_raw != null && !global_raw.isEmpty() && global_raw.size() > 1) {
throw new BadRequestException("Multiple global dimensions are not supported");
}
Boolean global = global_raw == null ? null : global_raw.get(0);
if ((permissions == null || permissions.isEmpty())) {
if (projects == null) {
if (global == null || global) {
result = Stream.concat(result, authorityHolder.getGlobalPermissions().toList().stream().map(p -> new PermissionAssignmentData(p)));
}
if (global == null || !global) {
result = Stream.concat(result, authorityHolder.getProjectsPermissions().entrySet().stream().flatMap(entry -> entry.getValue().toList().stream().filter(p -> p.isProjectAssociationSupported()).map(p -> new PermissionAssignmentData(p, entry.getKey()))));
}
return FinderDataBinding.getItemHolder(result);
}
if (global == null || global) {
result = Stream.concat(result, authorityHolder.getGlobalPermissions().toList().stream().filter(p -> p.isProjectAssociationSupported()).map(p -> new PermissionAssignmentData(p)));
}
if (global == null || !global) {
result = Stream.concat(result, projects.stream().flatMap(project -> {
Permissions projectPermissions = authorityHolder.getProjectsPermissions().get(project.getProjectId());
return projectPermissions == null ? Stream.empty() : projectPermissions.toList().stream().filter(p -> p.isProjectAssociationSupported()).map(p -> new PermissionAssignmentData(p, project.getProjectId()));
}));
}
return FinderDataBinding.getItemHolder(result);
}
if (projects == null) {
if (global == null || global) {
result = Stream.concat(result, permissions.stream().filter(p -> authorityHolder.isPermissionGrantedGlobally(p)).map(p -> new PermissionAssignmentData(p)));
}
if (global == null || !global) {
List<SProject> allProjects = serviceLocator.getSingletonService(ProjectManager.class).getProjects();
result = Stream.concat(result, permissions.stream().filter(p -> p.isProjectAssociationSupported()).flatMap(p -> allProjects.stream().filter(project -> {
Permissions projectPermissions = authorityHolder.getProjectsPermissions().get(project.getProjectId());
return projectPermissions != null && projectPermissions.contains(p);
}).map(project -> new PermissionAssignmentData(p, project.getProjectId()))));
}
return FinderDataBinding.getItemHolder(result);
}
if (global == null || global) {
result = Stream.concat(result, permissions.stream().filter(p -> p.isProjectAssociationSupported()).filter(p -> authorityHolder.isPermissionGrantedGlobally(p)).map(p -> new PermissionAssignmentData(p)));
}
if (global == null || !global) {
result = Stream.concat(result, projects.stream().flatMap(project -> permissions.stream().filter(p -> p.isProjectAssociationSupported()).filter(p -> {
Permissions projectPermissions = authorityHolder.getProjectsPermissions().get(project.getProjectId());
return projectPermissions != null && projectPermissions.contains(p);
}).map(p -> new PermissionAssignmentData(p, project.getProjectId()))));
}
return FinderDataBinding.getItemHolder(result);
}
use of jetbrains.buildServer.serverSide.auth.Permissions in project teamcity-rest by JetBrains.
the class ProjectFinderTest method testUserPermissionDimension.
@Test
public void testUserPermissionDimension() throws Exception {
myFixture.getServerSettings().setPerProjectPermissionsEnabled(true);
ProjectEx root = myProjectManager.getRootProject();
final SProject project10 = createProject("p10", "project 10");
final SProject project10_10 = project10.createProject("p10_10", "p10 child1");
final SProject project20 = createProject("p20", "project 20");
final SProject project30 = createProject("p30", "project 30");
RoleImpl role10 = new RoleImpl("role10", "custom role", new Permissions(Permission.TAG_BUILD), myFixture.getRolesManager());
myFixture.getRolesManager().addRole(role10);
RoleImpl role20 = new RoleImpl("role20", "custom role", new Permissions(Permission.CHANGE_SERVER_SETTINGS, Permission.LABEL_BUILD), myFixture.getRolesManager());
myFixture.getRolesManager().addRole(role20);
RoleImpl role30 = new RoleImpl("role30", "custom role", new Permissions(Permission.RUN_BUILD), myFixture.getRolesManager());
myFixture.getRolesManager().addRole(role30);
role30.addIncludedRole(role10);
final SUser user10 = createUser("user10");
final SUser user20 = createUser("user20");
final SUser user30 = createUser("user30");
final SUser user40 = createUser("user40");
final SUserGroup group10 = myFixture.createUserGroup("group1", "group 1", "");
final SUserGroup group20 = myFixture.createUserGroup("group1.1", "group 1.1", "");
group10.addSubgroup(group20);
group20.addUser(user20);
group10.addRole(RoleScope.projectScope(project10.getProjectId()), role30);
user10.addRole(RoleScope.projectScope(project10_10.getProjectId()), role10);
user30.addRole(RoleScope.globalScope(), role30);
user40.addRole(RoleScope.projectScope(project10_10.getProjectId()), role20);
check(null, getRootProject(), project10, project10_10, project20, project30);
check("userPermission:(user:(id:" + user10.getId() + "),permission:tag_build)", project10_10);
checkExceptionOnItemsSearch(LocatorProcessException.class, "userPermission:(user:(id:" + user10.getId() + "))");
checkExceptionOnItemsSearch(LocatorProcessException.class, "userPermission:(permission:view_project)");
check("userPermission:(user:(id:" + user20.getId() + "),permission:tag_build)", project10, project10_10);
// project permission granted globally
check("userPermission:(user:(id:" + user30.getId() + "),permission:TAG_BUILD)", getRootProject(), project10, project10_10, project20, project30);
check("userPermission:(user:(id:" + user30.getId() + "),permission:change_server_settings)");
// global permission
check("userPermission:(user:(id:" + user40.getId() + "),permission:change_server_settings)", getRootProject(), project10, project10_10, project20, project30);
check("userPermission:(user:(id:" + user40.getId() + "),permission:TAG_BUILD)");
RoleImpl role11 = new RoleImpl("role11", "custom role", new Permissions(Permission.VIEW_PROJECT), myFixture.getRolesManager());
myFixture.getRolesManager().addRole(role11);
user10.addRole(RoleScope.projectScope(project10_10.getProjectId()), role11);
// view project is propagated on top
check("userPermission:(user:(id:" + user10.getId() + "),permission:view_project)", getRootProject(), project10, project10_10);
}
use of jetbrains.buildServer.serverSide.auth.Permissions in project teamcity-rest by JetBrains.
the class ServerRequestTest method test_user_needs_view_agent_details_permission_to_access_avaliable_agents.
@Test(description = "TW-68673")
public void test_user_needs_view_agent_details_permission_to_access_avaliable_agents() throws Throwable {
MockAuthorityHolder mockUser = new MockAuthorityHolder();
mockUser.globalPerms = new Permissions(Permission.VIEW_AGENT_DETAILS);
myFixture.getSecurityContext().runAs(mockUser, () -> {
LicensingData data = myRequest.getLicensingData("maxAgents,serverLicenseType,agentsLeft");
assertNull(data.maxAgents);
assertNull(data.serverLicenseType);
assertNotNull(data.getAgentsLeft());
});
}
use of jetbrains.buildServer.serverSide.auth.Permissions in project teamcity-rest by JetBrains.
the class ServerRequestTest method test_user_needs_manage_server_licenses_permission_to_access_licensing_data.
@Test
public void test_user_needs_manage_server_licenses_permission_to_access_licensing_data() throws Throwable {
MockAuthorityHolder mockUser = new MockAuthorityHolder();
myFixture.getSecurityContext().runAs(mockUser, () -> {
LicensingData data = myRequest.getLicensingData("maxAgents,serverLicenseType,agentsLeft");
assertNull(data.maxAgents);
assertNull(data.serverLicenseType);
assertNull(data.getAgentsLeft());
});
mockUser.globalPerms = new Permissions(Permission.MANAGE_SERVER_LICENSES);
myFixture.getSecurityContext().runAs(mockUser, () -> {
LicensingData data = myRequest.getLicensingData("maxAgents,serverLicenseType,agentsLeft");
assertNotNull(data.maxAgents);
assertNotNull(data.serverLicenseType);
assertNotNull(data.getAgentsLeft());
});
}
use of jetbrains.buildServer.serverSide.auth.Permissions in project teamcity-rest by JetBrains.
the class DebugRequest method getRolesStringPresentation.
@NotNull
public static String getRolesStringPresentation(@NotNull final AuthorityHolder authorityHolder, @NotNull final ProjectManager projectManager) {
StringBuilder result = new StringBuilder();
final Permission[] globalPermissions = authorityHolder.getGlobalPermissions().toArray();
if (globalPermissions.length > 0) {
result.append("Global:\n");
for (Permission p : globalPermissions) {
result.append("\t").append(p.getName()).append("\n");
}
}
for (Map.Entry<String, Permissions> permissionsEntry : authorityHolder.getProjectsPermissions().entrySet()) {
SProject projectById = null;
try {
projectById = projectManager.findProjectById(permissionsEntry.getKey());
} catch (Exception e) {
// ignore
}
if (projectById != null) {
result.append("Project ").append(projectById.describe(false)).append("\n");
} else {
result.append("Project internal id: ").append(permissionsEntry.getKey()).append("\n");
}
for (Permission p : permissionsEntry.getValue().toArray()) {
result.append("\t").append(p.getName()).append("\n");
}
}
return result.toString();
}
Aggregations