Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit bf5a137

Browse files
lgallgal
authored andcommitted
Merge remote-tracking branch 'origin/2624_sormas-to-sormas-rest-endpoints' into 2624_sormas-to-sormas-rest-endpoints
# Conflicts: # sormas-backend/src/main/java/de/symeda/sormas/backend/user/KeycloakService.java
2 parents b824f94 + 5f0a064 commit bf5a137

7 files changed

Lines changed: 69 additions & 48 deletions

File tree

sormas-backend/src/main/java/de/symeda/sormas/backend/common/StartupShutdownService.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
import javax.persistence.EntityManager;
4646
import javax.persistence.PersistenceContext;
4747

48+
import de.symeda.sormas.backend.user.event.MockPasswordUpdateEvent;
4849
import de.symeda.sormas.backend.user.event.MockUserCreateEvent;
50+
import de.symeda.sormas.backend.user.event.PasswordResetEvent;
4951
import de.symeda.sormas.backend.user.event.UserCreateEvent;
5052
import org.slf4j.Logger;
5153
import org.slf4j.LoggerFactory;
@@ -124,18 +126,8 @@ public class StartupShutdownService {
124126
@EJB
125127
private UserService userService;
126128
@EJB
127-
private CaseService caseService;
128-
@EJB
129129
private ContactService contactService;
130130
@EJB
131-
private EventParticipantService eventParticipantService;
132-
@EJB
133-
private EpiDataService epiDataService;
134-
@EJB
135-
private SymptomsService symptomsService;
136-
@EJB
137-
private PersonService personService;
138-
@EJB
139131
private RegionService regionService;
140132
@EJB
141133
private DistrictService districtService;
@@ -150,8 +142,6 @@ public class StartupShutdownService {
150142
@EJB
151143
private ImportFacadeEjbLocal importFacade;
152144
@EJB
153-
private DiseaseConfigurationFacadeEjbLocal diseaseConfigurationFacade;
154-
@EJB
155145
private DiseaseConfigurationService diseaseConfigurationService;
156146
@EJB
157147
private FeatureConfigurationService featureConfigurationService;
@@ -161,6 +151,9 @@ public class StartupShutdownService {
161151
@Inject
162152
private Event<UserCreateEvent> userCreateEvent;
163153

154+
@Inject
155+
private Event<PasswordResetEvent> passwordResetEvent;
156+
164157
@PostConstruct
165158
public void startup() {
166159

@@ -454,13 +447,15 @@ private void createOrUpdateSormasToSormasUser() {
454447
newUser.setUserName(SORMAS_TO_SORMAS_USER_NAME);
455448

456449
userService.persist(newUser);
450+
userCreateEvent.fire(new MockUserCreateEvent(newUser, sormasToSormasUserPassword));
457451
}
458452
} else if (!DataHelper
459453
.equal(sormasToSormasUser.getPassword(), PasswordHelper.encodePassword(sormasToSormasUserPassword, sormasToSormasUser.getSeed()))) {
460454
sormasToSormasUser.setSeed(PasswordHelper.createPass(16));
461455
sormasToSormasUser.setPassword(PasswordHelper.encodePassword(sormasToSormasUserPassword, sormasToSormasUser.getSeed()));
462456

463457
userService.persist(sormasToSormasUser);
458+
passwordResetEvent.fire(new MockPasswordUpdateEvent(sormasToSormasUser, sormasToSormasUserPassword));
464459
}
465460
}));
466461
}

sormas-backend/src/main/java/de/symeda/sormas/backend/user/KeycloakService.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,9 @@
1919
package de.symeda.sormas.backend.user;
2020

2121
import com.nimbusds.jose.util.JSONObjectUtils;
22-
import de.symeda.sormas.api.AuthProvider;
2322
import de.symeda.sormas.api.Language;
2423
import de.symeda.sormas.api.user.UserRole;
25-
import de.symeda.sormas.backend.common.ConfigFacadeEjb.ConfigFacadeEjbLocal;
26-
import de.symeda.sormas.backend.user.event.MockUserCreateEvent;
27-
import de.symeda.sormas.backend.user.event.PasswordResetEvent;
28-
import de.symeda.sormas.backend.user.event.UserCreateEvent;
29-
import de.symeda.sormas.backend.user.event.UserUpdateEvent;
24+
import de.symeda.sormas.backend.user.event.*;
3025
import net.minidev.json.JSONObject;
3126
import org.apache.commons.collections.CollectionUtils;
3227
import org.apache.commons.lang3.StringUtils;
@@ -41,7 +36,6 @@
4136
import org.slf4j.LoggerFactory;
4237

4338
import javax.annotation.PostConstruct;
44-
import javax.ejb.EJB;
4539
import javax.ejb.LocalBean;
4640
import javax.ejb.Stateless;
4741
import javax.enterprise.event.Observes;
@@ -70,9 +64,6 @@ public class KeycloakService {
7064

7165
private final Logger logger = LoggerFactory.getLogger(getClass());
7266

73-
@EJB
74-
private ConfigFacadeEjbLocal configFacade;
75-
7667
private static final String OIDC_REALM = "realm";
7768
private static final String OIDC_SERVER_URL = "auth-server-url";
7869
private static final String OIDC_CREDENTIALS = "credentials";
@@ -89,12 +80,6 @@ public class KeycloakService {
8980

9081
@PostConstruct
9182
public void init() {
92-
93-
if(!AuthProvider.KEYCLOAK.equalsIgnoreCase(configFacade.getAuthenticationProvider())) {
94-
logger.info("Keycloak Auth Provider not active");
95-
return;
96-
}
97-
9883
Optional<String> oidcJson = ConfigProvider.getConfig().getOptionalValue("sormas.backend.security.oidc.json", String.class);
9984

10085
if (!oidcJson.isPresent()) {
@@ -126,20 +111,17 @@ public void handleUserCreateEvent(@Observes UserCreateEvent userCreateEvent) {
126111
return;
127112
}
128113

129-
User user = userCreateEvent.getUser();
130114
String password = null;
131115
if (userCreateEvent instanceof MockUserCreateEvent) {
132116
password = ((MockUserCreateEvent) userCreateEvent).getPassword();
133-
Optional<UserRepresentation> mockUser = getUserByUsername(keycloak.get(), user.getUserName());
134-
if (mockUser.isPresent()) {
135-
logger.info("Mock user {} already exists. Will not create a new one", user.getUserName());
136-
return;
137-
}
138117
}
139118

119+
User user = userCreateEvent.getUser();
140120
String userId = createUser(keycloak.get(), user, password);
141121
if (StringUtils.isNotBlank(user.getUserEmail())) {
142122
sendActivationEmail(keycloak.get(), userId);
123+
} else {
124+
logger.warn("Cannot send activation email, because the user has no email");
143125
}
144126
}
145127

@@ -176,7 +158,18 @@ public void handlePasswordResetEvent(@Observes PasswordResetEvent passwordResetE
176158
logger.warn("Cannot find user to update for username {}", user.getUserName());
177159
return;
178160
}
179-
userRepresentation.ifPresent(existing -> sendPasswordResetEmail(keycloak.get(), existing.getId()));
161+
162+
String userId = userRepresentation.get().getId();
163+
164+
if (passwordResetEvent instanceof MockPasswordUpdateEvent) {
165+
UserRepresentation existingUser = userRepresentation.get();
166+
setCredentials(existingUser, ((MockPasswordUpdateEvent) passwordResetEvent).getPassword());
167+
keycloak.get().realms().realm(REALM_NAME).users().get(userId).update(existingUser);
168+
} else if (StringUtils.isNotBlank(user.getUserEmail())) {
169+
sendPasswordResetEmail(keycloak.get(), userId);
170+
} else {
171+
logger.warn("Cannot send password reset email, because the user has no email");
172+
}
180173
}
181174

182175
private UserRepresentation createUserRepresentation(User user, String password) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* SORMAS® - Surveillance Outbreak Response Management & Analysis System
3+
* Copyright © 2016-2020 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package de.symeda.sormas.backend.user.event;
20+
21+
import de.symeda.sormas.backend.user.User;
22+
23+
/**
24+
* Event used for hard coded user password changes.
25+
*
26+
* @author Alex Vidrean
27+
* @since 05-Oct-20
28+
*/
29+
public class MockPasswordUpdateEvent extends PasswordResetEvent {
30+
31+
private final String password;
32+
33+
public MockPasswordUpdateEvent(User user, String password) {
34+
super(user);
35+
this.password = password;
36+
}
37+
38+
public String getPassword() {
39+
return password;
40+
}
41+
}

sormas-backend/src/main/java/de/symeda/sormas/backend/user/event/MockUserCreateEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
public class MockUserCreateEvent extends UserCreateEvent {
3030

31-
private String password;
31+
private final String password;
3232

3333
public MockUserCreateEvent(User user, String password) {
3434
super(user);

sormas-backend/src/main/java/de/symeda/sormas/backend/user/event/PasswordResetEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
public class PasswordResetEvent {
3030

31-
private User user;
31+
private final User user;
3232

3333
public PasswordResetEvent(User user) {
3434
this.user = user;

sormas-backend/src/main/java/de/symeda/sormas/backend/user/event/UserCreateEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
public class UserCreateEvent {
3030

31-
private User user;
31+
private final User user;
3232

3333
public UserCreateEvent(User user) {
3434
this.user = user;

sormas-backend/src/main/java/de/symeda/sormas/backend/user/event/UserUpdateEvent.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
*/
2929
public class UserUpdateEvent {
3030

31-
private User oldUser;
31+
private final User oldUser;
3232

33-
private User newUser;
33+
private final User newUser;
3434

3535
public UserUpdateEvent(User oldUser, User newUser) {
3636
this.oldUser = oldUser;
@@ -41,15 +41,7 @@ public User getOldUser() {
4141
return oldUser;
4242
}
4343

44-
public void setOldUser(User oldUser) {
45-
this.oldUser = oldUser;
46-
}
47-
4844
public User getNewUser() {
4945
return newUser;
5046
}
51-
52-
public void setNewUser(User newUser) {
53-
this.newUser = newUser;
54-
}
5547
}

0 commit comments

Comments
 (0)