BrightSide Workbench Full Report + Source Code
SecurityGroups.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.nio.file.Path;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Set;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.lock.Initializer;
29 
34 public class SecurityGroups {
35 
36  protected final Map<String, SecurityGroup> socialGroups;
37 
38  public static SecurityGroup get(String id) {
39  return instance().getSocialGroup(id);
40  }
41 
42  public static Collection<SecurityGroup> getAll() {
43  return instance().socialGroups.values();
44  }
45 
46  /* Utils */
47 
48  public SecurityGroup getSocialGroup(String id) {
49  return socialGroups.get(id);
50  }
51 
52  protected void recurseSocialGroups(Set<String> flatSocials, SecurityGroup sg) {
53  if(sg != null) {
54  flatSocials.add(sg.getId());
55  sg.getSyndicate().forEach(social -> {
56  recurseSocialGroups(flatSocials, socialGroups.get(social));
57  });
58  }
59  }
60 
61  protected boolean possibleTag(String tag) {
62  return socialGroups.values().stream().anyMatch(social -> social.getTags().contains(tag));
63  }
64 
65  /* Factory */
66 
67  private static final Initializer<SecurityGroups> INIT = new Initializer<>();
68 
69  public static SecurityGroups instance() {
70  return INIT.instance(() -> new SecurityGroups());
71  }
72 
73  public static void reset() {
74  INIT.reset();
75  }
76 
77  protected SecurityGroups() {
78  socialGroups = new HashMap<>();
79  loadMap();
80  }
81 
82  private static final String
83  SOCIAL_GROUP_FOLDER = "/WEB-INF/elephant/socials";
84 
85  private void loadMap() {
86  File root = new File(ElephantContext.getRealPath(SOCIAL_GROUP_FOLDER));
87  if(root.exists()) {
88  for(File f : root.listFiles()) {
90  socialGroups.put(sg.getId(), sg);
91  }
92  }
93  }
94 
95  private void saveSet() {
96  socialGroups.entrySet().forEach(entry -> {
97  File file = Path.of(ElephantContext.getRealPath(SOCIAL_GROUP_FOLDER), entry.getKey() + ".el").toFile();
98  SecurityGroup.saveTo(file, entry.getValue());
99  });
100  }
101 
102 }
static SecurityGroup loadFrom(File file)
static SecurityGroups instance()
final Map< String, SecurityGroup > socialGroups
SecurityGroup getSocialGroup(String id)
static Collection< SecurityGroup > getAll()
void recurseSocialGroups(Set< String > flatSocials, SecurityGroup sg)