Skip to content

Commit 885fe38

Browse files
fix(core): updated perunDefaultBlockedLoginChecker
Edited error message to contain list of user ids. Check usage of all default blocked logins at once.
1 parent cef237c commit 885fe38

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

perun-core/src/main/java/cz/metacentrum/perun/core/bl/AttributesManagerBl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,15 @@ public interface AttributesManagerBl {
432432
*/
433433
boolean isLoginAlreadyUsed(PerunSession sess, String login, String namespace);
434434

435+
/**
436+
* Gets IDs of users who use the given login in any namespace.
437+
*
438+
* @param sess perun session
439+
* @param login login
440+
* @return list of user IDs
441+
*/
442+
List<Integer> getUserIdsByLogin(PerunSession sess, String login);
443+
435444
/**
436445
* Get all attributes associated with the UserExtSource which have name in list attrNames (empty and virtual too).
437446
*

perun-core/src/main/java/cz/metacentrum/perun/core/blImpl/AttributesManagerBlImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,11 @@ public boolean isLoginAlreadyUsed(PerunSession sess, String login, String namesp
658658
return getAttributesManagerImpl().isLoginAlreadyUsed(sess, login, namespace);
659659
}
660660

661+
@Override
662+
public List<Integer> getUserIdsByLogin(PerunSession sess, String login) {
663+
return getAttributesManagerImpl().getUserIdsByLogin(sess, login);
664+
}
665+
661666
@Override
662667
public List<Attribute> getAttributes(PerunSession sess, Host host) {
663668
//get virtual attributes

perun-core/src/main/java/cz/metacentrum/perun/core/impl/AttributesManagerImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,16 @@ public boolean isLoginAlreadyUsed(PerunSession sess, String login, String namesp
12631263
}
12641264
}
12651265

1266-
1266+
public List<Integer> getUserIdsByLogin(PerunSession sess, String login) {
1267+
try {
1268+
return jdbc.queryForList("select distinct attr_val.user_id from attr_names as attr join user_attr_values attr_val on attr.id=attr_val.attr_id\n" +
1269+
"where attr.friendly_name like 'login-namespace:%%' \n" +
1270+
" and attr.friendly_name not like '%%persistent%%' \n" +
1271+
" and attr_val.attr_value like ?", Integer.class, login);
1272+
} catch (RuntimeException ex) {
1273+
throw new InternalErrorException(ex);
1274+
}
1275+
}
12671276

12681277
@Override
12691278
public List<Attribute> getAttributes(PerunSession sess, UserExtSource ues, List<String> attrNames) {

perun-core/src/main/java/cz/metacentrum/perun/core/impl/DefaultBlockedLoginChecker.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
1212

13+
import java.util.HashMap;
14+
import java.util.List;
1315
import java.util.Set;
1416

1517
/**
@@ -41,13 +43,21 @@ public void setPerunBl(PerunBl perunBl) {
4143
public void checkDefaultBlockedLogins() {
4244
log.debug("DefaultBlockedLoginChecker starts checking default blocked logins.");
4345

46+
HashMap<String, List<Integer>> usagesOfDefaultBlockedLogin = new HashMap<>();
47+
4448
Set<String> logins = BeansUtils.getCoreConfig().getBlockedLogins();
4549
for (String login : logins) {
46-
if (perunBl.getAttributesManagerBl().isLoginAlreadyUsed(sess, login, null)) {
50+
List<Integer> userIds = perunBl.getAttributesManagerBl().getUserIdsByLogin(sess, login);
51+
if (!userIds.isEmpty()) {
4752
log.error("Login {} can not be blocked by default because it is already used.", login);
48-
throw new InternalErrorException("Login " + login + " can not be blocked by default because it is already used. Please edit the core config!");
53+
usagesOfDefaultBlockedLogin.put(login, userIds);
4954
}
5055
}
56+
57+
if (!usagesOfDefaultBlockedLogin.isEmpty()) {
58+
throw new InternalErrorException("Default blocked logins in core config contain logins that users already use: " + usagesOfDefaultBlockedLogin);
59+
60+
}
5161
}
5262

5363
}

perun-core/src/main/java/cz/metacentrum/perun/core/implApi/AttributesManagerImplApi.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,15 @@ public interface AttributesManagerImplApi {
496496
*/
497497
boolean isLoginAlreadyUsed(PerunSession sess, String login, String namespace);
498498

499+
/**
500+
* Gets IDs of users who use the given login in any namespace.
501+
*
502+
* @param sess perun session
503+
* @param login login
504+
* @return list of user IDs
505+
*/
506+
List<Integer> getUserIdsByLogin(PerunSession sess, String login);
507+
499508
/**
500509
* Get all virtual attributes associated with the user.
501510
*

0 commit comments

Comments
 (0)