BrightSide Workbench Full Report + Source Code
SecurityGroup.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.HashSet;
28 import java.util.Objects;
29 import java.util.Set;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import org.turro.string.Strings;
33 import org.turro.configuration.JsonConfiguration;
34 import org.turro.util.Comparison;
35 
40 public class SecurityGroup implements Comparable<SecurityGroup> {
41 
42  private String id, name;
43  private boolean inherits;
44  private SecurityGroupType type;
45  private final Set<String> syndicate = new HashSet<>();
46  private final Set<AllowedGroup> allowed = new HashSet<>();
47  private final Set<String> roles = new HashSet<>();
48  private final Set<String> tags = new HashSet<>();
49 
50  public String getId() {
51  return id;
52  }
53 
54  public void setId(String id) {
55  this.id = id;
56  }
57 
58  public String getName() {
59  return name;
60  }
61 
62  public void setName(String name) {
63  this.name = name;
64  }
65 
66  public boolean isInherits() {
67  return inherits;
68  }
69 
70  public void setInherits(boolean inherits) {
71  this.inherits = inherits;
72  }
73 
75  return type == null ? SecurityGroupType.SG_EXTERNAL : type;
76  }
77 
78  public void setType(SecurityGroupType type) {
79  this.type = type;
80  }
81 
82  public Set<String> getSyndicate() {
83  return syndicate;
84  }
85 
86  public Set<AllowedGroup> getAllowed() {
87  return allowed;
88  }
89 
90  public Set<String> getRoles() {
91  return roles;
92  }
93 
94  public Set<String> getTags() {
95  return tags;
96  }
97 
98  /* Configuration */
99 
100  private void prepareSaving() {
101  getSyndicate().removeIf(s -> Strings.isBlank(s));
102  getAllowed().removeIf(a -> Strings.isBlank(a.getId()));
103  }
104 
105  public static SecurityGroup loadFrom(File file) {
106  try(Reader reader = new FileReader(file)) {
107  return JsonConfiguration.read(reader, SecurityGroup.class);
108  } catch (IOException ex) {
109  Logger.getLogger(SecurityGroup.class.getName()).log(Level.SEVERE, null, ex);
110  return null;
111  }
112  }
113 
114  public static void saveTo(File file, SecurityGroup socialGroup) {
115  try(Writer writer = new FileWriter(file)) {
116  socialGroup.prepareSaving();
117  JsonConfiguration.write(writer, socialGroup);
118  } catch (IOException ex) {
119  Logger.getLogger(SecurityGroup.class.getName()).log(Level.SEVERE, null, ex);
120  }
121  }
122 
123  /* Standards */
124 
125  public static final String
126  ACCOUNT_AUXILIAR="account_auxiliar",
127  ADMIN_AUXILIAR="admin_auxiliar",
128  ADMINISTRATION="administration",
129  ATTACH_ADMIN="attach_admin",
130  BRIGHTSIDE_ADMIN="brightside_admin",
131  CLUSTER="cluster",
132  COLLABORATOR="collaborator",
133  COLLABORATOR_PRIVILEGED="collaborator_privileged",
134  COMERCIAL="comercial",
135  COMERCIAL_DIR_ASSISTANT="comercial_dir_assistant",
136  COMERCIAL_DIRECTION="comercial_direction",
137  COMPUTER_MANAGER="computer_manager",
138  CONTACTS_ADMIN="contacts_admin",
139  CONTENT_ADMIN="content_admin",
140  CONSULTANT="consultant",
141  COUNSELOR="counselor",
142  CUSTOMER="customer",
143  CUSTOMER_PRIVILEGED="customer_privileged",
144  DIRECTION="direction",
145  DIRECTION_ASSISTANT="direction_assistant",
146  DIRECTORY="directory",
147  DOSSIER_ADMIN="dossier_admin",
148  EMPLOYEE="employee",
149  EVENT_ASSISTANT="event_assistant",
150  FINANCIALS_ADMIN="financials_admin",
151  GROUPCOMPANY="groupcompany",
152  GUEST="guest",
153  INVESTOR="investor",
154  HUB="hub",
155  HUB_SUPPORT="hub-support",
156  MANAGER="manager",
157  MARKETING="marketing",
158  MARKETPLACE="marketplace",
159  OPERATOR="operator",
160  PARTNER="partner",
161  PATRON="patron",
162  PROVIDER="provider",
163  PROVIDER_PRIVILEGED="provider_privileged",
164  RESEARCHER="researcher",
165  STUDENT_ADMIN="student_admin",
166  TECHPARTNER="techpartner",
167  TESTIMONIAL="testimonial",
168  WEB_ADMIN="web_admin";
169 
170  /* Comparable */
171 
172  @Override
173  public int compareTo(SecurityGroup o) {
174  return Comparison.ascendant().compare(id, o.id).get();
175  }
176 
177  /* Utils */
178 
179  @Override
180  public int hashCode() {
181  int hash = 5;
182  hash = 37 * hash + Objects.hashCode(this.id);
183  return hash;
184  }
185 
186  @Override
187  public boolean equals(Object obj) {
188  if (this == obj) {
189  return true;
190  }
191  if (obj == null) {
192  return false;
193  }
194  if (getClass() != obj.getClass()) {
195  return false;
196  }
197  final SecurityGroup other = (SecurityGroup) obj;
198  return Objects.equals(this.id, other.id);
199  }
200 
201 }
void setType(SecurityGroupType type)
static void saveTo(File file, SecurityGroup socialGroup)
static final String ACCOUNT_AUXILIAR
void setInherits(boolean inherits)
Set< AllowedGroup > getAllowed()
static SecurityGroup loadFrom(File file)