Search in sources :

Example 21 with Subject

use of org.spongepowered.api.service.permission.Subject in project UltimateChat by FabioZumbi12.

the class UCPerms78 method getGroupAndTag.

public Subject getGroupAndTag(User player) throws InterruptedException, ExecutionException {
    HashMap<Integer, Subject> subs = new HashMap<>();
    for (SubjectReference sub : player.getParents()) {
        if (sub.getCollectionIdentifier().equals(getGroups().getIdentifier()) && (sub.getSubjectIdentifier() != null)) {
            Subject subj = sub.resolve().get();
            subs.put(subj.getParents().size(), subj);
        }
    }
    return subs.isEmpty() ? null : subs.get(Collections.max(subs.keySet()));
}
Also used : HashMap(java.util.HashMap) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) Subject(org.spongepowered.api.service.permission.Subject)

Example 22 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class NicknameService method setNick.

private void setNick(User pl, Cause cause, Text nickname, boolean bypass) throws NicknameException {
    String plain = nickname.toPlain().trim();
    if (plain.isEmpty()) {
        throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.tooshort"), NicknameException.Type.TOO_SHORT);
    }
    // Does the user exist?
    try {
        Optional<User> match = Sponge.getServiceManager().provideUnchecked(UserStorageService.class).get(nickname.toPlain().trim());
        // The only person who can use such a name is oneself.
        if (match.isPresent() && !match.get().getUniqueId().equals(pl.getUniqueId())) {
            // Fail - cannot use another's name.
            throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.nameinuse", plain), NicknameException.Type.NOT_OWN_IGN);
        }
    } catch (IllegalArgumentException ignored) {
    // We allow some other nicknames too.
    }
    if (!bypass) {
        // Giving subject must have the colour permissions and whatnot. Also,
        // colour and color are the two spellings we support. (RULE BRITANNIA!)
        Optional<Subject> os = cause.first(Subject.class);
        if (os.isPresent()) {
            stripPermissionless(os.get(), nickname);
        }
        if (!pattern.matcher(plain).matches()) {
            throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.nopattern", pattern.pattern()), NicknameException.Type.INVALID_PATTERN);
        }
        int strippedNameLength = plain.length();
        // Do a regex remove to check minimum length requirements.
        if (strippedNameLength < Math.max(min, 1)) {
            throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.tooshort"), NicknameException.Type.TOO_SHORT);
        }
        // Do a regex remove to check maximum length requirements. Will be at least the minimum length
        if (strippedNameLength > Math.max(max, min)) {
            throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.toolong"), NicknameException.Type.TOO_SHORT);
        }
    }
    // Send an event
    Text currentNickname = getNickname(pl).orElse(null);
    ChangeNicknameEvent cne = new ChangeNicknameEvent(cause, currentNickname, nickname, pl);
    if (Sponge.getEventManager().post(cne)) {
        throw new NicknameException(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.eventcancel", pl.getName()), NicknameException.Type.EVENT_CANCELLED);
    }
    ModularUserService mus = Nucleus.getNucleus().getUserDataManager().getUnchecked(pl);
    NicknameUserDataModule nicknameUserDataModule = mus.get(NicknameUserDataModule.class);
    nicknameUserDataModule.setNickname(nickname);
    mus.set(nicknameUserDataModule);
    mus.save();
    Text set = nicknameUserDataModule.getNicknameAsText().get();
    if (pl.isOnline()) {
        pl.getPlayer().get().sendMessage(Text.builder().append(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.nick.success.base")).append(Text.of(" - ", TextColors.RESET, set)).build());
    }
}
Also used : User(org.spongepowered.api.entity.living.player.User) ChangeNicknameEvent(io.github.nucleuspowered.nucleus.modules.nickname.events.ChangeNicknameEvent) Text(org.spongepowered.api.text.Text) ModularUserService(io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService) Subject(org.spongepowered.api.service.permission.Subject) UserStorageService(org.spongepowered.api.service.user.UserStorageService) NicknameException(io.github.nucleuspowered.nucleus.api.exceptions.NicknameException) NicknameUserDataModule(io.github.nucleuspowered.nucleus.modules.nickname.datamodules.NicknameUserDataModule)

Example 23 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListGroupTests method testWeightsAreBeingAppliedCorrectly.

// Make 2 groups, example one admin group and another ace
// 
// Set the order on nucleus config for them to show admin first then ace
// group-order=[ admin, ace ]
// 
// Set weight of admin to some number higher than ace
// 
// Reloadable or restart server then /list to see if it worked
@Test
@SuppressWarnings("all")
public void testWeightsAreBeingAppliedCorrectly() {
    // Create two groups.
    Subject admin = createSubjectWithWeight("admin", 1);
    Subject ace = createSubjectWithWeight("ace", 0);
    // The player is in both groups. Also, order is important.
    List<Subject> parents = Lists.newArrayList(admin, ace);
    Player player = Mockito.mock(Player.class);
    List<SubjectReference> lsr = getSubjectReferences(parents);
    Mockito.when(player.getParents()).thenReturn(lsr);
    Mockito.when(player.getParents(Mockito.anySet())).thenReturn(lsr);
    // Create our map.
    Map<Player, List<String>> map = Maps.newHashMap();
    map.put(player, Lists.newArrayList("admin", "ace"));
    // No aliases.
    Map<String, String> aliases = Maps.newHashMap();
    // Now, let's run it through our method.
    Map<String, List<Player>> result = ListPlayerCommand.linkPlayersToGroups(parents, aliases, map);
    Assert.assertEquals("There should only be one entry", 1, result.size());
    List<Player> players = result.get("admin");
    Assert.assertNotNull("Players is null", players);
    Assert.assertEquals("There should only be one player!", 1, players.size());
    Assert.assertTrue("map is not empty", map.isEmpty());
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) List(java.util.List) Subject(org.spongepowered.api.service.permission.Subject) Test(org.junit.Test)

Example 24 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListGroupTests method tesMultipleWeightedTrees.

@Test
public void tesMultipleWeightedTrees() {
    // Using example from Rasgnarok
    // Gym Leader has a weight of 6, and Moderator 9
    // I tried as Helper(5) and QuestBuilder(8)
    // and Helper showed instead of QB
    // They are not in the same inheritance tree either
    // Swapping Helper for GymLeader reproduced the issue
    // Helper parents DeputyMod which in turn parents Moderator
    // Gym Leader parents 8 other groups
    // and QB inherits from Builder
    // So Helpers that are also Leaders are showing as Leaders
    List<Subject> subjects = Lists.newArrayList();
    Subject gymLeader = createSubjectWithWeight("gymleader", 6);
    subjects.add(gymLeader);
    List<Subject> gymleaders = createSubjects(8, "gym", gymLeader);
    subjects.addAll(gymleaders);
    Subject builder = createSubject("builder");
    subjects.add(builder);
    Subject questBuilder = createSubjectWithWeight("QuestBuilder", 8, builder);
    subjects.add(questBuilder);
    Subject helper = createSubjectWithWeight("helper", 5);
    Subject dep = createSubjectWithWeight("depmod", 7, helper);
    Subject mod = createSubjectWithWeight("mod", 9, dep);
    subjects.add(helper);
    subjects.add(dep);
    subjects.add(mod);
    List<Subject> sorted = printWeights(subjects.stream().sorted((x, y) -> ListPlayerCommand.groupComparison(ListPlayerCommand.weightingFunction, x, y)).collect(Collectors.toList()));
    List<Integer> integers = sorted.stream().map(x -> Util.getIntOptionFromSubject(x, "nucleus.list.weight").orElse(0)).collect(Collectors.toList());
    for (int i = 1; i < integers.size(); i++) {
        Assert.assertTrue(integers.get(i - 1) >= integers.get(i));
    }
}
Also used : Arrays(java.util.Arrays) Context(org.spongepowered.api.service.context.Context) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) List(java.util.List) Lists(com.google.common.collect.Lists) ListPlayerCommand(io.github.nucleuspowered.nucleus.modules.playerinfo.commands.ListPlayerCommand) Map(java.util.Map) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) Player(org.spongepowered.api.entity.living.player.Player) Assert(org.junit.Assert) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) Subject(org.spongepowered.api.service.permission.Subject) Subject(org.spongepowered.api.service.permission.Subject) Test(org.junit.Test)

Example 25 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListGroupTests method testTwoGroupsReturnsTheCorrectOrder.

@Test
public void testTwoGroupsReturnsTheCorrectOrder() {
    Subject admin = createSubjectWithWeight("admin", 1);
    Subject ace = createSubjectWithWeight("ace", 0);
    List<Subject> ls = Lists.newArrayList(ace, admin);
    ls.sort((x, y) -> ListPlayerCommand.groupComparison(ListPlayerCommand.weightingFunction, x, y));
    Assert.assertEquals(admin, ls.get(0));
    Assert.assertEquals(ace, ls.get(1));
}
Also used : Subject(org.spongepowered.api.service.permission.Subject) Test(org.junit.Test)

Aggregations

Subject (org.spongepowered.api.service.permission.Subject)28 SubjectReference (org.spongepowered.api.service.permission.SubjectReference)11 Test (org.junit.Test)10 List (java.util.List)9 Player (org.spongepowered.api.entity.living.player.Player)9 Context (org.spongepowered.api.service.context.Context)6 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5 PermissionService (org.spongepowered.api.service.permission.PermissionService)5 Text (org.spongepowered.api.text.Text)5 Lists (com.google.common.collect.Lists)4 Maps (com.google.common.collect.Maps)4 Util (io.github.nucleuspowered.nucleus.Util)4 Map (java.util.Map)4 CommandSource (org.spongepowered.api.command.CommandSource)4 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)3 ListPlayerCommand (io.github.nucleuspowered.nucleus.modules.playerinfo.commands.ListPlayerCommand)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 CompletableFuture (java.util.concurrent.CompletableFuture)3