Search in sources :

Example 1 with SubjectReference

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

the class SubjectSettingCallback method apply.

static boolean apply(@Nullable ProxySubject ref, @Nullable PermissionService input) {
    if (ref == null) {
        // as a listener, and will not be tested again.
        return false;
    }
    // if PS has just been unregistered, ignore the change.
    if (input == null) {
        return true;
    }
    final SubjectReference subject;
    // we can skip some unnecessary instance creation this way.
    if (input instanceof LanternPermissionService) {
        final LanternPermissionService service = (LanternPermissionService) input;
        final LanternSubjectCollection collection = service.get(ref.getSubjectCollectionIdentifier());
        subject = collection.get(ref.getIdentifier()).asSubjectReference();
    } else {
        // build a new subject reference using the permission service
        // this doesn't actually load the subject, so it will be lazily init'd when needed.
        subject = input.newSubjectReference(ref.getSubjectCollectionIdentifier(), ref.getIdentifier());
    }
    ref.setInternalSubject(subject);
    return true;
}
Also used : LanternPermissionService(org.lanternpowered.server.service.permission.LanternPermissionService) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) LanternSubjectCollection(org.lanternpowered.server.service.permission.base.LanternSubjectCollection)

Example 2 with SubjectReference

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

the class ListGroupTests method testPlayerNotInAnyGroupIsLeftOver.

@Test
@SuppressWarnings("all")
public void testPlayerNotInAnyGroupIsLeftOver() {
    // 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);
    Player player2 = Mockito.mock(Player.class);
    List<SubjectReference> lsr = getSubjectReferences(parents);
    Mockito.when(player.getParents()).thenReturn(lsr);
    Mockito.when(player.getParents(Mockito.anySet())).thenReturn(lsr);
    Mockito.when(player2.getParents()).thenReturn(Lists.newArrayList());
    Mockito.when(player2.getParents(Mockito.anySet())).thenReturn(Lists.newArrayList());
    // Create our map.
    Map<Player, List<String>> map = Maps.newHashMap();
    map.put(player, Lists.newArrayList("admin", "ace"));
    map.put(player2, Lists.newArrayList());
    // 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.assertEquals("One player should have been left over", 1, map.keySet().size());
}
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 3 with SubjectReference

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

the class ListGroupTests method testWeightsAreBeingAppliedCorrectlyWithReversedGroupsInList.

@Test
@SuppressWarnings("all")
public void testWeightsAreBeingAppliedCorrectlyWithReversedGroupsInList() {
    // Create two groups.
    Subject admin = createSubjectWithWeight("admin", 1);
    Subject ace = createSubjectWithWeight("ace", 0);
    // Order of groups to check for.
    List<Subject> order = Lists.newArrayList(admin, ace);
    // The player is in both groups.
    List<Subject> parents = Lists.newArrayList(ace, admin);
    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("ace", "admin"));
    // No aliases.
    Map<String, String> aliases = Maps.newHashMap();
    // Now, let's run it through our method.
    Map<String, List<Player>> result = ListPlayerCommand.linkPlayersToGroups(order, 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 4 with SubjectReference

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

the class ListGroupTests method createSubjectWithWeight.

private static Subject createSubjectWithWeight(String name, int weight, Subject... parents) {
    Subject subject = Mockito.mock(Subject.class);
    Mockito.when(subject.getIdentifier()).thenReturn(name);
    Mockito.when(subject.getOption(Mockito.anySetOf(Context.class), Mockito.eq("nucleus.list.weight"))).thenReturn(Optional.of(String.valueOf(weight)));
    Mockito.when(subject.getOption(Mockito.eq("nucleus.list.weight"))).thenReturn(Optional.of(String.valueOf(weight)));
    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 5 with SubjectReference

use of org.spongepowered.api.service.permission.SubjectReference in project SpongeCommon by SpongePowered.

the class SubjectSettingCallback method test.

@Override
public boolean test(@Nullable PermissionService input) {
    IMixinSubject ref = this.ref.get();
    if (ref == null) {
        // as a listener, and will not be tested again.
        return false;
    }
    // if PS has just been unregistered, ignore the change.
    if (input == null) {
        return true;
    }
    SubjectReference subject;
    // we can skip some unnecessary instance creation this way.
    if (input instanceof SpongePermissionService) {
        SpongePermissionService serv = (SpongePermissionService) input;
        SpongeSubjectCollection collection = serv.get(ref.getSubjectCollectionIdentifier());
        if (ref instanceof User && collection instanceof UserCollection) {
            // GameProfile is already resolved, use it directly
            subject = ((UserCollection) collection).get((GameProfile) ((User) ref).getProfile()).asSubjectReference();
        } else {
            subject = collection.get(((Subject) ref).getIdentifier()).asSubjectReference();
        }
    } else {
        // build a new subject reference using the permission service
        // this doesn't actually load the subject, so it will be lazily init'd when needed.
        subject = input.newSubjectReference(ref.getSubjectCollectionIdentifier(), ((Subject) ref).getIdentifier());
    }
    ref.setSubject(subject);
    return true;
}
Also used : User(org.spongepowered.api.entity.living.player.User) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) SpongeSubjectCollection(org.spongepowered.common.service.permission.base.SpongeSubjectCollection) IMixinSubject(org.spongepowered.common.interfaces.IMixinSubject) Subject(org.spongepowered.api.service.permission.Subject) MixinSubject(org.spongepowered.common.mixin.core.command.MixinSubject) IMixinSubject(org.spongepowered.common.interfaces.IMixinSubject)

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