org.spongepowered.api.service.permission.Subject Java Examples

The following examples show how to use org.spongepowered.api.service.permission.Subject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SubjectComparator.java    From EssentialCmds with MIT License 6 votes vote down vote up
@Override
public int compare(Subject o1, Subject o2)
{
	if (o1.getParents().size() > o2.getParents().size())
	{
		return 1;
	}
	else if (o1.getParents().size() == o2.getParents().size())
	{
		return 0;
	}
	else
	{
		return -1;
	}
}
 
Example #2
Source File: CommandHelper.java    From GriefPrevention with MIT License 6 votes vote down vote up
public static Consumer<CommandSource> createFlagConsumer(CommandSource src, Subject subject, String subjectName, Set<Context> contexts, GPClaim claim, String flagPermission, Tristate flagValue, String source) {
    return consumer -> {
        String target = flagPermission.replace(GPPermissions.FLAG_BASE + ".", "");
        if (target.isEmpty()) {
            target = "any";
        }
        Tristate newValue = Tristate.UNDEFINED;
        if (flagValue == Tristate.TRUE) {
            newValue = Tristate.FALSE;
        } else if (flagValue == Tristate.UNDEFINED) {
            newValue = Tristate.TRUE;
        }

        CommandHelper.applyFlagPermission(src, subject, subjectName, claim, flagPermission, source, target, newValue, null, FlagType.GROUP);
    };
}
 
Example #3
Source File: Host.java    From SubServers-2 with Apache License 2.0 6 votes vote down vote up
/**
 * Determine if an <i>object</i> can perform some action on this host using possible permissions
 *
 * @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 boolean permits(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("%", this.getName().toLowerCase()));
        }
    }

    return permitted;
}
 
Example #4
Source File: GPClaim.java    From GriefPrevention with MIT License 6 votes vote down vote up
@Override
public boolean isGroupTrusted(String group, TrustType type) {
    if (group == null) {
        return false;
    }

    if (!PermissionUtils.hasGroupSubject(group)) {
        return false;
    }

    final Subject subj = PermissionUtils.getGroupSubject(group);
    Set<Context> contexts = new HashSet<>();
    contexts.add(this.getContext());

    return subj.hasPermission(contexts, GPPermissions.getTrustPermission(type));
}
 
Example #5
Source File: ClaimContextCalculator.java    From GriefPrevention with MIT License 6 votes vote down vote up
@Override
public boolean matches(Context context, Subject subject) {
    if (context.getKey().equals("gp_claim")) {
        if (subject.getCommandSource().isPresent() && subject.getCommandSource().get() instanceof Player) {
            Player player = (Player) subject.getCommandSource().get();
            GPPlayerData playerData = GriefPreventionPlugin.instance.dataStore.getPlayerData(player.getWorld(), player.getUniqueId());
            if (playerData == null) {
                return false;
            }

            GPClaim playerClaim = GriefPreventionPlugin.instance.dataStore.getClaimAtPlayer(playerData, player.getLocation());
            if (playerClaim != null && playerClaim.id.equals(UUID.fromString(context.getValue()))) {
                return true;
            }
        }
    }

    return false;
}
 
Example #6
Source File: CommandHelper.java    From GriefPrevention with MIT License 6 votes vote down vote up
public static Text getClickableText(CommandSource src, Subject subject, String subjectName, Set<Context> contexts, GPClaim claim, String flagPermission, Tristate flagValue, String source, FlagType type) {
    Text onClickText = Text.of("Click here to toggle flag value.");
    boolean hasPermission = true;
    if (type == FlagType.INHERIT) {
        onClickText = Text.of("This flag is inherited from parent claim ", claim.getName().orElse(claim.getFriendlyNameType()), " and ", TextStyles.UNDERLINE, "cannot", TextStyles.RESET, " be changed.");
        hasPermission = false;
    } else if (src instanceof Player) {
        Text denyReason = claim.allowEdit((Player) src);
        if (denyReason != null) {
            onClickText = denyReason;
            hasPermission = false;
        }
    }

    Text.Builder textBuilder = Text.builder()
    .append(Text.of(flagValue.toString().toLowerCase()))
    .onHover(TextActions.showText(Text.of(onClickText, "\n", getFlagTypeHoverText(type))));
    if (hasPermission) {
        textBuilder.onClick(TextActions.executeCallback(createFlagConsumer(src, subject, subjectName, contexts, claim, flagPermission, flagValue, source)));
    }
    return textBuilder.build();
}
 
Example #7
Source File: VirtualChestPermissionManager.java    From VirtualChest with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean matches(Context context, Subject subject)
{
    if (CONTEXT_KEY.equals(context.getKey()))
    {
        try
        {
            UUID value = UUID.fromString(context.getValue());
            VirtualChestActions actions = this.plugin.getVirtualChestActions();
            return actions.getActivatedActions(subject.getIdentifier()).contains(value);
        }
        catch (IllegalArgumentException e)
        {
            return false;
        }
    }
    return false;
}
 
Example #8
Source File: ClaimContextCalculator.java    From GriefDefender with MIT License 6 votes vote down vote up
@Override
public boolean matches(Context context, Subject subject) {
    if (context.getKey().equals("gd_claim")) {
        if (subject.getCommandSource().isPresent() && subject.getCommandSource().get() instanceof Player) {
            Player player = (Player) subject.getCommandSource().get();
            GDPlayerData playerData = GriefDefenderPlugin.getInstance().dataStore.getPlayerData(player.getWorld(), player.getUniqueId());
            if (playerData == null) {
                return false;
            }

            GDClaim playerClaim = GriefDefenderPlugin.getInstance().dataStore.getClaimAtPlayer(playerData, player.getLocation());
            if (playerClaim != null && playerClaim.getUniqueId().equals(UUID.fromString(context.getValue()))) {
                return true;
            }
        }
    }

    return false;
}
 
Example #9
Source File: VirtualChestPermissionManager.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void accumulateContexts(Subject subject, Set<Context> accumulator)
{
    this.plugin.getVirtualChestActions().getActivatedActions(subject.getIdentifier()).forEach(actionUUID ->
    {
        SubjectData data = subject.getTransientSubjectData();
        Context context = new Context(CONTEXT_KEY, actionUUID.toString());
        Map<String, Boolean> permissions = data.getPermissions(Collections.singleton(context));
        this.logger.debug("Ignored {} permission(s) for action {} (context):", permissions.size(), actionUUID);
        permissions.forEach((permission, state) -> this.logger.debug("- {} ({})", permission, state));
        accumulator.add(context);
    });
}
 
Example #10
Source File: PEXActions.java    From ChatUI with MIT License 5 votes vote down vote up
@Override
public void removeParent(Player player, Subject subject, Set<Context> contexts, SubjectReference parent) {
    command(player, subjContext(subject, contexts)
            .append("parent remove ").append(parent.getCollectionIdentifier()).append(' ')
            .append(parent.getSubjectIdentifier())
            .toString());
}
 
Example #11
Source File: PEXActions.java    From ChatUI with MIT License 5 votes vote down vote up
@Override
public CompletableFuture<Boolean> setOption(Player player, Subject subject, Set<Context> contexts, String key, String value) {
    CommandResult res = command(player, subjContext(subject, contexts)
            .append("options ").append(key).append(' ')
            .append(value == null ? "" : value).toString());
    return CompletableFuture.completedFuture(res.getSuccessCount().isPresent() && res.getSuccessCount().get() > 0);
}
 
Example #12
Source File: PermissionOption.java    From UltimateCore with MIT License 5 votes vote down vote up
public Integer getIntFor(Subject subject) {
    String value = getFor(subject);
    if (!ArgumentUtil.isInteger(value)) {
        Messages.log(Messages.getFormatted("core.option.invalidnumber", "%option%", get(), "%value%", value));
        return 0;
    }
    return Integer.parseInt(value);
}
 
Example #13
Source File: SubjectCollectionProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public @NonNull Map<Subject, Boolean> getLoadedWithPermission(@NonNull String s) {
    return this.handle.getLoadedWithPermission(s).entrySet().stream()
            .collect(ImmutableCollectors.toMap(
                    sub -> sub.getKey().sponge(),
                    Map.Entry::getValue
            ));
}
 
Example #14
Source File: SubjectViewer.java    From ChatUI with MIT License 5 votes vote down vote up
public void setActive(Subject subject, boolean clearHistory) {
    if (clearHistory) {
        this.history.clear();
    } else if (this.activeSubj != null) {
        this.history.add(this.activeSubj);
    }
    this.permList = null;
    this.activeSubj = subject;
    this.tableScroll.reset();
}
 
Example #15
Source File: CommandUntrustAll.java    From GriefPrevention with MIT License 5 votes vote down vote up
private void removeAllGroupTrust(Claim claim, Subject group) {
    GPClaim gpClaim = (GPClaim) claim;
    Set<Context> contexts = new HashSet<>(); 
    contexts.add(gpClaim.getContext());
    for (TrustType type : TrustType.values()) {
        group.getSubjectData().setPermission(contexts, GPPermissions.getTrustPermission(type), Tristate.UNDEFINED);
        gpClaim.getGroupTrustList(type).remove(group);
        gpClaim.getInternalClaimData().setRequiresSave(true);
        for (Claim child : gpClaim.children) {
            this.removeAllGroupTrust(child, group);
        }
    }
}
 
Example #16
Source File: GPFlagClaimEvent.java    From GriefPrevention with MIT License 5 votes vote down vote up
public Set(Claim claim, Subject subject, ClaimFlag flag, String source, String target, Tristate value, Context context) {
    super(claim, subject);
    this.flag = flag;
    this.source = source;
    this.target = target;
    this.value = value;
    this.context = context;
}
 
Example #17
Source File: PermissionDescriptionProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public @NonNull Map<Subject, Boolean> getAssignedSubjects(@NonNull String s) {
    return this.handle.getAssignedSubjects(s).entrySet().stream()
            .collect(ImmutableCollectors.toMap(
                    e -> new SubjectProxy(this.service, e.getKey().toReference()),
                    Map.Entry::getValue
            ));
}
 
Example #18
Source File: SpongeContextManager.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public QueryOptionsSupplier getCacheFor(Subject subject) {
    if (subject == null) {
        throw new NullPointerException("subject");
    }

    return this.subjectCaches.get(subject);
}
 
Example #19
Source File: SpongeContextManager.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
protected void invalidateCache(Subject subject) {
    QueryOptionsCache<Subject> cache = this.subjectCaches.getIfPresent(subject);
    if (cache != null) {
        cache.invalidate();
    }
}
 
Example #20
Source File: GPClaim.java    From GriefPrevention with MIT License 5 votes vote down vote up
@Override
public Tristate getPermissionValue(Subject subject, ClaimFlag flag, String source, String target, Context context) {
    if (source.equalsIgnoreCase("any:any") || source.equalsIgnoreCase("any")) {
        source = null;
    }
    if (target.equalsIgnoreCase("any:any") || target.equalsIgnoreCase("any")) {
        target = null;
    }
    if (subject != GriefPreventionPlugin.GLOBAL_SUBJECT && (context == this.getDefaultContext() || context == this.getOverrideContext())) {
        return Tristate.UNDEFINED;
    }
    return GPPermissionHandler.getClaimPermission(this, flag, subject, source, target, context);
}
 
Example #21
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull List<Subject> getParents(@NonNull Set<Context> contexts) {
    return (List) handle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts)).stream()
            .map(s -> new SubjectProxy(this.service, s))
            .collect(ImmutableCollectors.toList())).join();
}
 
Example #22
Source File: SubjectDataProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public boolean removeParent(@NonNull Set<Context> contexts, @NonNull Subject parent) {
    handle().thenCompose(handle -> handle.removeParent(
            CompatibilityUtil.convertContexts(contexts),
            this.service.getReferenceFactory().obtain(parent)
    ));
    return true;
}
 
Example #23
Source File: PrefixVariable.java    From UltimateCore with MIT License 5 votes vote down vote up
@Override
public Optional<Text> getValue(@Nullable Object player) {
    if (player instanceof Subject) {
        return Optional.of(Messages.toText(((Subject) player).getOption("prefix").orElse("")));
    }
    return Optional.empty();
}
 
Example #24
Source File: SubjectCollectionProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull Map<Subject, Boolean> getAllWithPermission(@NonNull String s) {
    // again, these methods will lazily load subjects.
    return (Map) this.handle.getAllWithPermission(s)
            .thenApply(map -> map.entrySet().stream()
                    .collect(ImmutableCollectors.toMap(
                            e -> new SubjectProxy(this.service, e.getKey()),
                            Map.Entry::getValue
                    ))
            ).join();
}
 
Example #25
Source File: SubjectCollectionProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull Map<Subject, Boolean> getAllWithPermission(@NonNull Set<Context> set, @NonNull String s) {
    return (Map) this.handle.getAllWithPermission(CompatibilityUtil.convertContexts(set), s)
            .thenApply(map -> map.entrySet().stream()
                    .collect(ImmutableCollectors.toMap(
                            e -> new SubjectProxy(this.service, e.getKey()),
                            Map.Entry::getValue
                    ))
            ).join();
}
 
Example #26
Source File: SubjectProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public boolean isChildOf(@NonNull Set<Context> contexts, @NonNull Subject parent) {
    return handle().thenApply(handle -> handle.isChildOf(
            CompatibilityUtil.convertContexts(contexts),
            this.service.getReferenceFactory().obtain(parent)
    )).join();
}
 
Example #27
Source File: SubjectProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull List<Subject> getParents() {
    return (List) handle().thenApply(handle -> handle.getParents(getContextsCache().getContextSet()).stream()
            .map(s -> new SubjectProxy(this.service, s))
            .collect(ImmutableCollectors.toList())).join();
}
 
Example #28
Source File: SubjectProxy.java    From LuckPerms with MIT License 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public @NonNull List<Subject> getParents(@NonNull Set<Context> contexts) {
    return (List) handle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts)).stream()
            .map(s -> new SubjectProxy(this.service, s))
            .collect(ImmutableCollectors.toList())).join();
}
 
Example #29
Source File: Server.java    From SubServers-2 with Apache License 2.0 5 votes vote down vote up
/**
 * Determine if an <i>object</i> can perform some action on this server using possible permissions
 *
 * @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 boolean permits(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("%", this.getName().toLowerCase()));

            // Check all hosts & individual hosts permission
            if (this instanceof SubServer) {
                permitted = permitted || object.hasPermission(perm.replace("%", "::*"))
                                      || object.hasPermission(perm.replace("%", "::" + ((SubServer) this).getHost().toLowerCase()));
            }

            // Check all groups & individual groups permission
            List<String> groups = this.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;
}
 
Example #30
Source File: ClaimContextCalculator.java    From GriefDefender with MIT License 5 votes vote down vote up
@Override
public void accumulateContexts(Subject calculable, Set<Context> accumulator) {
    if (calculable.getCommandSource().isPresent() && calculable.getCommandSource().get() instanceof Player) {
        Player player = (Player) calculable.getCommandSource().get();
        GDPlayerData playerData = GriefDefenderPlugin.getInstance().dataStore.getPlayerData(player.getWorld(), player.getUniqueId());
        if (playerData == null) {
            return;
        }
        if (playerData.ignoreActiveContexts) {
            playerData.ignoreActiveContexts = false;
            return;
        }

        GDClaim sourceClaim = GriefDefenderPlugin.getInstance().dataStore.getClaimAtPlayer(playerData, player.getLocation());
        if (sourceClaim != null) {
            if (playerData == null || playerData.canIgnoreClaim(sourceClaim)) {
                return;
            }

            if (sourceClaim.parent != null && sourceClaim.getData().doesInheritParent()) {
                accumulator.add(sourceClaim.parent.getSpongeContext());
            } else {
                accumulator.add(sourceClaim.getSpongeContext());
            }
            accumulator.add(new Context("server", GriefDefenderPlugin.getInstance().getPermissionProvider().getServerName()));
        }
    }

}