BrightSide Workbench Full Report + Source Code
Actors.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.actor;
20 
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.Properties;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import java.util.stream.Collectors;
30 import org.turro.elephant.context.Application;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.entities.Entities;
33 import org.turro.entities.IElephantEntity;
34 import org.turro.file.FileWatch;
35 import org.turro.plugin.contacts.IContact;
36 import org.turro.reflection.Instances;
37 import org.turro.script.Scripting;
38 
43 public class Actors extends Properties {
44 
45  public static boolean isActor(String actor) {
46  return isActorFor((IContact) Application.getUser(), null, actor);
47  }
48 
49  public static boolean isActor(IContact contact, String actor) {
50  return isActorFor(contact, null, actor);
51  }
52 
53  public static boolean isActorFor(Object entity, String actor) {
54  return isActorFor((IContact) Application.getUser(), entity, actor);
55  }
56 
57  public static boolean isActorFor(IContact contact, Object entity, String actor) {
58  return isActorFor(contact, Entities.getController(entity), actor);
59  }
60 
61  public static boolean isActorFor(IElephantEntity iee, String actor) {
62  return isActorFor((IContact) Application.getUser(), iee, actor);
63  }
64 
65  public static boolean isActorFor(IContact contact, IElephantEntity iee, String actor) {
66  return Scripting.evalFor(contact, iee, getFormula(actor));
67  }
68 
69  /* Collections*/
70 
71  public static Set<String> getActors() {
72  return load().stringPropertyNames();
73  }
74 
75  public static List<String> getSortedActors() {
76  return getActors().stream().sorted().collect(Collectors.toList());
77  }
78 
79  /* Formulas */
80 
81  private static String getFormula(String actor) {
82  return load().getProperty(actor);
83  }
84 
85  /* Actor's context */
86 
87  private static final String ACTORS_FILE = "/WEB-INF/elephant/conf/actor-context.properties";
88 
89  private static Actors _actors;
90  private static long _lastLoad;
91 
92  private static Actors load() {
93  boolean exists = FileWatch.exists(ElephantContext.getRealPath(ACTORS_FILE));
94  if(_actors == null || FileWatch.isNewer(ElephantContext.getRealPath(ACTORS_FILE), _lastLoad) || !exists) {
95  if(exists) {
96  try(FileInputStream fis = new FileInputStream(ElephantContext.getRealPath(ACTORS_FILE))) {
97  _actors = new Actors();
98  addStandards();
99  _actors.load(fis);
100  } catch (IOException ex) {
101  Logger.getLogger(Actors.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
102  }
103  } else {
104  _actors = new Actors();
105  addStandards();
106  }
107  _lastLoad = FileWatch.getTime(ElephantContext.getRealPath(ACTORS_FILE));
108  }
109  return _actors;
110  }
111 
112  private static void store() {
113  try(FileOutputStream fos = new FileOutputStream(ElephantContext.getRealPath(ACTORS_FILE))) {
114  _actors.store(fos, null);
115  } catch (IOException ex) {
116  Logger.getLogger(Actors.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
117  }
118  }
119 
120  private static void addStandards() {
121  _actors.put("SysAdmin", "pfunc.anyOfSyndications(user, \"brightside_admin\")");
122  _actors.put("PartnerNetwork", "pfunc.anyOfSyndications(user, \"partner,patron,contacts_admin\")");
123  _actors.put("OnlyStudent", "user.student && !pfunc.anyOfSyndications(user, \"partner,patron\")");
124  Instances.cached().byAnnotation(ElephantActor.class, IElephantActor.class).forEach((iActor) -> {
125  iActor.addStandards(_actors);
126  });
127  }
128 
129 }
static boolean isActorFor(IElephantEntity iee, String actor)
Definition: Actors.java:61
static boolean isActor(IContact contact, String actor)
Definition: Actors.java:49
static boolean isActorFor(Object entity, String actor)
Definition: Actors.java:53
static boolean isActorFor(IContact contact, Object entity, String actor)
Definition: Actors.java:57
static Set< String > getActors()
Definition: Actors.java:71
static boolean isActor(String actor)
Definition: Actors.java:45
static boolean isActorFor(IContact contact, IElephantEntity iee, String actor)
Definition: Actors.java:65
static List< String > getSortedActors()
Definition: Actors.java:75
static IElephantEntity getController(String path)
Definition: Entities.java:78
static boolean evalFor(Object entity, String value)
Definition: Scripting.java:45