BrightSide Workbench Full Report + Source Code
RoleMap.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.io.FileReader;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.io.Writer;
27 import java.util.TreeMap;
28 import java.util.TreeSet;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import org.turro.string.Strings;
32 import org.turro.configuration.JsonConfiguration;
33 
38 public class RoleMap {
39 
40  private String name;
41  private final TreeMap<String, TreeSet<String>> rolePermissions = new TreeMap<>();
42 
43  public void setName(String name) {
44  this.name = name;
45  }
46 
47  public String getName() {
48  return name;
49  }
50 
51  public TreeMap<String, TreeSet<String>> getRolePermissions() {
52  return rolePermissions;
53  }
54 
55  /* Configuration */
56 
57  private void prepareSaving() {
58  getRolePermissions().entrySet().removeIf(entry -> Strings.isBlank(entry.getKey()));
59  }
60 
61  public static RoleMap loadFrom(File file) {
62  try(Reader reader = new FileReader(file)) {
63  return JsonConfiguration.read(reader, RoleMap.class);
64  } catch (IOException ex) {
65  Logger.getLogger(RoleMap.class.getName()).log(Level.SEVERE, null, ex);
66  return null;
67  }
68  }
69 
70  public static void saveTo(File file, RoleMap roleMap) {
71  try(Writer writer = new FileWriter(file)) {
72  roleMap.prepareSaving();
73  JsonConfiguration.write(writer, roleMap);
74  } catch (IOException ex) {
75  Logger.getLogger(RoleMap.class.getName()).log(Level.SEVERE, null, ex);
76  }
77  }
78 
79 }
static RoleMap loadFrom(File file)
Definition: RoleMap.java:61
void setName(String name)
Definition: RoleMap.java:43
static void saveTo(File file, RoleMap roleMap)
Definition: RoleMap.java:70
TreeMap< String, TreeSet< String > > getRolePermissions()
Definition: RoleMap.java:51