Search in sources :

Example 6 with SubjectReference

use of org.spongepowered.api.service.permission.SubjectReference in project LanternServer by LanternPowered.

the class ProxySubject method resolveNullableSubject.

@Nullable
default Subject resolveNullableSubject() {
    SubjectReference reference = getInternalSubject();
    if (reference == null) {
        final Optional<PermissionService> optService = Lantern.getGame().getServiceManager().provide(PermissionService.class);
        if (optService.isPresent()) {
            // Try to update the internal subject
            SubjectSettingCallback.apply(this, optService.get());
            // Get the new subject reference, can be null if failed
            reference = getInternalSubject();
        }
    }
    return reference == null ? null : reference.resolve().join();
}
Also used : PermissionService(org.spongepowered.api.service.permission.PermissionService) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) Nullable(javax.annotation.Nullable)

Example 7 with SubjectReference

use of org.spongepowered.api.service.permission.SubjectReference 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 8 with SubjectReference

use of org.spongepowered.api.service.permission.SubjectReference 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 9 with SubjectReference

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

the class ListGroupTests method createSubject.

private static Subject createSubject(String name, Subject... parents) {
    Subject subject = Mockito.mock(Subject.class);
    List<Subject> ls = Arrays.asList(parents);
    Mockito.when(subject.getIdentifier()).thenReturn(name);
    Mockito.when(subject.getOption(Mockito.anySetOf(Context.class), Mockito.eq("nucleus.list.weight"))).then(x -> ls.stream().map(y -> y.getOption("nucleus.list.weight")).filter(Optional::isPresent).findFirst().orElse(Optional.empty()));
    Mockito.when(subject.getOption(Mockito.eq("nucleus.list.weight"))).then(x -> ls.stream().map(y -> y.getOption("nucleus.list.weight")).filter(Optional::isPresent).findFirst().orElse(Optional.empty()));
    List<SubjectReference> lsr = getSubjectReferences(Arrays.asList(parents));
    Mockito.when(subject.getParents()).thenReturn(lsr);
    Mockito.when(subject.getParents(Mockito.anySetOf(Context.class))).thenReturn(lsr);
    return subject;
}
Also used : Context(org.spongepowered.api.service.context.Context) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) Subject(org.spongepowered.api.service.permission.Subject)

Example 10 with SubjectReference

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

the class ListGroupTests method testWeightsAreBeingAppliedCorrectlyWithAliases.

@Test
@SuppressWarnings("all")
public void testWeightsAreBeingAppliedCorrectlyWithAliases() {
    // 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();
    aliases.put("admin", "Admin");
    aliases.put("ace", "Ace");
    // 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)

Aggregations

SubjectReference (org.spongepowered.api.service.permission.SubjectReference)10 Subject (org.spongepowered.api.service.permission.Subject)8 List (java.util.List)4 Test (org.junit.Test)4 Player (org.spongepowered.api.entity.living.player.Player)4 Context (org.spongepowered.api.service.context.Context)2 HashMap (java.util.HashMap)1 Nullable (javax.annotation.Nullable)1 LanternPermissionService (org.lanternpowered.server.service.permission.LanternPermissionService)1 LanternSubjectCollection (org.lanternpowered.server.service.permission.base.LanternSubjectCollection)1 User (org.spongepowered.api.entity.living.player.User)1 PermissionService (org.spongepowered.api.service.permission.PermissionService)1 IMixinSubject (org.spongepowered.common.interfaces.IMixinSubject)1 MixinSubject (org.spongepowered.common.mixin.core.command.MixinSubject)1 SpongeSubjectCollection (org.spongepowered.common.service.permission.base.SpongeSubjectCollection)1