Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPMRPP-96643 migrate role validations #64

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies {
compile 'com.epam.reportportal:commons'
compile 'com.epam.reportportal:commons-dao'
} else {
implementation 'com.github.reportportal:commons-dao:4caa252'
implementation 'com.github.reportportal:commons-dao:a072e00'
api 'com.github.reportportal:commons:feature~orgs-SNAPSHOT'
}
api 'org.pf4j:pf4j:3.10.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,59 @@
package com.epam.reportportal.extension;

import static java.util.Optional.ofNullable;

import com.epam.reportportal.rules.commons.validation.BusinessRule;
import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.commons.ReportPortalUser;
import com.epam.ta.reportportal.dao.ProjectRepository;
import com.epam.ta.reportportal.dao.organization.OrganizationRepositoryCustom;
import com.epam.ta.reportportal.entity.organization.Organization;
import com.epam.ta.reportportal.entity.organization.OrganizationRole;
import com.epam.ta.reportportal.entity.project.Project;
import com.epam.ta.reportportal.entity.project.ProjectRole;
import com.epam.reportportal.rules.exception.ErrorType;

import static java.util.Optional.ofNullable;
import com.epam.ta.reportportal.entity.user.UserRole;
import java.util.Map.Entry;

/**
* @author <a href="mailto:[email protected]">Ivan Budayeu</a>
*/
public abstract class ProjectManagerCommand<T> extends ProjectMemberCommand<T> {

protected ProjectManagerCommand(ProjectRepository projectRepository) {
super(projectRepository);
protected ProjectManagerCommand(ProjectRepository projectRepository, OrganizationRepositoryCustom organizationRepository) {
super(projectRepository, organizationRepository);
}

@Override
protected void validatePermissions(ReportPortalUser user, Project project) {
ProjectRole projectRole = ofNullable(user.getProjectDetails()).flatMap(detailsMapping -> ofNullable(detailsMapping.get(project.getName())))
.map(ReportPortalUser.ProjectDetails::getProjectRole)
.orElseThrow(() -> new ReportPortalException(ErrorType.ACCESS_DENIED));
Organization organization = organizationRepository.findById(project.getOrganizationId())
.orElseThrow(
() -> new ReportPortalException(ErrorType.NOT_FOUND, project.getOrganizationId()));

if (user.getUserRole() == UserRole.ADMINISTRATOR) {
return;
}

OrganizationRole orgRole = ofNullable(user.getOrganizationDetails())
.flatMap(detailsMapping -> ofNullable(detailsMapping.get(organization.getName())))
.map(ReportPortalUser.OrganizationDetails::getOrgRole)
.orElseThrow(() -> new ReportPortalException(ErrorType.ACCESS_DENIED));

if (orgRole.sameOrHigherThan(OrganizationRole.MANAGER)) {
return;
}

var projectRole = user.getOrganizationDetails().entrySet().stream()
.filter(entry -> entry.getKey().equals(organization.getName()))
.map(Entry::getValue)
.flatMap(orgDetails -> orgDetails.getProjectDetails().entrySet().stream())
.map(Entry::getValue)
.filter(details -> details.getProjectId().equals(project.getId()))
.map(ReportPortalUser.OrganizationDetails.ProjectDetails::getProjectRole)
.findFirst()
.orElseThrow(() -> new ReportPortalException(ErrorType.ACCESS_DENIED));

BusinessRule.expect(projectRole, ProjectRole.PROJECT_MANAGER::sameOrLowerThan).verify(ErrorType.ACCESS_DENIED);
BusinessRule.expect(projectRole, ProjectRole.EDITOR::sameOrLowerThan)
.verify(ErrorType.ACCESS_DENIED);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.epam.reportportal.extension;

import static java.util.Optional.ofNullable;

import com.epam.reportportal.rules.commons.validation.BusinessRule;
import com.epam.reportportal.rules.commons.validation.Suppliers;
import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.commons.ReportPortalUser;
import com.epam.ta.reportportal.dao.ProjectRepository;
import com.epam.ta.reportportal.dao.organization.OrganizationRepositoryCustom;
import com.epam.ta.reportportal.entity.organization.Organization;
import com.epam.ta.reportportal.entity.project.Project;
import com.epam.reportportal.rules.exception.ErrorType;
import org.springframework.security.core.context.SecurityContextHolder;

import com.epam.ta.reportportal.entity.user.UserRole;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;

import static java.util.Optional.ofNullable;
import org.springframework.security.core.context.SecurityContextHolder;

/**
* @author <a href="mailto:[email protected]">Ivan Budayeu</a>
Expand All @@ -24,10 +26,13 @@ public abstract class ProjectMemberCommand<T> extends AbstractRoleBasedCommand<T


protected final ProjectRepository projectRepository;
protected final OrganizationRepositoryCustom organizationRepository;

protected ProjectMemberCommand(ProjectRepository projectRepository) {

protected ProjectMemberCommand(ProjectRepository projectRepository, OrganizationRepositoryCustom organizationRepository) {
this.projectRepository = projectRepository;
}
this.organizationRepository = organizationRepository;
}

@Override
public void validateRole(Map<String, Object> params) {
Expand All @@ -42,9 +47,20 @@ public void validateRole(Map<String, Object> params) {
}

protected void validatePermissions(ReportPortalUser user, Project project) {
BusinessRule.expect(ofNullable(user.getProjectDetails()).flatMap(detailsMapping -> ofNullable(detailsMapping.get(project.getName()))),
Optional::isPresent
).verify(ErrorType.ACCESS_DENIED);
if (user.getUserRole() == UserRole.ADMINISTRATOR) {
return;
}
Organization organization = organizationRepository.findById(project.getOrganizationId())
.orElseThrow(() -> new ReportPortalException(ErrorType.NOT_FOUND));

user.getOrganizationDetails().entrySet().stream()
.filter(entry -> entry.getKey().equals(organization.getName()))
.map(Entry::getValue)
.flatMap(orgDetails -> orgDetails.getProjectDetails().entrySet().stream())
.map(Entry::getValue)
.filter(details -> details.getProjectId().equals(project.getId()))
.findFirst()
.orElseThrow(() -> new ReportPortalException(ErrorType.ACCESS_DENIED));
}

public static Long retrieveLong(Map<String, Object> params, String param) {
Expand Down
Loading