Skip to content

Commit f754faf

Browse files
committed
Conflict resolving during importing from LDAP
1 parent 18cdfcb commit f754faf

25 files changed

+629
-146
lines changed

aaa/src/main/java/name/nkonev/aaa/Constants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ public static class QueryVariables {
6464
public static final String BEHALF_USER_ID = "behalfUserId";
6565
}
6666

67+
public static final String LDAP_CONFLICT_PREFIX = "conflicts_with_ldap_";
68+
6769
public static final String DELETED = "deleted";
6870
public static final long DELETED_ID = -1;
6971

7072
public static final Long NonExistentUser = -65000L;
7173

7274
public static final Set<String> FORBIDDEN_USERNAMES = Set.of(DELETED, "all", "here");
75+
public static final Set<String> FORBIDDEN_USERNAME_PREFIXES = Set.of(LDAP_CONFLICT_PREFIX);
7376

7477
public static final int MIN_PASSWORD_LENGTH = 6;
7578
public static final int MAX_PASSWORD_LENGTH = 30;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package name.nkonev.aaa.config.properties;
2+
3+
public enum ConflictBy {
4+
USERNAME,
5+
EMAIL
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package name.nkonev.aaa.config.properties;
2+
3+
public enum ConflictResolveStrategy {
4+
IGNORE,
5+
WRITE_NEW_AND_REMOVE_OLD,
6+
WRITE_NEW_AND_RENAME_OLD
7+
}

aaa/src/main/java/name/nkonev/aaa/config/properties/LdapProperties.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public record LdapProperties(
44
LdapAuthProperties auth,
55
LdapAttributes attributeNames,
6-
LdapPasswordEncodingProperties password
6+
LdapPasswordEncodingProperties password,
7+
ConflictResolveStrategy resolveConflictsStrategy
78
) {
89
}

aaa/src/main/java/name/nkonev/aaa/converter/UserAccountConverter.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.stream.Collectors;
2222

2323
import static name.nkonev.aaa.Constants.FORBIDDEN_USERNAMES;
24+
import static name.nkonev.aaa.Constants.FORBIDDEN_USERNAME_PREFIXES;
2425
import static name.nkonev.aaa.utils.RoleUtils.DEFAULT_ROLE;
2526

2627
@Component
@@ -211,7 +212,8 @@ public static UserAccount buildUserAccountEntityForInsert(name.nkonev.aaa.dto.Ed
211212
null,
212213
null,
213214
null,
214-
userAccountDTO.loginColor()
215+
userAccountDTO.loginColor(),
216+
null
215217
);
216218
}
217219

@@ -234,14 +236,19 @@ public static void validateLengthEmail(String email) {
234236
private static String checkAndTrimLogin(String login, boolean isForOauth2) {
235237
login = login != null ? login.trim() : null;
236238
login = trimToNull(login);
239+
login = NullEncode.forHtml(login);
237240

238241
if (login != null) {
239242
if (FORBIDDEN_USERNAMES.contains(login)) {
240243
throw new BadRequestException("forbidden login");
241244
}
242-
}
243245

244-
login = NullEncode.forHtml(login);
246+
for (var fp : FORBIDDEN_USERNAME_PREFIXES) {
247+
if (login.startsWith(fp)) {
248+
throw new BadRequestException("forbidden login");
249+
}
250+
}
251+
}
245252

246253
if (login != null && !isForOauth2) {
247254
Assert.isTrue(!login.startsWith(FacebookOAuth2UserService.LOGIN_PREFIX), "not allowed prefix");
@@ -280,6 +287,7 @@ public static UserAccount buildUserAccountEntityForFacebookInsert(String faceboo
280287
null,
281288
null,
282289
null,
290+
null,
283291
null
284292
);
285293
}
@@ -310,6 +318,7 @@ public static UserAccount buildUserAccountEntityForVkontakteInsert(String vkonta
310318
null,
311319
null,
312320
null,
321+
null,
313322
null
314323
);
315324
}
@@ -340,6 +349,7 @@ public static UserAccount buildUserAccountEntityForGoogleInsert(String googleId,
340349
googleId,
341350
null,
342351
null,
352+
null,
343353
null
344354
);
345355
}
@@ -370,14 +380,13 @@ public static UserAccount buildUserAccountEntityForKeycloakInsert(String keycloa
370380
null,
371381
keycloakId,
372382
null,
383+
null,
373384
null
374385
);
375386
}
376387

377-
public static UserAccount buildUserAccountEntityForLdapInsert(String login, String ldapId, Set<UserRole> mappedRoles, String email) {
388+
public static UserAccount buildUserAccountEntityForLdapInsert(String login, String ldapId, Set<UserRole> mappedRoles, String email, boolean locked, boolean enabled, LocalDateTime syncLdapTime) {
378389
final boolean expired = false;
379-
final boolean locked = false;
380-
final boolean enabled = true;
381390
final boolean confirmed = true;
382391

383392
return new UserAccount(
@@ -400,7 +409,8 @@ public static UserAccount buildUserAccountEntityForLdapInsert(String login, Stri
400409
null,
401410
null,
402411
ldapId,
403-
null
412+
null,
413+
syncLdapTime
404414
);
405415
}
406416

aaa/src/main/java/name/nkonev/aaa/entity/jdbc/UserAccount.java

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public record UserAccount(
3030
String googleId,
3131
String keycloakId,
3232
String ldapId,
33-
String loginColor
33+
String loginColor,
34+
LocalDateTime syncLdapTime
3435
) {
3536

3637
public UserAccount withPassword(String newPassword) {
@@ -54,7 +55,8 @@ public UserAccount withPassword(String newPassword) {
5455
googleId,
5556
keycloakId,
5657
ldapId,
57-
loginColor
58+
loginColor,
59+
syncLdapTime
5860
);
5961
}
6062

@@ -79,7 +81,8 @@ public UserAccount withUsername(String newUsername) {
7981
googleId,
8082
keycloakId,
8183
ldapId,
82-
loginColor
84+
loginColor,
85+
syncLdapTime
8386
);
8487
}
8588

@@ -104,7 +107,8 @@ public UserAccount withAvatar(String newAvatar) {
104107
googleId,
105108
keycloakId,
106109
ldapId,
107-
loginColor
110+
loginColor,
111+
syncLdapTime
108112
);
109113
}
110114

@@ -129,7 +133,8 @@ public UserAccount withAvatarBig(String newAvatarBig) {
129133
googleId,
130134
keycloakId,
131135
ldapId,
132-
loginColor
136+
loginColor,
137+
syncLdapTime
133138
);
134139
}
135140

@@ -154,7 +159,8 @@ public UserAccount withEmail(String newEmailToSet) {
154159
googleId,
155160
keycloakId,
156161
ldapId,
157-
loginColor
162+
loginColor,
163+
syncLdapTime
158164
);
159165
}
160166

@@ -179,7 +185,8 @@ public UserAccount withShortInfo(String newShortInfo) {
179185
googleId,
180186
keycloakId,
181187
ldapId,
182-
loginColor
188+
loginColor,
189+
syncLdapTime
183190
);
184191
}
185192

@@ -204,7 +211,8 @@ public UserAccount withLocked(boolean newLocked) {
204211
googleId,
205212
keycloakId,
206213
ldapId,
207-
loginColor
214+
loginColor,
215+
syncLdapTime
208216
);
209217
}
210218

@@ -229,7 +237,8 @@ public UserAccount withEnabled(boolean newEnabled) {
229237
googleId,
230238
keycloakId,
231239
ldapId,
232-
loginColor
240+
loginColor,
241+
syncLdapTime
233242
);
234243
}
235244

@@ -254,7 +263,8 @@ public UserAccount withConfirmed(boolean newConfirmed) {
254263
googleId,
255264
keycloakId,
256265
ldapId,
257-
loginColor
266+
loginColor,
267+
syncLdapTime
258268
);
259269
}
260270

@@ -279,7 +289,8 @@ public UserAccount withRoles(UserRole[] newRoles) {
279289
googleId,
280290
keycloakId,
281291
ldapId,
282-
loginColor
292+
loginColor,
293+
syncLdapTime
283294
);
284295
}
285296

@@ -304,7 +315,34 @@ public UserAccount withLoginColor(String newLoginColor) {
304315
googleId,
305316
keycloakId,
306317
ldapId,
307-
newLoginColor
318+
newLoginColor,
319+
syncLdapTime
320+
);
321+
}
322+
323+
public UserAccount withSyncLdapTime(LocalDateTime newSyncLdapDateTime) {
324+
return new UserAccount(
325+
id,
326+
creationType,
327+
username,
328+
password,
329+
avatar,
330+
avatarBig,
331+
shortInfo,
332+
expired,
333+
locked,
334+
enabled,
335+
confirmed,
336+
roles,
337+
email,
338+
lastLoginDateTime,
339+
facebookId,
340+
vkontakteId,
341+
googleId,
342+
keycloakId,
343+
ldapId,
344+
loginColor,
345+
newSyncLdapDateTime
308346
);
309347
}
310348

@@ -329,7 +367,8 @@ public UserAccount withOauthIdentifiers(OAuth2Identifiers newOauthIdentifiers) {
329367
newOauthIdentifiers.googleId,
330368
newOauthIdentifiers.keycloakId,
331369
ldapId,
332-
loginColor
370+
loginColor,
371+
syncLdapTime
333372
);
334373
}
335374

aaa/src/main/java/name/nkonev/aaa/repository/jdbc/UserAccountRepository.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ public interface UserAccountRepository extends ListCrudRepository<UserAccount, L
4141
Set<Long> findUserIds(List<Long> userIds);
4242

4343
List<UserAccount> findByLdapIdInOrderById(Collection<String> strings);
44+
45+
@Modifying
46+
@Query("update user_account set sync_ldap_time = :newSyncLdapDateTime where ldap_id in (:ldapUserIds)")
47+
void updateSyncLdapTime(Set<String> ldapUserIds, LocalDateTime newSyncLdapDateTime);
48+
49+
@Modifying
50+
@Query("delete from user_account where ldap_id is not null and sync_ldap_time < :currTime")
51+
long deleteWithLdapIdElderThan(LocalDateTime currTime);
4452
}

0 commit comments

Comments
 (0)