Skip to content

Commit b561d91

Browse files
hide offer search from anyone but admin and project-manager (#497)
* hide offer search from anyone but admin and project-manager * typo * allow users to edit access to their projects * Revert "allow users to edit access to their projects" This reverts commit 1e3d8d1.
1 parent f29e76b commit b561d91

File tree

7 files changed

+55
-8
lines changed

7 files changed

+55
-8
lines changed

finances-infrastructure/pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
<version>0.34.0</version>
2424
<scope>compile</scope>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.security</groupId>
28+
<artifactId>spring-security-core</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>life.qbic.identity</groupId>
32+
<artifactId>project-management-infrastructure</artifactId>
33+
<version>0.34.0</version>
34+
<scope>compile</scope>
35+
</dependency>
2636
</dependencies>
2737

2838
</project>

finances-infrastructure/src/main/java/life/qbic/finance/infrastructure/SimpleOfferSearchService.java

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import life.qbic.finance.domain.model.OfferId;
88
import life.qbic.finance.domain.model.OfferPreview;
99
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.security.access.prepost.PreAuthorize;
1011
import org.springframework.stereotype.Service;
1112

1213
/**
@@ -23,20 +24,24 @@ public class SimpleOfferSearchService implements OfferSearchService {
2324

2425
private final OfferRepository offerRepository;
2526

27+
2628
@Override
29+
@PreAuthorize("hasAnyAuthority('ROLE_PROJECT_MANAGER', 'ROLE_ADMIN')")
2730
public List<OfferPreview> findByProjectTitleOrOfferId(String projectTitle, String offerId) {
2831
return offerPreviewRepository.findByProjectTitleContainingIgnoreCaseOrOfferIdContainingIgnoreCase(
2932
projectTitle, offerId);
3033
}
3134

3235
@Override
36+
@PreAuthorize("hasAnyAuthority('ROLE_PROJECT_MANAGER', 'ROLE_ADMIN')")
3337
public List<OfferPreview> findByProjectTitleOrOfferId(String projectTitle, String offerId,
3438
int offset, int limit) {
3539
return offerPreviewRepository.findByProjectTitleContainingIgnoreCaseOrOfferIdContainingIgnoreCase(
3640
projectTitle, offerId, new OffsetBasedRequest(offset, limit)).stream().toList();
3741
}
3842

3943
@Override
44+
@PreAuthorize("hasAnyAuthority('ROLE_PROJECT_MANAGER', 'ROLE_ADMIN')")
4045
public Optional<Offer> findByOfferId(String offerId) {
4146
return Optional.ofNullable(offerRepository.findByOfferId(OfferId.from(offerId)));
4247
}

project-management-infrastructure/src/main/java/life/qbic/projectmanagement/infrastructure/project/ProjectRepositoryImpl.java

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public class ProjectRepositoryImpl implements ProjectRepository {
5252
private static final Logger log = logger(ProjectRepositoryImpl.class);
5353
private final QbicProjectRepo projectRepo;
5454
private final QbicProjectDataRepo projectDataRepo;
55-
5655
private final ProjectAccessService projectAccessService;
5756

5857
@Autowired

user-interface/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@
250250
<version>0.34.0</version>
251251
<scope>compile</scope>
252252
</dependency>
253+
<dependency>
254+
<groupId>org.springframework.security</groupId>
255+
<artifactId>spring-security-core</artifactId>
256+
</dependency>
253257

254258
</dependencies>
255259

user-interface/src/main/java/life/qbic/datamanager/views/projects/create/AddProjectDialog.java

+6
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ public AddProjectDialog(FinanceService financeService,
137137
adaptFooterButtons(stepper.getFirstStep());
138138
}
139139

140+
/**
141+
* Allows user to search the offer database to prefill some project information
142+
*/
143+
public void enableOfferSearch() {
144+
projectDesignLayout.enableOfferSearch();
145+
}
140146

141147
private void onCancelClicked(ClickEvent<Button> clickEvent) {
142148
fireEvent(new CancelEvent(this, clickEvent.isFromClient()));

user-interface/src/main/java/life/qbic/datamanager/views/projects/create/ProjectDesignLayout.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ public class ProjectDesignLayout extends Div implements HasBinderValidation<Proj
4343

4444
private static final Logger log = logger(ProjectDesignLayout.class);
4545
private static final String TITLE = "Project Design";
46-
final ComboBox<OfferSummary> offerSearchField = new ComboBox<>("Offer");
46+
private final ComboBox<OfferSummary> offerSearchField = new ComboBox<>("Offer");
4747
private final TextField codeField = new TextField("Code");
4848
private final TextField titleField = new TextField("Title");
4949
private final TextArea projectDescription = new TextArea("Description");
5050
private final Button generateCodeButton = new Button(new Icon(VaadinIcon.REFRESH));
5151
private final Binder<ProjectDesign> projectDesignBinder = new Binder<>(ProjectDesign.class);
5252
private final FinanceService financeService;
53+
private final Span projectDesignDescription = new Span(
54+
"Specify the name and objective of the research project.");
5355

5456
public ProjectDesignLayout(FinanceService financeService) {
5557
this.financeService = financeService;
@@ -61,11 +63,7 @@ public ProjectDesignLayout(FinanceService financeService) {
6163
private void initLayout() {
6264
Span projectDesignTitle = new Span(TITLE);
6365
projectDesignTitle.addClassName("title");
64-
Span projectDesignDescription = new Span(
65-
"Specify the name and objective of the research project. You can either select a project from the offer list or create a new one.");
66-
offerSearchField.setClassName("search-field");
67-
offerSearchField.setPlaceholder("Search for offers");
68-
offerSearchField.setPrefixComponent(VaadinIcon.SEARCH.create());
66+
6967
codeField.setHelperText("Q and 4 letters/numbers");
7068
codeField.setValue(ProjectCode.random().value());
7169
codeField.addClassName("code-field");
@@ -76,11 +74,24 @@ private void initLayout() {
7674
codeTitleAndButtonSpan.addClassNames("code-and-title");
7775
projectDescription.setPlaceholder("Please enter a description for your project");
7876
projectDescription.addClassName("description-field");
77+
78+
// disable offer access until user authority is known
79+
offerSearchField.setEnabled(false);
80+
offerSearchField.setVisible(false);
81+
offerSearchField.setClassName("search-field");
82+
offerSearchField.setPlaceholder("Search for offers");
83+
offerSearchField.setPrefixComponent(VaadinIcon.SEARCH.create());
7984
add(projectDesignTitle, projectDesignDescription, offerSearchField, codeTitleAndButtonSpan,
8085
projectDescription);
8186
addClassName("project-design-layout");
8287
}
8388

89+
public void enableOfferSearch() {
90+
offerSearchField.setEnabled(true);
91+
offerSearchField.setVisible(true);
92+
projectDesignDescription.add(" You can either select a project from the offer list or create a new one.");
93+
}
94+
8495
private void initCodeGenerationButton() {
8596
generateCodeButton.setTooltipText("Generate Project Code");
8697
generateCodeButton.addThemeVariants(ButtonVariant.LUMO_ICON);
@@ -209,7 +220,6 @@ public String getDefaultErrorMessage() {
209220
return "Invalid Input found in Project Design";
210221
}
211222

212-
213223
public static final class ProjectDesign implements Serializable {
214224

215225
@Serial

user-interface/src/main/java/life/qbic/datamanager/views/projects/overview/ProjectOverviewMain.java

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import com.vaadin.flow.router.Route;
77
import jakarta.annotation.security.PermitAll;
88
import java.io.Serial;
9+
import java.util.Arrays;
10+
import java.util.HashSet;
11+
import java.util.Set;
912
import life.qbic.application.commons.ApplicationException;
1013
import life.qbic.application.commons.Result;
1114
import life.qbic.datamanager.views.AppRoutes.Projects;
@@ -26,6 +29,7 @@
2629
import life.qbic.projectmanagement.domain.model.project.Funding;
2730
import life.qbic.projectmanagement.domain.model.project.Project;
2831
import org.springframework.beans.factory.annotation.Autowired;
32+
import org.springframework.security.core.context.SecurityContextHolder;
2933

3034
/**
3135
* Project overview {@link Main} component that shows project information and additional components to manage project
@@ -63,6 +67,9 @@ public ProjectOverviewMain(@Autowired ProjectCollectionComponent projectCollecti
6367
this.projectCollectionComponent.addCreateClickedListener(projectCreationClickedEvent -> {
6468
AddProjectDialog addProjectDialog = new AddProjectDialog(this.financeService,
6569
this.ontologyTermInformationService, this.contactRepository);
70+
if(isOfferSearchAllowed()) {
71+
addProjectDialog.enableOfferSearch();
72+
}
6673
addProjectDialog.addConfirmListener(this::createProject);
6774
addProjectDialog.addCancelListener(it -> it.getSource().close());
6875
addProjectDialog.open();
@@ -75,6 +82,12 @@ public ProjectOverviewMain(@Autowired ProjectCollectionComponent projectCollecti
7582
System.identityHashCode(projectCollectionComponent)));
7683
}
7784

85+
private boolean isOfferSearchAllowed() {
86+
Set<String> allowedRoles = new HashSet<>(Arrays.asList("ROLE_ADMIN", "ROLE_PROJECT_MANAGER"));
87+
return SecurityContextHolder.getContext().getAuthentication().getAuthorities().stream()
88+
.anyMatch(r -> allowedRoles.contains(r.getAuthority()));
89+
}
90+
7891
private void createProject(ConfirmEvent confirmEvent) {
7992
Funding funding = null;
8093
if (confirmEvent.getFundingEntry() != null && !confirmEvent.getFundingEntry()

0 commit comments

Comments
 (0)