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

Commit 5f0a064

Browse files
committed
SORMAS-Foundation#2976 Create sormas2sormas user in Keycloak
1 parent e356ccb commit 5f0a064

7 files changed

Lines changed: 68 additions & 30 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: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import com.nimbusds.jose.util.JSONObjectUtils;
2222
import de.symeda.sormas.api.Language;
2323
import de.symeda.sormas.api.user.UserRole;
24-
import de.symeda.sormas.backend.user.event.MockUserCreateEvent;
25-
import de.symeda.sormas.backend.user.event.PasswordResetEvent;
26-
import de.symeda.sormas.backend.user.event.UserCreateEvent;
27-
import de.symeda.sormas.backend.user.event.UserUpdateEvent;
24+
import de.symeda.sormas.backend.user.event.*;
2825
import net.minidev.json.JSONObject;
2926
import org.apache.commons.collections.CollectionUtils;
3027
import org.apache.commons.lang3.StringUtils;
@@ -123,6 +120,8 @@ public void handleUserCreateEvent(@Observes UserCreateEvent userCreateEvent) {
123120
String userId = createUser(keycloak.get(), user, password);
124121
if (StringUtils.isNotBlank(user.getUserEmail())) {
125122
sendActivationEmail(keycloak.get(), userId);
123+
} else {
124+
logger.warn("Cannot send activation email, because the user has no email");
126125
}
127126
}
128127

@@ -159,7 +158,18 @@ public void handlePasswordResetEvent(@Observes PasswordResetEvent passwordResetE
159158
logger.warn("Cannot find user to update for username {}", user.getUserName());
160159
return;
161160
}
162-
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+
}
163173
}
164174

165175
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)