Skip to content

Commit 5a11d5e

Browse files
Hide contact dropdown from everyone but admins (#533)
* hide known contacts from everyone but admin * address review comments * replace button by labeled checkbox * remove unnecessary throws * address code smells * only set contacts if there are any --------- Co-authored-by: Steffengreiner <[email protected]>
1 parent 925bbd9 commit 5a11d5e

File tree

7 files changed

+66
-32
lines changed

7 files changed

+66
-32
lines changed

project-management/src/main/java/life/qbic/projectmanagement/application/ContactRepository.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package life.qbic.projectmanagement.application;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45
import life.qbic.projectmanagement.domain.model.project.Contact;
6+
import org.springframework.security.access.prepost.PostFilter;
57
import org.springframework.stereotype.Component;
68

79
@Component
810
public class ContactRepository {
911

12+
@PostFilter("hasAnyAuthority('ROLE_ADMIN')")
1013
public List<Contact> findAll() {
11-
//TODO implement
12-
return dummyContacts();
14+
//TODO implement
15+
return dummyContacts();
1316
}
1417

1518
private static List<Contact> dummyContacts() {
16-
return List.of(
19+
return new ArrayList<>(List.of(
1720
new Contact("Max Mustermann", "[email protected]"),
1821
new Contact("David Müller", "[email protected]"),
1922
new Contact("John Koch", "[email protected]"),
@@ -22,7 +25,7 @@ private static List<Contact> dummyContacts() {
2225
new Contact("Anna Bell", "[email protected]"),
2326
new Contact("Sophia Turner", "[email protected]"),
2427
new Contact("Tylor Smith", "[email protected]")
25-
);
28+
));
2629
}
2730

2831

user-interface/frontend/themes/datamanager/components/dialog.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ vaadin-dialog-overlay::part(title) {
216216
}
217217

218218
.contact-field .prefill-input-fields .contact-self-select {
219-
width: 5em;
219+
width: 36em;
220220
}
221221

222222
.edit-project-dialog .form-content .contact-field .name-field {

user-interface/src/main/java/life/qbic/datamanager/views/general/contact/AutocompleteContactField.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import static java.util.Objects.isNull;
44

5-
import com.vaadin.flow.component.ClickEvent;
6-
import com.vaadin.flow.component.button.Button;
5+
import com.vaadin.flow.component.checkbox.Checkbox;
76
import com.vaadin.flow.component.combobox.ComboBox;
87
import com.vaadin.flow.component.customfield.CustomField;
98
import com.vaadin.flow.component.html.Div;
@@ -32,12 +31,12 @@ public class AutocompleteContactField extends CustomField<Contact> implements
3231

3332

3433
private final ComboBox<Contact> contactSelection;
35-
private final Button selfSelect;
34+
private final Checkbox selfSelect;
3635
private final TextField nameField;
3736
private final TextField emailField;
3837
private final Binder<Contact> binder;
3938

40-
public AutocompleteContactField(String label) {
39+
public AutocompleteContactField(String label, String shortName) {
4140
setLabel(label);
4241
addClassName("contact-field");
4342
binder = new Binder<>();
@@ -52,9 +51,9 @@ public AutocompleteContactField(String label) {
5251
contactSelection.setItemLabelGenerator(
5352
contact -> "%s - %s".formatted(contact.getFullName(), contact.getEmail()));
5453

55-
selfSelect = new Button("Myself");
54+
selfSelect = new Checkbox("Choose myself as %s for this project".formatted(shortName));
5655
selfSelect.addClassName("contact-self-select");
57-
selfSelect.addClickListener(this::onSelfSelected);
56+
selfSelect.addValueChangeListener(this::onSelfSelected);
5857

5958
nameField = new TextField();
6059
nameField.setRequired(false);
@@ -98,11 +97,13 @@ public AutocompleteContactField(String label) {
9897
clear();
9998
}
10099

101-
private void onSelfSelected(ClickEvent<Button> buttonClickEvent) {
102-
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
103-
QbicUserDetails details = (QbicUserDetails) authentication.getPrincipal();
104-
Contact userAsContact = new Contact(details.fullName(), details.getEmailAddress());
105-
setContact(userAsContact);
100+
private void onSelfSelected(ComponentValueChangeEvent<Checkbox, Boolean> checkboxvalueChangeEvent) {
101+
if(checkboxvalueChangeEvent.getValue().booleanValue()) {
102+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
103+
QbicUserDetails details = (QbicUserDetails) authentication.getPrincipal();
104+
Contact userAsContact = new Contact(details.fullName(), details.getEmailAddress());
105+
setContact(userAsContact);
106+
}
106107
}
107108

108109
private void updateValidationProperty() {
@@ -182,4 +183,7 @@ public Binder<Contact> getBinder() {
182183
return binder;
183184
}
184185

186+
public void hideContactBox() {
187+
contactSelection.setVisible(false);
188+
}
185189
}

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ public AddProjectDialog(FinanceService financeService,
8080
this.experimentalInformationLayout = new ExperimentalInformationLayout(
8181
ontologyLookupService);
8282

83-
List<Contact> knownContacts = contactRepository.findAll().stream()
84-
.map(contact -> new Contact(contact.fullName(), contact.emailAddress())).toList();
85-
collaboratorsLayout.setPrincipalInvestigators(knownContacts);
86-
collaboratorsLayout.setResponsiblePersons(knownContacts);
87-
collaboratorsLayout.setProjectManagers(knownContacts);
83+
List<Contact> knownContacts = contactRepository.findAll().stream().map(contact ->
84+
new Contact(contact.fullName(), contact.emailAddress())).toList();
85+
if(knownContacts.isEmpty()) {
86+
collaboratorsLayout.hideContactBox();
87+
} else {
88+
collaboratorsLayout.setKnownContacts(knownContacts);
89+
}
8890

8991
stepContent = new HashMap<>();
9092

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public CollaboratorsLayout() {
3737
collaboratorsBinder.setBean(new ProjectCollaborators());
3838
collaboratorsBinder.setFieldsValidationStatusChangeListenerEnabled(true);
3939

40-
principalInvestigatorField = new AutocompleteContactField("Principal Investigator");
40+
principalInvestigatorField = new AutocompleteContactField("Principal Investigator", "PI");
4141
principalInvestigatorField.setRequired(true);
4242
collaboratorsBinder.forField(principalInvestigatorField)
4343
.withNullRepresentation(principalInvestigatorField.getEmptyValue())
4444
.withValidator(it -> principalInvestigatorField.validate().isValid(), "")
4545
.bind(ProjectCollaborators::getPrincipalInvestigator,
4646
ProjectCollaborators::setPrincipalInvestigator);
4747

48-
responsiblePersonField = new AutocompleteContactField("Project Responsible/Co-Investigator (optional)");
48+
responsiblePersonField = new AutocompleteContactField("Project Responsible/Co-Investigator (optional)", "Responsible");
4949
responsiblePersonField.setRequired(false);
5050
responsiblePersonField.setHelperText("Should be contacted about project-related questions");
5151
collaboratorsBinder.forField(responsiblePersonField)
@@ -54,7 +54,7 @@ public CollaboratorsLayout() {
5454
.bind(bean -> bean.getResponsiblePerson().orElse(null),
5555
ProjectCollaborators::setResponsiblePerson);
5656

57-
projectManagerField = new AutocompleteContactField("Project Manager");
57+
projectManagerField = new AutocompleteContactField("Project Manager", "Manager");
5858
projectManagerField.setRequired(true);
5959
collaboratorsBinder.forField(projectManagerField)
6060
.withNullRepresentation(projectManagerField.getEmptyValue())
@@ -118,6 +118,18 @@ public ProjectCollaborators getProjectCollaborators() {
118118
return projectCollaborators;
119119
}
120120

121+
public void setKnownContacts(List<Contact> knownContacts) {
122+
principalInvestigatorField.setItems(knownContacts);
123+
responsiblePersonField.setItems(knownContacts);
124+
projectManagerField.setItems(knownContacts);
125+
}
126+
127+
public void hideContactBox() {
128+
principalInvestigatorField.hideContactBox();
129+
responsiblePersonField.hideContactBox();
130+
projectManagerField.hideContactBox();
131+
}
132+
121133
public static final class ProjectCollaborators implements Serializable {
122134

123135
@Serial

user-interface/src/main/java/life/qbic/datamanager/views/projects/edit/EditProjectInformationDialog.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ public EditProjectInformationDialog(ContactRepository contactRepository) {
5151

5252
formLayout = new EditProjectInformationForm();
5353

54-
List<Contact> knownContacts = contactRepository.findAll().stream()
55-
.map(contact -> new Contact(contact.fullName(), contact.emailAddress())).toList();
56-
formLayout.setPrincipalInvestigators(knownContacts);
57-
formLayout.setResponsiblePersons(knownContacts);
58-
formLayout.setProjectManagers(knownContacts);
54+
List<Contact> knownContacts = contactRepository.findAll().stream().map(contact ->
55+
new Contact(contact.fullName(), contact.emailAddress())).toList();
5956

57+
if(knownContacts.isEmpty()) {
58+
formLayout.hideContactBox();
59+
} else {
60+
formLayout.setKnownContacts(knownContacts);
61+
}
6062

6163
binder = formLayout.getBinder();
6264

user-interface/src/main/java/life/qbic/datamanager/views/projects/edit/EditProjectInformationForm.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ public EditProjectInformationForm() {
8585
projectContactsLayout.add(projectContactsTitle);
8686
projectContactsLayout.add(projectContactsDescription);
8787

88-
principalInvestigatorField = new AutocompleteContactField("Principal Investigator");
88+
principalInvestigatorField = new AutocompleteContactField("Principal Investigator", "PI");
8989
principalInvestigatorField.setRequired(true);
9090
principalInvestigatorField.setId("principal-investigator");
9191
binder.forField(principalInvestigatorField)
9292
.withValidator(it -> principalInvestigatorField.isValid(), "")
9393
.bind((ProjectInformation::getPrincipalInvestigator),
9494
ProjectInformation::setPrincipalInvestigator);
9595

96-
responsiblePersonField = new AutocompleteContactField("Project Responsible (optional)");
96+
responsiblePersonField = new AutocompleteContactField("Project Responsible (optional)", "Responsible");
9797
responsiblePersonField.setRequired(false);
9898
responsiblePersonField.setId("responsible-person");
9999
responsiblePersonField.setHelperText("Should be contacted about project-related questions");
@@ -103,7 +103,7 @@ public EditProjectInformationForm() {
103103
.bind(projectInformation -> projectInformation.getResponsiblePerson().orElse(null),
104104
ProjectInformation::setResponsiblePerson);
105105

106-
projectManagerField = new AutocompleteContactField("Project Manager");
106+
projectManagerField = new AutocompleteContactField("Project Manager", "Manager");
107107
projectManagerField.setRequired(true);
108108
projectManagerField.setId("project-manager");
109109
binder.forField(projectManagerField)
@@ -176,4 +176,15 @@ public Binder<ProjectInformation> getBinder() {
176176
return binder;
177177
}
178178

179+
public void setKnownContacts(List<Contact> knownContacts) {
180+
principalInvestigatorField.setItems(knownContacts);
181+
responsiblePersonField.setItems(knownContacts);
182+
projectManagerField.setItems(knownContacts);
183+
}
184+
185+
public void hideContactBox() {
186+
principalInvestigatorField.hideContactBox();
187+
responsiblePersonField.hideContactBox();
188+
projectManagerField.hideContactBox();
189+
}
179190
}

0 commit comments

Comments
 (0)