BrightSide Workbench Full Report + Source Code
Permission.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.function.Supplier;
22 import org.turro.elephant.context.Application;
23 import org.turro.plugin.contacts.IContact;
24 
29 public class Permission {
30 
31  private final IContact contact;
32  private final String[] roles;
33  private final Supplier<Boolean> supplier;
34 
35  private Permission(IContact contact, String... roles) {
36  this.contact = contact;
37  this.roles = roles;
38  this.supplier = null;
39  }
40 
41  private Permission(IContact contact, Supplier<Boolean> supplier) {
42  this.contact = contact;
43  this.supplier = supplier;
44  this.roles = null;
45  }
46 
47  public boolean check() {
48  if(roles != null) {
49  for(String role : roles) {
50  if(role.contains(":")) {
51  if(isInRole(role)) {
52  return true;
53  }
54  } else {
55  if(hasAnyRoleKey(role)) {
56  return true;
57  }
58  }
59  }
60  }
61  if(supplier != null) {
62  return supplier.get();
63  }
64  return false;
65  }
66 
67  private boolean isInRole(String role) {
68  return contact != null || contact.isValid() ?
69  contact.isInRole(role) : false;
70  }
71 
72  private boolean hasAnyRoleKey(String role) {
73  return contact != null || contact.isValid() ?
74  contact.hasAnyRoleKey(role) : false;
75  }
76 
77  /* Factory */
78 
79  public static Permission from(String... roles) {
80  return new Permission(Application.getUser(), roles);
81  }
82 
83  public static Permission from(Supplier<Boolean> supplier) {
84  return new Permission(Application.getUser(), supplier);
85  }
86 
87  public static Permission from(IContact contact, String... roles) {
88  return new Permission(contact, roles);
89  }
90 
91  public static Permission from(IContact contact, Supplier<Boolean> supplier) {
92  return new Permission(contact, supplier);
93  }
94 
95 }
static Permission from(String... roles)
Definition: Permission.java:79
static Permission from(Supplier< Boolean > supplier)
Definition: Permission.java:83
static Permission from(IContact contact, String... roles)
Definition: Permission.java:87
static Permission from(IContact contact, Supplier< Boolean > supplier)
Definition: Permission.java:91
boolean isInRole(String role)
boolean hasAnyRoleKey(String role)