/
ObjectPermission.java
104 lines (91 loc) · 4.24 KB
/
ObjectPermission.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package net.ME1312.SubServers.Client.Sponge.Library;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.SubServers.Client.Common.Network.API.Host;
import net.ME1312.SubServers.Client.Common.Network.API.Proxy;
import net.ME1312.SubServers.Client.Common.Network.API.Server;
import net.ME1312.SubServers.Client.Common.Network.API.SubServer;
import org.spongepowered.api.service.permission.Subject;
import java.util.List;
/**
* Object Permissions Class
*/
public class ObjectPermission {
private ObjectPermission() {}
/**
* Determine if an <i>object</i> can perform some action on this proxy using possible permissions
*
* @param proxy Proxy to check against
* @param object Object to check against
* @param permissions Permissions to check (use <b>%</b> as a placeholder for the proxy name)
* @return Permission Check Result
*/
public static boolean permits(Proxy proxy, Subject object, String... permissions) {
return permits(proxy.getName(), object, permissions);
}
/**
* Determine if an <i>object</i> can perform some action on a host using possible permissions
*
* @param host Host to check against
* @param object Object to check against
* @param permissions Permissions to check (use <b>%</b> as a placeholder for the host name)
* @return Permission Check Result
*/
public static boolean permits(Host host, Subject object, String... permissions) {
return permits(host.getName(), object, permissions);
}
/**
* Determine if an <i>object</i> can perform some action on another object using possible permissions
*
* @param string String to check against
* @param object Object to check against
* @param permissions Permissions to check (use <b>%</b> as a placeholder for the object name)
* @return Permission Check Result
*/
private static boolean permits(String string, Subject object, String... permissions) {
if (Util.isNull(object)) throw new NullPointerException();
boolean permitted = false;
for (int p = 0; !permitted && p < permissions.length; p++) {
String perm = permissions[p];
if (perm != null) {
// Check all proxies & individual proxies permission
permitted = object.hasPermission(perm.replace("%", "*"))
|| object.hasPermission(perm.replace("%", string.toLowerCase()));
}
}
return permitted;
}
/**
* Determine if an <i>object</i> can perform some action on a server using possible permissions
*
* @param server Server to check against
* @param object Object to check against
* @param permissions Permissions to check (use <b>%</b> as a placeholder for the server name)
* @return Permission Check Result
*/
public static boolean permits(Server server, Subject object, String... permissions) {
if (Util.isNull(object)) throw new NullPointerException();
boolean permitted = false;
for (int p = 0; !permitted && p < permissions.length; p++) {
String perm = permissions[p];
if (perm != null) {
// Check all servers & individual servers permission
permitted = object.hasPermission(perm.replace("%", "*"))
|| object.hasPermission(perm.replace("%", server.getName().toLowerCase()));
// Check all hosts & individual hosts permission
if (server instanceof SubServer) {
permitted = permitted || object.hasPermission(perm.replace("%", "::*"))
|| object.hasPermission(perm.replace("%", "::" + ((SubServer) server).getHost().toLowerCase()));
}
// Check all groups & individual groups permission
List<String> groups = server.getGroups();
if (groups.size() > 0) {
permitted = permitted || object.hasPermission(perm.replace("%", ":*"));
for (int g = 0; !permitted && g < groups.size(); g++) {
permitted = object.hasPermission(perm.replace("%", ":" + groups.get(g).toLowerCase()));
}
}
}
}
return permitted;
}
}