Add method all permissions
Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (c) 2011-2016 Bosch Software Innovations GmbH, Germany. All rights reserved.
|
||||
*/
|
||||
package org.eclipse.hawkbit.im.authentication;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
|
||||
/**
|
||||
* * Utility method for creation of <tt>GrantedAuthority</tt> collections etc.
|
||||
*/
|
||||
public final class PermissionUtils {
|
||||
|
||||
private PermissionUtils() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link GrantedAuthority} by a special role.
|
||||
*
|
||||
* @param roles
|
||||
* the roles
|
||||
* @return a list of {@link GrantedAuthority}
|
||||
*/
|
||||
public static List<GrantedAuthority> createAuthorityList(final Collection<String> roles) {
|
||||
final List<GrantedAuthority> authorities = new ArrayList<>(roles.size());
|
||||
|
||||
for (final String role : roles) {
|
||||
authorities.add(new SimpleGrantedAuthority(role));
|
||||
}
|
||||
|
||||
return authorities;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all authorities.
|
||||
*
|
||||
* @return a list of {@link GrantedAuthority}
|
||||
*/
|
||||
public static List<GrantedAuthority> createAllAuthorityList() {
|
||||
return createAuthorityList(SpPermission.getAllAuthorities());
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,14 @@
|
||||
package org.eclipse.hawkbit.im.authentication;
|
||||
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
|
||||
@@ -35,6 +42,8 @@ import org.springframework.security.core.GrantedAuthority;
|
||||
*/
|
||||
public final class SpPermission {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SpPermission.class);
|
||||
|
||||
/**
|
||||
* Permission to read the targets from the
|
||||
* {@link ProvisioningTargetRepository} including their meta information,
|
||||
@@ -139,6 +148,27 @@ public final class SpPermission {
|
||||
// Constants only
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all permission.
|
||||
*
|
||||
* @return all permission
|
||||
*/
|
||||
public static Collection<String> getAllAuthorities() {
|
||||
final List<String> allPermissions = new ArrayList<>();
|
||||
final Field[] declaredFields = SpPermission.class.getDeclaredFields();
|
||||
for (final Field field : declaredFields) {
|
||||
if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) {
|
||||
field.setAccessible(true);
|
||||
try {
|
||||
allPermissions.add((String) field.get(null));
|
||||
} catch (final IllegalAccessException e) {
|
||||
LOGGER.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return allPermissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains all the spring security evaluation expressions for the
|
||||
* {@link PreAuthorize} annotation for method security.
|
||||
|
||||
Reference in New Issue
Block a user