Launcher/LauncherAPI/src/main/java/pro/gravit/launcher/ClientPermissions.java

169 lines
4.5 KiB
Java
Raw Normal View History

package pro.gravit.launcher;
2021-10-12 12:55:32 +03:00
import java.util.*;
2019-10-19 19:46:04 +03:00
2018-10-01 13:08:16 +03:00
public class ClientPermissions {
public static final ClientPermissions DEFAULT = new ClientPermissions();
2020-04-05 17:47:21 +03:00
@LauncherNetworkAPI
private List<String> roles;
@LauncherNetworkAPI
private List<String> perms;
2021-10-12 12:55:32 +03:00
private transient List<PermissionPattern> available;
2019-01-15 06:35:39 +03:00
2018-10-01 13:08:16 +03:00
public ClientPermissions() {
2018-10-01 13:08:16 +03:00
}
2018-11-08 15:30:16 +03:00
2022-04-11 15:28:47 +03:00
public ClientPermissions(List<String> roles, List<String> permissions) {
this.roles = new ArrayList<>(roles);
this.perms = new ArrayList<>(permissions);
}
2018-11-08 15:30:16 +03:00
public static ClientPermissions getSuperuserAccount() {
ClientPermissions perm = new ClientPermissions();
perm.addPerm("*");
return perm;
}
2020-04-05 10:27:04 +03:00
public boolean hasRole(String role) {
return roles != null && roles.contains(role);
}
public synchronized void compile() {
if (available != null) {
return;
}
2021-11-22 11:00:09 +03:00
if (perms == null) {
perms = new ArrayList<>(0);
}
available = new ArrayList<>(perms.size());
for (String a : perms) {
2021-10-12 12:55:32 +03:00
available.add(new PermissionPattern(a));
}
}
public boolean hasPerm(String action) {
if (available == null) {
compile();
}
2021-10-12 12:55:32 +03:00
for (PermissionPattern p : available) {
if (p.match(action)) {
return true;
}
}
return false;
}
public void addRole(String role) {
if (roles == null) {
roles = new ArrayList<>(1);
}
roles.add(role);
}
public void addPerm(String perm) {
if (perms == null) {
perms = new ArrayList<>(1);
}
perms.add(perm);
2022-11-18 10:47:03 +03:00
if (available == null) {
2021-09-25 14:46:07 +03:00
available = new ArrayList<>(1);
}
available.add(new PermissionPattern(perm));
}
public void removePerm(String action) {
if (perms == null) {
return;
}
2022-11-18 10:47:03 +03:00
if (available == null) {
return;
}
perms.remove(action);
available.remove(new PermissionPattern(action));
}
public List<String> getRoles() {
return roles;
}
public List<String> getPerms() {
return perms;
}
@Override
public String toString() {
2021-06-22 07:13:12 +03:00
return "ClientPermissions{" +
2021-09-25 14:46:07 +03:00
"roles=" + String.join(", ", roles == null ? Collections.emptyList() : roles) +
", actions=" + String.join(", ", perms == null ? Collections.emptyList() : perms) +
2021-06-22 07:13:12 +03:00
'}';
}
2020-04-05 10:27:04 +03:00
2021-10-12 12:55:32 +03:00
public static class PermissionPattern {
private final String[] parts;
private final int priority;
public PermissionPattern(String pattern) {
List<String> prepare = new ArrayList<>();
2022-11-18 10:47:03 +03:00
for (int i = 0; true; ) {
2021-10-12 12:55:32 +03:00
int pos = pattern.indexOf("*", i);
2022-11-18 10:47:03 +03:00
if (pos >= 0) {
2021-10-12 12:55:32 +03:00
prepare.add(pattern.substring(i, pos));
2022-11-18 10:47:03 +03:00
i = pos + 1;
2021-10-12 12:55:32 +03:00
} else {
prepare.add(pattern.substring(i));
break;
}
}
priority = prepare.size() - 1;
parts = prepare.toArray(new String[0]);
}
public int getPriority() {
return priority;
}
public boolean match(String str) {
2022-11-18 10:47:03 +03:00
if (parts.length == 0) {
2021-10-12 12:55:32 +03:00
return true;
}
2022-11-18 10:47:03 +03:00
if (parts.length == 1) {
2021-10-12 12:55:32 +03:00
return parts[0].equals(str);
}
int offset = 0;
2022-11-18 10:47:03 +03:00
if (!str.startsWith(parts[0])) {
2021-10-12 12:55:32 +03:00
return false;
}
2022-11-18 10:47:03 +03:00
if (!str.endsWith(parts[parts.length - 1])) {
2021-10-12 12:55:32 +03:00
return false;
}
2022-11-18 10:47:03 +03:00
for (int i = 1; i < parts.length - 1; ++i) {
2021-10-12 12:55:32 +03:00
int pos = str.indexOf(parts[i], offset);
2022-11-18 10:47:03 +03:00
if (pos >= 0) {
offset = pos + 1;
2021-10-12 12:55:32 +03:00
} else {
return false;
}
}
return true;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PermissionPattern that = (PermissionPattern) o;
return priority == that.priority && Arrays.equals(parts, that.parts);
}
@Override
public int hashCode() {
int result = Objects.hash(priority);
result = 31 * result + Arrays.hashCode(parts);
return result;
}
2021-10-12 12:55:32 +03:00
}
2018-10-01 13:08:16 +03:00
}