Skip to content

Commit

Permalink
Add support for collections and iterables to authZ generators (#761)
Browse files Browse the repository at this point in the history
* Add support for collections and iterables to authZ generators

* removed pmd issues

* Add support for collections and iterables to authZ generators
  • Loading branch information
mmoayyed authored and leleuj committed Dec 5, 2016
1 parent 3104ecb commit de29ae2
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 82 deletions.
Expand Up @@ -3,7 +3,7 @@
import org.pac4j.core.profile.CommonProfile;

import java.util.Arrays;
import java.util.List;
import java.util.Collection;

/**
* Grant default roles and/or permissions to a user profile.
Expand All @@ -13,11 +13,11 @@
*/
public class DefaultRolesPermissionsAuthorizationGenerator<U extends CommonProfile> implements AuthorizationGenerator<U> {

private final List<String> defaultRoles;
private final Collection<String> defaultRoles;

private final List<String> defaultPermissions;
private final Collection<String> defaultPermissions;

public DefaultRolesPermissionsAuthorizationGenerator(final List<String> defaultRoles, final List<String> defaultPermissions) {
public DefaultRolesPermissionsAuthorizationGenerator(final Collection<String> defaultRoles, final Collection<String> defaultPermissions) {
this.defaultRoles = defaultRoles;
this.defaultPermissions = defaultPermissions;
}
Expand Down
@@ -1,72 +1,80 @@
package org.pac4j.core.authorization.generator;

import org.pac4j.core.profile.CommonProfile;

import java.util.Arrays;
import java.util.Collection;
import java.util.StringTokenizer;

import org.pac4j.core.profile.CommonProfile;

/**
* <p>Generate the authorization information by inspecting attributes.</p>
* <p>The attributes containing the roles separated by the {@link #splitChar} property (can be set through {@link #setSplitChar(String)}) are
* defined in the constructor. It's the same for the attributes containing the permissions.</p>
*
*
* @author Jerome Leleu
* @since 1.5.0
*/
public class FromAttributesAuthorizationGenerator<U extends CommonProfile> implements AuthorizationGenerator<U> {
private final String[] roleAttributes;
private final String[] permissionAttributes;

private final Collection<String> roleAttributes;

private final Collection<String> permissionAttributes;

private String splitChar = ",";
public FromAttributesAuthorizationGenerator(final String[] roleAttributes, final String[] permissionAttributes) {
this.roleAttributes = copyArray(roleAttributes);
this.permissionAttributes = copyArray(permissionAttributes);

public FromAttributesAuthorizationGenerator(final Collection<String> roleAttributes, final Collection<String> permissionAttributes) {
this.roleAttributes = roleAttributes;
this.permissionAttributes = permissionAttributes;
}

private String[] copyArray(final String[] original) {
if (original != null) {
return Arrays.copyOf(original, original.length);
public FromAttributesAuthorizationGenerator(final String[] roleAttributes, final String[] permissionAttributes) {
if (roleAttributes != null) {
this.roleAttributes = Arrays.asList(roleAttributes);
} else {
return null;
this.roleAttributes = null;
}
if (permissionAttributes != null) {
this.permissionAttributes = Arrays.asList(permissionAttributes);
} else {
this.permissionAttributes = null;
}
}

public void generate(final U profile) {
generateAuth(profile, this.roleAttributes, true);
generateAuth(profile, this.permissionAttributes, false);
}

private void generateAuth(final U profile, final String[] attributes, final boolean isRole) {
if (attributes != null) {
for (final String attribute : attributes) {
final Object value = profile.getAttribute(attribute);
if (value != null) {
if(value instanceof String) {
final StringTokenizer st = new StringTokenizer((String) value, this.splitChar);
while (st.hasMoreTokens()) {
setAuth(profile, st.nextToken(), isRole);
}
} else if(value.getClass().isArray() && value.getClass().getComponentType().isAssignableFrom(String.class)) {
for(Object item : (Object[])value) {
setAuth(profile, item.toString(), isRole);
}
} else if(Collection.class.isAssignableFrom(value.getClass())) {
for(Object item : (Collection<?>)value) {
if(item.getClass().isAssignableFrom(String.class)) {
setAuth(profile, item.toString(), isRole);
}

private void generateAuth(final U profile, final Iterable<String> attributes, final boolean isRole) {
if (attributes == null) {
return;
}

for (final String attribute : attributes) {
final Object value = profile.getAttribute(attribute);
if (value != null) {
if (value instanceof String) {
final StringTokenizer st = new StringTokenizer((String) value, this.splitChar);
while (st.hasMoreTokens()) {
addRoleOrPermissionToProfile(profile, st.nextToken(), isRole);
}
} else if (value.getClass().isArray() && value.getClass().getComponentType().isAssignableFrom(String.class)) {
for (Object item : (Object[]) value) {
addRoleOrPermissionToProfile(profile, item.toString(), isRole);
}
} else if (Collection.class.isAssignableFrom(value.getClass())) {
for (Object item : (Collection<?>) value) {
if (item.getClass().isAssignableFrom(String.class)) {
addRoleOrPermissionToProfile(profile, item.toString(), isRole);
}
}
}
}
}

}

private void setAuth(final U profile, final String value, final boolean isRole) {
private void addRoleOrPermissionToProfile(final U profile, final String value, final boolean isRole) {
if (isRole) {
profile.addRole(value);
} else {
Expand All @@ -77,7 +85,7 @@ private void setAuth(final U profile, final String value, final boolean isRole)
public String getSplitChar() {
return this.splitChar;
}

public void setSplitChar(final String splitChar) {
this.splitChar = splitChar;
}
Expand Down
47 changes: 18 additions & 29 deletions pac4j-core/src/main/java/org/pac4j/core/profile/UserProfile.java
Expand Up @@ -14,7 +14,7 @@
/**
* This class is the user profile retrieved from a provider after successful authentication: it's an identifier (string) and attributes
* (objects). Additional concepts are the "remember me" nature of the user profile and the associated roles, permissions and client name.
*
*
* @author Jerome Leleu
* @since 1.0.0
*/
Expand All @@ -40,7 +40,7 @@ public abstract class UserProfile implements Serializable, Externalizable {

/**
* Build a profile from user identifier and attributes.
*
*
* @param id user identifier
* @param attributes user attributes
*/
Expand All @@ -51,7 +51,7 @@ public void build(final Object id, final Map<String, Object> attributes) {

/**
* Add an attribute.
*
*
* @param key key of the attribute
* @param value value of the attribute
*/
Expand All @@ -64,7 +64,7 @@ public void addAttribute(final String key, Object value) {

/**
* Add attributes.
*
*
* @param attributes use attributes
*/
public void addAttributes(final Map<String, Object> attributes) {
Expand All @@ -87,7 +87,7 @@ public void removeAttribute(final String key) {

/**
* Set the identifier and convert it if necessary.
*
*
* @param id user identifier
*/
public void setId(final Object id) {
Expand All @@ -104,7 +104,7 @@ public void setId(final Object id) {

/**
* Get the user identifier. This identifier is unique for this provider but not necessarily through all providers.
*
*
* @return the user identifier
*/
public String getId() {
Expand All @@ -114,7 +114,7 @@ public String getId() {
/**
* Get the user identifier with a prefix which is the profile type (full class name with package).
* This identifier is unique through all providers.
*
*
* @return the typed user identifier
*/
public String getTypedId() {
Expand All @@ -123,7 +123,7 @@ public String getTypedId() {

/**
* Get all attributes as immutable map.
*
*
* @return the immutable attributes
*/
public Map<String, Object> getAttributes() {
Expand All @@ -138,7 +138,7 @@ public Map<String, Object> getAttributes() {

/**
* Return the attribute with name.
*
*
* @param name attribute name
* @return the attribute with name
*/
Expand Down Expand Up @@ -184,7 +184,7 @@ public <T> T getAttribute(final String name, final Class<T> clazz) {

/**
* Add a role.
*
*
* @param role the role to add.
*/
public void addRole(final String role) {
Expand All @@ -197,7 +197,7 @@ public void addRole(final String role) {
*
* @param roles the roles to add.
*/
public void addRoles(final List<String> roles) {
public void addRoles(final Collection<String> roles) {
CommonHelper.assertNotNull("roles", roles);
this.roles.addAll(roles);
}
Expand All @@ -214,37 +214,26 @@ public void addRoles(final Set<String> roles) {

/**
* Add a permission.
*
*
* @param permission the permission to add.
*/
public void addPermission(final String permission) {
CommonHelper.assertNotBlank("permission", permission);
this.permissions.add(permission);
}

/**
* Add permissions.
/** Add permissions.
*
* @param permissions the permissions to add.
*/
public void addPermissions(final List<String> permissions) {
CommonHelper.assertNotNull("permissions", permissions);
this.permissions.addAll(permissions);
}

/**
* Add permissions.
*
* @param permissions the permissions to add.
*/
public void addPermissions(final Set<String> permissions) {
public void addPermissions(final Collection<String> permissions) {
CommonHelper.assertNotNull("permissions", permissions);
this.permissions.addAll(permissions);
}

/**
* Define if this profile is remembered.
*
*
* @param rme whether the user is remembered.
*/
public void setRemembered(final boolean rme) {
Expand All @@ -253,7 +242,7 @@ public void setRemembered(final boolean rme) {

/**
* Get the roles of the user.
*
*
* @return the user roles.
*/
public Set<String> getRoles() {
Expand All @@ -262,7 +251,7 @@ public Set<String> getRoles() {

/**
* Get the permissions of the user.
*
*
* @return the user permissions.
*/
public Set<String> getPermissions() {
Expand All @@ -271,7 +260,7 @@ public Set<String> getPermissions() {

/**
* Is the user remembered?
*
*
* @return whether the user is remembered.
*/
public boolean isRemembered() {
Expand Down
@@ -1,13 +1,15 @@
package org.pac4j.core.authorization.generator;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;
import org.pac4j.core.profile.CommonProfile;


import static org.junit.Assert.*;

/**
Expand All @@ -17,16 +19,16 @@
* @since 1.5.0
*/
public final class FromAttributesAuthorizationGeneratorTests {
private final static String ATTRIB1 = "attrib1";
private final static String VALUE1 = "info11,info12";
private final static String ATTRIB2 = "attrib2";
private final static String VALUE2 = "info21,info22";
private final static String ATTRIB3 = "attrib3";
private final static String ATTRIB4 = "attrib4";
private final static String ATTRIB5 = "attrib5";
private final static String[] ATTRIB_ARRAY = new String[]{"infoA1", "infoA2", "infoA3"};
private final static List<String> ATTRIB_LIST = new ArrayList<>();

private static final String ATTRIB1 = "attrib1";
private static final String VALUE1 = "info11,info12";
private static final String ATTRIB2 = "attrib2";
private static final String VALUE2 = "info21,info22";
private static final String ATTRIB3 = "attrib3";
private static final String ATTRIB4 = "attrib4";
private static final String ATTRIB5 = "attrib5";
private static final String[] ATTRIB_ARRAY = new String[]{"infoA1", "infoA2", "infoA3"};
private static final List<String> ATTRIB_LIST = new ArrayList<>();

static {
ATTRIB_LIST.add("infoL1");
Expand All @@ -45,9 +47,19 @@ public void setUp() {
this.profile.addAttribute(ATTRIB4, ATTRIB_LIST);
}

@Test
public void testNoConfigWithCollections() {
final FromAttributesAuthorizationGenerator<CommonProfile> generator =
new FromAttributesAuthorizationGenerator<>(new ArrayList<>(), new HashSet<>());
generator.generate(this.profile);
assertEquals(0, this.profile.getRoles().size());
assertEquals(0, this.profile.getPermissions().size());
}

@Test
public void testNoConfig() {
final FromAttributesAuthorizationGenerator<CommonProfile> generator = new FromAttributesAuthorizationGenerator<>(null, null);
final FromAttributesAuthorizationGenerator<CommonProfile> generator =
new FromAttributesAuthorizationGenerator<>((String[]) null, (String[]) null);
generator.generate(this.profile);
assertEquals(0, this.profile.getRoles().size());
assertEquals(0, this.profile.getPermissions().size());
Expand Down

0 comments on commit de29ae2

Please sign in to comment.