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;
}
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());
}
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());
}
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;
}
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;
}
Aggregations