attach userprofile to session

This commit is contained in:
Jörg Prante 2024-04-17 18:41:55 +02:00
parent 3bf46413b1
commit e444bfca5a
5 changed files with 39 additions and 33 deletions

View file

@ -27,8 +27,6 @@ public class BaseUserProfile implements UserProfile {
private boolean isRemembered;
private boolean isLoggedIn;
public BaseUserProfile() {
this.attributes = new BaseAttributes();
this.effectiveAttributes = new BaseAttributes();
@ -38,15 +36,9 @@ public class BaseUserProfile implements UserProfile {
this.effectivePermissions = new ArrayList<>();
}
@Override
public boolean isLoggedIn() {
return isLoggedIn;
}
@Override
public void setUserId(String uid) {
this.uid = uid;
this.isLoggedIn = uid != null;
}
@Override
@ -94,17 +86,14 @@ public class BaseUserProfile implements UserProfile {
return effectiveRoles;
}
@Override
public boolean hasRole(String role) {
return roles.contains(role);
}
@Override
public boolean hasEffectiveRole(String role) {
return effectiveRoles.contains(role);
}
@Override
public boolean hasAccess(String requireAnyRole, String requireAllRoles) {
boolean access = true;
if (!requireAnyRole.isEmpty()) {
@ -121,7 +110,6 @@ public class BaseUserProfile implements UserProfile {
return access;
}
@Override
public boolean hasAnyRole(String[] expectedRoles) {
if (expectedRoles == null || expectedRoles.length == 0) {
return true;
@ -134,7 +122,6 @@ public class BaseUserProfile implements UserProfile {
return false;
}
@Override
public boolean hasAnyEffectiveRole(String[] expectedRoles) {
if (expectedRoles == null || expectedRoles.length == 0) {
return true;
@ -147,9 +134,8 @@ public class BaseUserProfile implements UserProfile {
return false;
}
@Override
public boolean hasAllRoles(String[] expectedRoles) {
if (expectedRoles == null || expectedRoles.length == 0) {
if (expectedRoles == null) {
return true;
}
for (String role : expectedRoles) {
@ -160,9 +146,8 @@ public class BaseUserProfile implements UserProfile {
return true;
}
@Override
public boolean hasAllEffectiveRoles(String[] expectedRoles) {
if (expectedRoles == null || expectedRoles.length == 0) {
if (expectedRoles == null) {
return true;
}
for (String role : expectedRoles) {
@ -178,7 +163,6 @@ public class BaseUserProfile implements UserProfile {
permissions.add(permission);
}
@Override
public void removePermission(String permission) {
permissions.remove(permission);
}
@ -189,12 +173,12 @@ public class BaseUserProfile implements UserProfile {
}
@Override
public Attributes attributes() {
public Attributes getAttributes() {
return attributes;
}
@Override
public Attributes effectiveAttributes() {
public Attributes getEffectiveAttributes() {
return effectiveAttributes;
}
@ -203,6 +187,10 @@ public class BaseUserProfile implements UserProfile {
return permissions;
}
@Override
public void addEffectivePermission(String permission) {
effectivePermissions.add(permission);
}
@Override
public List<String> getEffectivePermissions() {
return effectivePermissions;
@ -215,15 +203,13 @@ public class BaseUserProfile implements UserProfile {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("uid=").append(uid)
.append(",roles=").append(roles)
.append(",permissons=").append(permissions)
.append(",attributes=").append(attributes)
.append(",euid=").append(euid)
.append(",eroles=").append(effectiveRoles)
.append(",epermissions=").append(effectivePermissions)
.append(",eattributes=").append(effectiveAttributes);
return sb.toString();
return "uid=" + uid +
",roles=" + roles +
",permissons=" + permissions +
",attributes=" + attributes +
",euid=" + euid +
",eroles=" + effectiveRoles +
",epermissions=" + effectivePermissions +
",eattributes=" + effectiveAttributes;
}
}

View file

@ -6,7 +6,7 @@ import org.xbib.net.GroupsProvider;
import org.xbib.net.SecurityRealm;
import org.xbib.net.UsersProvider;
public class LdapRealm extends SecurityRealm {
public class LdapRealm implements SecurityRealm {
private final String name;
@ -24,9 +24,9 @@ public class LdapRealm extends SecurityRealm {
Map<String, LdapGroupMapping> groupMappings) {
this.name = name;
this.contextFactories = contextFactories;
this.authenticator = new LdapAuthenticator(contextFactories, userMappings);
this.usersProvider = new LdapUsersProvider(contextFactories, userMappings);
this.groupsProvider = new LdapGroupsProvider(contextFactories, userMappings, groupMappings);
this.authenticator = new LdapAuthenticator(contextFactories, userMappings);
}
@Override

View file

@ -7,6 +7,7 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.xbib.datastructures.common.LRUCache;
import org.xbib.net.UserProfile;
public class BaseSession implements Session {
@ -30,6 +31,10 @@ public class BaseSession implements Session {
private boolean valid;
private boolean authenticated;
private UserProfile userProfile;
public BaseSession(SessionListener sessionListener,
int cacheSize,
String name,
@ -90,6 +95,19 @@ public class BaseSession implements Session {
return Duration.between(lastModified, now).compareTo(lifetime) > 0;
}
public void setUserProfile(UserProfile userProfile) {
this.userProfile = userProfile;
}
public UserProfile getUserProfile() {
return userProfile;
}
@Override
public boolean isAuthenticated() {
return userProfile != null && userProfile.getUserId() != null;
}
@Override
public boolean hasPayload() {
return !isEmpty() &&

View file

@ -15,6 +15,8 @@ public interface Session extends Map<String, Object> {
boolean isExpired();
boolean isAuthenticated();
boolean hasPayload();
Duration getAge();

View file

@ -6,7 +6,7 @@ dependencyResolutionManagement {
version('netty', '4.1.109.Final')
version('netty-tcnative', '2.0.65.Final')
version('datastructures', '5.0.7')
version('net', '4.3.0')
version('net', '4.4.0')
library('netty-codec-http2', 'io.netty', 'netty-codec-http2').versionRef('netty')
library('netty-handler', 'io.netty', 'netty-handler').versionRef('netty')
library('netty-handler-proxy', 'io.netty', 'netty-handler-proxy').versionRef('netty')