BrightSide Workbench Full Report + Source Code
Permissions.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.security;
20 
21 import java.io.File;
22 import java.util.HashSet;
23 import java.util.Set;
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;
31 
36 public class Permissions {
37 
38  protected final TreeMap<String, RoleMap> roleMaps;
39 
40  public static FlatPermissions from(Set<String> securityGroups) {
41  FlatPermissions permissions = new FlatPermissions();
42  securityGroups.forEach(sg -> {
43  SecurityGroup securityGroup = SecurityGroups.get(sg);
44  flatten(securityGroup, permissions);
45  // Nested are done when SocialGroups.syndicate()
46 // securityGroup.getSyndicate().forEach(nested -> {
47 // flatten(SecurityGroups.get(nested), permissions);
48 // });
49  });
50  return permissions;
51  }
52 
53  private static void flatten(SecurityGroup securityGroup, FlatPermissions permissions) {
54  permissions.put("@" + securityGroup.getId(), ON_SET);
55  securityGroup.getRoles().forEach(role -> {
56  RoleMap rm = instance().roleMaps.get(role);
57  permissions.put("#" + role, ON_SET);
58  if(rm != null) {
59  permissions.addPermissions(rm.getRolePermissions());
60  }
61  });
62  }
63 
64  public static Set<String> getSecurityGroupIdsBy(String role) {
65  if(role.contains(":")) { // permission
66  Set<String> roleNames = getRoleNamesByPermission(role);
67  return SecurityGroups.getAll().stream()
68  .filter(sg -> sg.getRoles().stream().anyMatch(r -> roleNames.contains(r)))
69  .map(sg -> sg.getId()).collect(Collectors.toSet());
70  } else { // role name
71  return SecurityGroups.getAll().stream()
72  .filter(sg -> sg.getRoles().contains(role))
73  .map(sg -> sg.getId()).collect(Collectors.toSet());
74  }
75  }
76 
77  public static Set<String> getRoleNames() {
78  return instance().roleMaps.keySet();
79  }
80 
81  public static Set<String> getRoleNames(Set<String> securityGroups) {
82  return securityGroups.stream().flatMap(sg -> SecurityGroups.get(sg).getRoles().stream()).collect(Collectors.toSet());
83  }
84 
85  public static Set<String> getRoleNamesByPermission(String role) {
86  String parts[] = role.split(":");
87  Set<String> set = new HashSet<>();
88  instance().roleMaps.entrySet().forEach(rm -> {
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());
93  } else {
94  set.add(rm.getValue().getName());
95  }
96  }
97  });
98  });
99  return set;
100  }
101 
102  /* Utils */
103 
104  public static SortedMap<String, SortedSet<String>> permissionStringMap() {
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(", ")));
110  });
111  map.put(role.getKey(), set);
112  });
113  return map;
114  }
115 
116  public static TreeSet
117  ON_SET = new TreeSet<>(Set.of("on")),
118  IS_SET = new TreeSet<>(Set.of("is"));
119 
120  /* Factory */
121 
122  private static final Initializer<Permissions> INIT = new Initializer<>();
123 
124  public static Permissions instance() {
125  return INIT.instance(() -> new Permissions());
126  }
127 
128  public static void reset() {
129  INIT.reset();
130  }
131 
132  protected Permissions() {
133  roleMaps = new TreeMap<>();
134  loadMap();
135  }
136 
137  private static final String
138  ROLES_FOLDER = "/WEB-INF/elephant/roles";
139 
140  private void loadMap() {
141  File root = new File(ElephantContext.getRealPath(ROLES_FOLDER));
142  for(File f : root.listFiles()) {
143  RoleMap rm = RoleMap.loadFrom(f);
144  roleMaps.put(rm.getName(), rm);
145  }
146  }
147 
148 }
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)
Definition: RoleMap.java:61
TreeMap< String, TreeSet< String > > getRolePermissions()
Definition: RoleMap.java:51
static SecurityGroup get(String id)
static Collection< SecurityGroup > getAll()