Skip to content

Commit ae53bc3

Browse files
authored
Merge pull request #257 from entando/ENG-5444
ENG-5444: Removed data associated with deleted user
2 parents 1146635 + 848fb4f commit ae53bc3

9 files changed

Lines changed: 391 additions & 186 deletions

File tree

engine/src/main/java/org/entando/entando/aps/system/services/userpreferences/UserPreferencesManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
package org.entando.entando.aps.system.services.userpreferences;
1515

1616
import java.util.Optional;
17+
import lombok.Setter;
18+
import lombok.extern.slf4j.Slf4j;
1719
import org.entando.entando.ent.exception.EntException;
1820

21+
@Slf4j
22+
@Setter
1923
public class UserPreferencesManager implements IUserPreferencesManager {
2024

2125
private UserPreferencesDAO userPreferencesDAO;
@@ -57,9 +61,5 @@ public void updateUserGravatarPreference(String username, boolean enabled) throw
5761
public void deleteUserPreferences(String username) throws EntException {
5862
userPreferencesDAO.deleteUserPreferences(username);
5963
}
60-
61-
public void setUserPreferencesDAO(UserPreferencesDAO userPreferencesDAO) {
62-
this.userPreferencesDAO = userPreferencesDAO;
63-
}
6464

6565
}

engine/src/main/java/org/entando/entando/aps/system/services/userprofile/AvatarService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public void deleteAvatar(UserDetails userDetails, BindingResult bindingResult) {
105105
}
106106
}
107107

108+
@Override
109+
public void deleteAvatar(String username) throws EntException {
110+
this.deletePrevUserAvatarFromFileSystemIfPresent(username);
111+
}
112+
108113
//------------------------ Utility methods ------------------------------------//
109114

110115
private FileBrowserFileRequest addProfileImageToFileSystem(

engine/src/main/java/org/entando/entando/aps/system/services/userprofile/IAvatarService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
import com.agiletec.aps.system.services.user.UserDetails;
44
import org.entando.entando.aps.system.services.userprofile.model.AvatarDto;
5+
import org.entando.entando.ent.exception.EntException;
56
import org.entando.entando.web.userprofile.model.ProfileAvatarRequest;
67
import org.springframework.validation.BindingResult;
78

89
public interface IAvatarService {
10+
911
AvatarDto getAvatarData(UserDetails userDetails);
1012

11-
1213
String updateAvatar(ProfileAvatarRequest request, UserDetails userDetails,
1314
BindingResult bindingResult);
1415

1516
void deleteAvatar(UserDetails userDetails, BindingResult bindingResult);
17+
18+
void deleteAvatar(String username) throws EntException;
19+
1620
}

engine/src/main/java/org/entando/entando/aps/system/services/userprofile/UserProfileManagerAspect.java renamed to engine/src/main/java/org/entando/entando/aps/system/services/userprofile/UserManagementAspect.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.entando.entando.ent.util.EntLogging.EntLogFactory;
2121
import com.agiletec.aps.system.services.user.AbstractUser;
2222
import com.agiletec.aps.system.services.user.UserDetails;
23+
import org.entando.entando.aps.system.services.userpreferences.IUserPreferencesManager;
24+
import org.entando.entando.ent.exception.EntException;
25+
import org.springframework.beans.factory.annotation.Autowired;
2326

2427
/**
2528
* Implementation of ProfileManager Aspect. This class join a user with his
@@ -28,11 +31,21 @@
2831
* @author E.Santoboni
2932
*/
3033
@Aspect
31-
public class UserProfileManagerAspect {
34+
public class UserManagementAspect {
3235

33-
private static final EntLogger logger = EntLogFactory.getSanitizedLogger(UserProfileManagerAspect.class);
36+
private static final EntLogger logger = EntLogFactory.getSanitizedLogger(UserManagementAspect.class);
3437

35-
private IUserProfileManager userProfileManager;
38+
private final IUserProfileManager userProfileManager;
39+
private final IAvatarService avatarService;
40+
private final IUserPreferencesManager userPreferencesManager;
41+
42+
@Autowired
43+
public UserManagementAspect(IUserProfileManager userProfileManager,
44+
IAvatarService avatarService, IUserPreferencesManager userPreferencesManager) {
45+
this.userProfileManager = userProfileManager;
46+
this.userPreferencesManager = userPreferencesManager;
47+
this.avatarService = avatarService;
48+
}
3649

3750
@AfterReturning(pointcut = "execution(* com.agiletec.aps.system.services.user.IUserManager.getUser(..))", returning = "user")
3851
public void injectProfile(Object user) {
@@ -80,7 +93,7 @@ public void updateProfile(Object user) {
8093
}
8194

8295
@AfterReturning(pointcut = "execution(* com.agiletec.aps.system.services.user.IUserManager.removeUser(..)) && args(key)")
83-
public void deleteProfile(Object key) {
96+
public void deleteUserData(Object key) {
8497
String username = null;
8598
if (key instanceof String) {
8699
username = key.toString();
@@ -91,18 +104,24 @@ public void deleteProfile(Object key) {
91104
if (username != null) {
92105
try {
93106
this.getUserProfileManager().deleteProfile(username);
94-
} catch (Throwable t) {
95-
logger.error("Error deleting profile. user: {}", username, t);
107+
} catch (EntException t) {
108+
logger.error("Error deleting user profile. user: {}", username, t);
109+
}
110+
try {
111+
this.avatarService.deleteAvatar(username);
112+
} catch (EntException t) {
113+
logger.error("Error deleting user avatar. user: {}", username, t);
114+
}
115+
try {
116+
this.userPreferencesManager.deleteUserPreferences(username);
117+
} catch (EntException t) {
118+
logger.error("Error deleting user preverences. user: {}", username, t);
96119
}
97120
}
98121
}
99122

100123
protected IUserProfileManager getUserProfileManager() {
101124
return userProfileManager;
102125
}
103-
104-
public void setUserProfileManager(IUserProfileManager userProfileManager) {
105-
this.userProfileManager = userProfileManager;
106-
}
107126

108127
}

engine/src/main/resources/spring/aps/managers/userprofileManagers.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
</property>
3939
</bean>
4040

41-
<bean id="UserProfileManagerAspect" class="org.entando.entando.aps.system.services.userprofile.UserProfileManagerAspect">
42-
<property name="userProfileManager" ref="UserProfileManager" />
43-
</bean>
41+
<bean id="UserManagementAspect" class="org.entando.entando.aps.system.services.userprofile.UserManagementAspect" />
4442

4543
<!-- API -->
4644

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
/*
2+
* Copyright 2018-Present Entando Inc. (http://www.entando.com) All rights reserved.
3+
*
4+
* This library is free software; you can redistribute it and/or modify it under
5+
* the terms of the GNU Lesser General Public License as published by the Free
6+
* Software Foundation; either version 2.1 of the License, or (at your option)
7+
* any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12+
* details.
13+
*/
14+
package org.entando.entando.aps.system.services.userprofile;
15+
16+
import static org.mockito.Mockito.when;
17+
18+
import com.agiletec.aps.system.SystemConstants;
19+
import com.agiletec.aps.system.common.entity.model.attribute.MonoTextAttribute;
20+
import com.agiletec.aps.system.common.entity.parse.attribute.MonoTextAttributeHandler;
21+
import com.agiletec.aps.system.services.user.User;
22+
import org.entando.entando.aps.system.services.userpreferences.IUserPreferencesManager;
23+
import org.entando.entando.aps.system.services.userprofile.model.IUserProfile;
24+
import org.entando.entando.aps.system.services.userprofile.model.UserProfile;
25+
import org.entando.entando.ent.exception.EntException;
26+
import org.junit.jupiter.api.Assertions;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.mockito.InjectMocks;
30+
import org.mockito.Mock;
31+
import org.mockito.Mockito;
32+
import org.mockito.junit.jupiter.MockitoExtension;
33+
34+
@ExtendWith(MockitoExtension.class)
35+
class UserManagementAspectTest {
36+
37+
@InjectMocks
38+
private UserManagementAspect userManagementAspect;
39+
40+
@Mock
41+
private UserProfileManager userProfileManager;
42+
@Mock
43+
private IAvatarService avatarService;
44+
@Mock
45+
private IUserPreferencesManager userPreferencesManager;
46+
47+
@Test
48+
void testInjectProfile() throws EntException {
49+
IUserProfile returned = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE);
50+
when(userProfileManager.getProfile(Mockito.anyString())).thenReturn(returned);
51+
52+
User user = new User();
53+
user.setUsername("test");
54+
Assertions.assertNull(user.getProfile());
55+
56+
userManagementAspect.injectProfile(user);
57+
Mockito.verify(userProfileManager, Mockito.times(1)).getProfile("test");
58+
IUserProfile profile = (IUserProfile) user.getProfile();
59+
Assertions.assertNotNull(profile);
60+
Assertions.assertEquals("test", profile.getUsername());
61+
}
62+
63+
@Test
64+
void testInjectProfileWithError() throws EntException {
65+
when(userProfileManager.getProfile(Mockito.anyString())).thenThrow(EntException.class);
66+
67+
User user = new User();
68+
user.setUsername("test");
69+
Assertions.assertNull(user.getProfile());
70+
71+
userManagementAspect.injectProfile(user);
72+
Mockito.verify(userProfileManager, Mockito.times(1)).getProfile("test");
73+
Assertions.assertNull(user.getProfile());
74+
}
75+
76+
@Test
77+
void testInjectProfileWithNullUser() throws EntException {
78+
userManagementAspect.injectProfile(null);
79+
Mockito.verifyNoInteractions(userProfileManager);
80+
}
81+
82+
@Test
83+
void testMissingInjectProfile() throws EntException {
84+
IUserProfile profile = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE);
85+
User user = new User();
86+
user.setUsername("test");
87+
user.setProfile(profile);
88+
89+
userManagementAspect.injectProfile(user);
90+
Mockito.verify(userProfileManager, Mockito.times(0)).getProfile("test");
91+
Assertions.assertNotNull(user.getProfile());
92+
Assertions.assertSame(profile, user.getProfile());
93+
}
94+
95+
@Test
96+
void testAddProfile() throws EntException {
97+
IUserProfile profile = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE);
98+
User user = new User();
99+
user.setUsername("test");
100+
user.setProfile(profile);
101+
userManagementAspect.addProfile(user);
102+
Mockito.verify(userProfileManager, Mockito.times(1)).addProfile("test", profile);
103+
}
104+
105+
@Test
106+
void testAddProfileWithNullUser() throws EntException {
107+
userManagementAspect.addProfile(null);
108+
Mockito.verifyNoInteractions(userProfileManager);
109+
}
110+
111+
@Test
112+
void testInvokeAddProfileWithUserWithNullProfile() throws EntException {
113+
User user = new User();
114+
user.setUsername("test");
115+
userManagementAspect.addProfile(user);
116+
Mockito.verifyNoInteractions(userProfileManager);
117+
}
118+
119+
@Test
120+
void testInvokeAddProfileWithError() {
121+
try {
122+
Mockito.doThrow(EntException.class).when(userProfileManager).addProfile(Mockito.anyString(), Mockito.any());
123+
User user = new User();
124+
user.setUsername("test");
125+
user.setProfile(Mockito.mock(IUserProfile.class));
126+
userManagementAspect.addProfile(user);
127+
Mockito.verify(userProfileManager, Mockito.times(1)).addProfile(Mockito.anyString(), Mockito.any(IUserProfile.class));
128+
} catch (Exception e) {
129+
Assertions.fail();
130+
}
131+
}
132+
133+
@Test
134+
void testUpdateProfile() throws EntException {
135+
IUserProfile profile = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE);
136+
User user = new User();
137+
user.setUsername("test");
138+
user.setProfile(profile);
139+
userManagementAspect.updateProfile(user);
140+
Mockito.verify(userProfileManager, Mockito.times(1)).updateProfile("test", profile);
141+
}
142+
143+
@Test
144+
void testUpdateProfileWithNullUSer() throws EntException {
145+
userManagementAspect.updateProfile(null);
146+
Mockito.verifyNoInteractions(userProfileManager);
147+
}
148+
149+
@Test
150+
void testInvokeUpdateProfileWithUserWithNullProfile() throws EntException {
151+
User user = new User();
152+
user.setUsername("test");
153+
userManagementAspect.updateProfile(user);
154+
Mockito.verifyNoInteractions(userProfileManager);
155+
}
156+
157+
@Test
158+
void testInvokeUpdateProfileWithError() {
159+
try {
160+
Mockito.doThrow(EntException.class).when(userProfileManager).updateProfile(Mockito.anyString(), Mockito.any());
161+
User user = new User();
162+
user.setUsername("test");
163+
user.setProfile(Mockito.mock(IUserProfile.class));
164+
userManagementAspect.updateProfile(user);
165+
Mockito.verify(userProfileManager, Mockito.times(1)).updateProfile(Mockito.anyString(), Mockito.any(IUserProfile.class));
166+
} catch (Exception e) {
167+
Assertions.fail();
168+
}
169+
}
170+
171+
@Test
172+
void testDeleteUserDataFromUser() throws EntException {
173+
User user = new User();
174+
user.setUsername("test");
175+
userManagementAspect.deleteUserData(user);
176+
Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("test");
177+
Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("test");
178+
Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("test");
179+
}
180+
181+
@Test
182+
void testDeleteUserDataFromUsername() throws EntException {
183+
userManagementAspect.deleteUserData("test");
184+
Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("test");
185+
Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("test");
186+
Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("test");
187+
}
188+
189+
@Test
190+
void testDeleteUserDataWithNullUsername() throws EntException {
191+
userManagementAspect.deleteUserData(null);
192+
Mockito.verifyNoInteractions(userProfileManager);
193+
Mockito.verifyNoInteractions(userPreferencesManager);
194+
Mockito.verifyNoInteractions(avatarService);
195+
}
196+
197+
@Test
198+
void testDeleteUserDataWithErrorOnUserProfileManager() {
199+
try {
200+
Mockito.doThrow(EntException.class).when(userProfileManager).deleteProfile(Mockito.anyString());
201+
userManagementAspect.deleteUserData("username_error1");
202+
Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("username_error1");
203+
Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("username_error1");
204+
Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("username_error1");
205+
} catch (Exception e) {
206+
Assertions.fail();
207+
}
208+
}
209+
210+
@Test
211+
void testDeleteUserDataWithErrorOnUserAvatarService() {
212+
try {
213+
Mockito.doThrow(EntException.class).when(avatarService).deleteAvatar(Mockito.anyString());
214+
userManagementAspect.deleteUserData("username_error2");
215+
Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("username_error2");
216+
Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("username_error2");
217+
Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("username_error2");
218+
} catch (Exception e) {
219+
Assertions.fail();
220+
}
221+
}
222+
223+
@Test
224+
void testDeleteUserDataWithErrorOnUserPreference() {
225+
try {
226+
Mockito.doThrow(EntException.class).when(userPreferencesManager).deleteUserPreferences(Mockito.anyString());
227+
userManagementAspect.deleteUserData("username_error3");
228+
Mockito.verify(userProfileManager, Mockito.times(1)).deleteProfile("username_error3");
229+
Mockito.verify(userPreferencesManager, Mockito.times(1)).deleteUserPreferences("username_error3");
230+
Mockito.verify(avatarService, Mockito.times(1)).deleteAvatar("username_error3");
231+
} catch (Exception e) {
232+
Assertions.fail();
233+
}
234+
}
235+
236+
private IUserProfile createFakeProfile(String username, String defaultProfileTypeCode) {
237+
UserProfile userProfile = new UserProfile();
238+
userProfile.setId(username);
239+
MonoTextAttribute monoTextAttribute = new MonoTextAttribute();
240+
monoTextAttribute.setName("Name");
241+
monoTextAttribute.setHandler(new MonoTextAttributeHandler());
242+
userProfile.addAttribute(monoTextAttribute);
243+
userProfile.setTypeCode(defaultProfileTypeCode);
244+
return userProfile;
245+
}
246+
247+
}

0 commit comments

Comments
 (0)