2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launcher;
|
2019-01-04 18:45:11 +03:00
|
|
|
|
2019-06-03 10:58:10 +03:00
|
|
|
import pro.gravit.launcher.serialize.HInput;
|
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
import java.io.IOException;
|
2021-09-22 12:50:05 +03:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.regex.Pattern;
|
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
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-03-30 06:56:23 +03:00
|
|
|
public long permissions;
|
2020-04-05 17:47:21 +03:00
|
|
|
@LauncherNetworkAPI
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-03-30 06:56:23 +03:00
|
|
|
public long flags;
|
2021-09-22 12:50:05 +03:00
|
|
|
@LauncherNetworkAPI
|
|
|
|
private List<String> roles;
|
|
|
|
@LauncherNetworkAPI
|
|
|
|
private List<String> actions;
|
|
|
|
|
|
|
|
private transient List<Pattern> available;
|
2019-01-15 06:35:39 +03:00
|
|
|
|
2019-01-04 18:45:11 +03:00
|
|
|
public ClientPermissions(HInput input) throws IOException {
|
2019-04-03 11:51:41 +03:00
|
|
|
this(input.readLong());
|
2019-01-04 18:45:11 +03:00
|
|
|
}
|
2019-01-15 06:35:39 +03:00
|
|
|
|
2018-10-01 13:08:16 +03:00
|
|
|
public ClientPermissions() {
|
2020-03-30 06:56:23 +03:00
|
|
|
|
2018-10-01 13:08:16 +03:00
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2020-03-30 06:56:23 +03:00
|
|
|
public ClientPermissions(long permissions) {
|
|
|
|
this.permissions = permissions;
|
2018-10-05 11:25:12 +03:00
|
|
|
}
|
2019-04-03 16:27:40 +03:00
|
|
|
|
2020-03-30 06:56:23 +03:00
|
|
|
public ClientPermissions(long permissions, long flags) {
|
|
|
|
this.permissions = permissions;
|
|
|
|
this.flags = flags;
|
|
|
|
}
|
2019-12-08 22:21:05 +03:00
|
|
|
|
2018-11-08 15:30:16 +03:00
|
|
|
public static ClientPermissions getSuperuserAccount() {
|
2018-10-05 11:36:50 +03:00
|
|
|
ClientPermissions perm = new ClientPermissions();
|
2020-09-25 18:44:39 +03:00
|
|
|
perm.setPermission(PermissionConsts.ADMIN, true);
|
2021-09-22 12:50:05 +03:00
|
|
|
perm.addAction("*");
|
2018-10-05 11:36:50 +03:00
|
|
|
return perm;
|
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
|
|
|
public long toLong() {
|
|
|
|
return permissions;
|
|
|
|
}
|
|
|
|
|
2021-09-22 12:50:05 +03:00
|
|
|
public boolean hasRole(String role) {
|
|
|
|
return roles != null && roles.contains(role);
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void compile() {
|
|
|
|
if (available != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
available = new ArrayList<>(actions.size());
|
|
|
|
for (String a : actions) {
|
|
|
|
available.add(Pattern.compile(a));
|
|
|
|
}
|
|
|
|
if (permissions != 0) {
|
|
|
|
if (isPermission(PermissionConsts.ADMIN)) {
|
|
|
|
roles.add("ADMIN");
|
|
|
|
available.add(Pattern.compile(".*"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasAction(String action) {
|
|
|
|
if (available == null) {
|
|
|
|
compile();
|
|
|
|
}
|
|
|
|
for (Pattern p : available) {
|
|
|
|
if (p.matcher(action).matches()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addRole(String role) {
|
|
|
|
if (roles == null) {
|
|
|
|
roles = new ArrayList<>(1);
|
|
|
|
}
|
|
|
|
roles.add(role);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addAction(String action) {
|
|
|
|
if (actions == null) {
|
|
|
|
actions = new ArrayList<>();
|
|
|
|
}
|
|
|
|
actions.add(action);
|
|
|
|
available.add(Pattern.compile(action));
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getRoles() {
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getActions() {
|
|
|
|
return actions;
|
|
|
|
}
|
|
|
|
|
2020-03-30 06:56:23 +03:00
|
|
|
//Read methods
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-04-05 10:27:04 +03:00
|
|
|
public final boolean isPermission(PermissionConsts con) {
|
2020-03-30 06:56:23 +03:00
|
|
|
return (permissions & con.mask) != 0;
|
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-04-05 10:27:04 +03:00
|
|
|
public final boolean isPermission(long mask) {
|
2020-03-30 06:56:23 +03:00
|
|
|
return (permissions & mask) != 0;
|
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-04-05 10:27:04 +03:00
|
|
|
public final boolean isFlag(FlagConsts con) {
|
2020-03-30 06:56:23 +03:00
|
|
|
return (flags & con.mask) != 0;
|
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-04-05 10:27:04 +03:00
|
|
|
public final boolean isFlag(long mask) {
|
2020-03-30 06:56:23 +03:00
|
|
|
return (flags & mask) != 0;
|
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
2020-03-30 06:56:23 +03:00
|
|
|
//Write methods
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-04-05 10:27:04 +03:00
|
|
|
public final void setPermission(PermissionConsts con, boolean value) {
|
|
|
|
if (value) this.permissions |= con.mask;
|
2020-03-30 06:56:23 +03:00
|
|
|
else this.permissions &= ~con.mask;
|
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-04-05 10:27:04 +03:00
|
|
|
public final void setPermission(long mask, boolean value) {
|
|
|
|
if (value) this.permissions |= mask;
|
2020-03-30 06:56:23 +03:00
|
|
|
else this.permissions &= ~mask;
|
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-04-05 10:27:04 +03:00
|
|
|
public final void setFlag(FlagConsts con, boolean value) {
|
|
|
|
if (value) this.flags |= con.mask;
|
2020-03-30 06:56:23 +03:00
|
|
|
else this.flags &= ~con.mask;
|
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-04-05 10:27:04 +03:00
|
|
|
public final void setFlag(long mask, boolean value) {
|
|
|
|
if (value) this.flags |= mask;
|
2020-03-30 06:56:23 +03:00
|
|
|
else this.flags &= ~mask;
|
|
|
|
}
|
|
|
|
|
2019-04-03 12:21:53 +03:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2021-09-22 12:50:05 +03:00
|
|
|
if (roles != null || actions != null) {
|
|
|
|
return "ClientPermissions{" +
|
|
|
|
"roles=" + String.join(", ", roles == null ? Collections.emptyList() : roles) +
|
|
|
|
", actions=" + String.join(", ", actions == null ? Collections.emptyList() : actions) +
|
|
|
|
'}';
|
|
|
|
}
|
2021-06-22 07:13:12 +03:00
|
|
|
return "ClientPermissions{" +
|
|
|
|
"permissions=" + permissions +
|
|
|
|
", flags=" + flags +
|
|
|
|
'}';
|
2019-04-03 12:21:53 +03:00
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-04-05 10:27:04 +03:00
|
|
|
public enum PermissionConsts {
|
|
|
|
ADMIN(0x01),
|
|
|
|
MANAGEMENT(0x02);
|
|
|
|
public final long mask;
|
|
|
|
|
|
|
|
PermissionConsts(long mask) {
|
|
|
|
this.mask = mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 12:50:05 +03:00
|
|
|
@Deprecated
|
2020-04-05 10:27:04 +03:00
|
|
|
public enum FlagConsts {
|
|
|
|
SYSTEM(0x01),
|
|
|
|
BANNED(0x02),
|
|
|
|
UNTRUSTED(0x04),
|
|
|
|
HIDDEN(0x08);
|
|
|
|
public final long mask;
|
|
|
|
|
|
|
|
FlagConsts(long mask) {
|
|
|
|
this.mask = mask;
|
|
|
|
}
|
|
|
|
}
|
2018-10-01 13:08:16 +03:00
|
|
|
}
|