BrightSide Workbench Full Report + Source Code
ConceptPermission.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.util.Collection;
22 import org.turro.string.Strings;
23 import org.turro.action.Contacts;
24 import org.turro.actor.Actors;
25 import org.turro.entities.IElephantEntity;
26 import org.turro.plugin.contacts.IContact;
27 import org.turro.script.Scripting;
28 import org.turro.util.Cached;
29 
34 public class ConceptPermission {
35 
36  public static final String
37  DRIVER = "driver",
38  COORDINATOR = "coordinator",
39  BENEFICIARY = "beneficiary",
40  OFFERER = "offerer",
41  RESEARCH = "research",
42  FUNDING = "funding",
43  SUPPORT = "support",
44  CONSORTIUM = "consortium",
45  ADMIN = "admin",
46  TAGS = "tags",
47  PROPOSALS = "proposals",
48  MILESTONES = "milestones",
49  DEBATES = "debates",
50  CALLS = "calls",
51  VOTES = "votes",
52  DELIVERABLES = "deliverables";
53 
54  private final transient String name;
55  private final boolean allow;
56 
57  private boolean show = true;
58  private String showRole, actionRole;
59  private String showScript, actionScript;
60  private String showActor, actionActor;
61  private Integer max, order;
62 
63  private final ConceptPermissionMap permissions = new ConceptPermissionMap();
64 
66  this.show = true;
67  return this;
68  }
69 
71  this.show = false;
72  return this;
73  }
74 
75  public ConceptPermission max(Integer max) {
76  this.max = max;
77  return this;
78  }
79 
80  public ConceptPermission order(Integer order) {
81  this.order = order;
82  return this;
83  }
84 
85  public ConceptPermission showRole(String role) {
86  this.showRole = role;
87  return this;
88  }
89 
90  public ConceptPermission actionRole(String role) {
91  this.actionRole = role;
92  return this;
93  }
94 
95  public ConceptPermission showScript(String script) {
96  this.showScript = script;
97  return this;
98  }
99 
100  public ConceptPermission actionScript(String script) {
101  this.actionScript = script;
102  return this;
103  }
104 
105  public ConceptPermission showActor(String actor) {
106  this.showActor = actor;
107  return this;
108  }
109 
110  public ConceptPermission actionActor(String actor) {
111  this.actionActor = actor;
112  return this;
113  }
114 
116  this.permissions.put(permission);
117  return this;
118  }
119 
120  public String getName() {
121  return name;
122  }
123 
124  public boolean isAllow() {
125  return allow;
126  }
127 
128  public boolean isShow() {
129  return show;
130  }
131 
132  public Integer getMax() {
133  return max;
134  }
135 
136  public Integer getOrder() {
137  return order;
138  }
139 
140  public ConceptPermission getPermission(String name) {
141  return permissions.get(name, iee);
142  }
143 
144  public Collection<String> getConcepts() {
145  return permissions.concepts();
146  }
147 
148  /* Checks */
149 
150  public boolean canShow() {
151  return canShow(contact.get());
152  }
153 
154  public boolean canShow(IContact contact) {
155  return isShow() && Contacts.isValid(contact) &&
156  (Strings.isBlank(showRole) || contact.isInRole(showRole)) &&
157  (Strings.isBlank(showScript) || processScript(showScript, iee)) &&
158  (Strings.isBlank(showActor) || processActor(showActor, iee));
159  }
160 
161  public boolean canAct() {
162  return canAct(contact.get());
163  }
164 
165  public boolean canAct(IContact contact) {
166  return isAllow() && Contacts.isValid(contact) &&
167  (Strings.isBlank(actionRole) || contact.isInRole(actionRole)) &&
168  (Strings.isBlank(actionScript) || processScript(actionScript, iee)) &&
169  (Strings.isBlank(actionActor) || processActor(actionActor, iee));
170  }
171 
172  /* Scripting */
173 
174  private boolean processScript(String value, IElephantEntity entity) {
175  return Scripting.evalFor(contact.get(), entity, value);
176  }
177 
178  private boolean processActor(String actor, IElephantEntity entity) {
179  return Actors.isActorFor(contact.get(), entity, actor);
180  }
181 
182  /* Initializer */
183 
184  private transient Cached<IContact> contact;
185  private transient IElephantEntity iee;
186 
187  public void init(IElephantEntity iee, Cached<IContact> contact) {
188  this.iee = iee;
189  this.contact = contact;
190  }
191 
192  /* Factory */
193 
194  public static ConceptPermission allow(String name) {
195  return new ConceptPermission(name, true);
196  }
197 
198  public static ConceptPermission forbid(String name) {
199  return new ConceptPermission(name, false).hide();
200  }
201 
202  private ConceptPermission(String name, boolean allow) {
203  this.name = name;
204  this.allow = allow;
205  this.order = -1;
206  }
207 
208 }
static boolean isValid(IContact contact)
Definition: Contacts.java:52
static boolean evalFor(Object entity, String value)
Definition: Scripting.java:45
ConceptPermission put(String key, ConceptPermission value)
ConceptPermission max(Integer max)
ConceptPermission showRole(String role)
ConceptPermission actionRole(String role)
ConceptPermission showScript(String script)
ConceptPermission actionActor(String actor)
ConceptPermission getPermission(String name)
ConceptPermission actionScript(String script)
ConceptPermission addPermission(ConceptPermission permission)
ConceptPermission order(Integer order)
static ConceptPermission allow(String name)
static ConceptPermission forbid(String name)
ConceptPermission showActor(String actor)
void init(IElephantEntity iee, Cached< IContact > contact)
boolean isInRole(String role)