19 package org.turro.security;
22 import java.util.HashSet;
24 import java.util.SortedMap;
25 import java.util.SortedSet;
26 import java.util.TreeMap;
27 import java.util.TreeSet;
28 import java.util.stream.Collectors;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.lock.Initializer;
38 protected final TreeMap<String, RoleMap>
roleMaps;
42 securityGroups.forEach(sg -> {
44 flatten(securityGroup, permissions);
54 permissions.put(
"@" + securityGroup.
getId(),
ON_SET);
55 securityGroup.
getRoles().forEach(role -> {
57 permissions.put(
"#" + role,
ON_SET);
65 if(role.contains(
":")) {
68 .filter(sg -> sg.getRoles().stream().anyMatch(r -> roleNames.contains(r)))
69 .map(sg -> sg.getId()).collect(Collectors.toSet());
72 .filter(sg -> sg.getRoles().contains(role))
73 .map(sg -> sg.getId()).collect(Collectors.toSet());
81 public static Set<String>
getRoleNames(Set<String> securityGroups) {
86 String parts[] = role.split(
":");
87 Set<String>
set =
new HashSet<>();
89 rm.getValue().getRolePermissions().entrySet().forEach(permission -> {
90 if(permission.getKey().equals(parts[0])) {
91 if(parts.length == 2 && permission.getValue().contains(parts[1])) {
92 set.add(rm.getValue().getName());
94 set.add(rm.getValue().getName());
105 SortedMap<String, SortedSet<String>> map =
new TreeMap<>();
106 instance().roleMaps.entrySet().forEach(role -> {
107 SortedSet<String>
set =
new TreeSet<>();
108 role.getValue().getRolePermissions().entrySet().forEach(permission -> {
109 set.add(permission.getKey() +
": " + permission.getValue().stream().collect(Collectors.joining(
", ")));
111 map.put(role.getKey(),
set);
116 public static TreeSet
117 ON_SET =
new TreeSet<>(Set.of(
"on")),
118 IS_SET =
new TreeSet<>(Set.of(
"is"));
122 private static final Initializer<Permissions> INIT =
new Initializer<>();
133 roleMaps =
new TreeMap<>();
137 private static final String
138 ROLES_FOLDER =
"/WEB-INF/elephant/roles";
140 private void loadMap() {
142 for(File f : root.listFiles()) {
144 roleMaps.put(rm.
getName(), rm);
static String getRealPath(String path)
void addPermissions(TreeMap< String, TreeSet< String >> rolePermissions)
static SortedMap< String, SortedSet< String > > permissionStringMap()
static Set< String > getRoleNames()
static Permissions instance()
final TreeMap< String, RoleMap > roleMaps
static FlatPermissions from(Set< String > securityGroups)
static Set< String > getRoleNames(Set< String > securityGroups)
static Set< String > getSecurityGroupIdsBy(String role)
static Set< String > getRoleNamesByPermission(String role)
static RoleMap loadFrom(File file)
TreeMap< String, TreeSet< String > > getRolePermissions()
static SecurityGroup get(String id)
static Collection< SecurityGroup > getAll()