use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.
the class ListGroupTests method testNonWeightedGroups.
@Test
public void testNonWeightedGroups() {
Subject subject1 = createSubject("subject1");
Subject subject2 = createSubject("subject2", subject1);
Subject subject3 = createSubject("subject3", subject1, subject2);
List<Subject> subjects = Lists.newArrayList(subject2, subject1, subject3);
List<Subject> sorted = subjects.stream().sorted((x, y) -> ListPlayerCommand.groupComparison(f -> 0, x, y)).collect(Collectors.toList());
Assert.assertEquals(subject3, sorted.get(0));
Assert.assertEquals(subject2, sorted.get(1));
Assert.assertEquals(subject1, sorted.get(2));
}
use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.
the class ListGroupTests method testTreeWeightedGroups.
@Test
public void testTreeWeightedGroups() {
Subject subject1 = createSubject("subject1");
Subject subject2 = createSubjectWithWeight("subject2", 4, subject1);
Subject subject2a = createSubjectWithWeight("subject2a", -1, subject1);
Subject subject3 = createSubject("subject3", subject1, subject2);
Subject subject4 = createSubjectWithWeight("subject4", 1, subject1, subject2);
Subject subject5 = createSubjectWithWeight("subject5", 2, subject1, subject2);
List<Subject> subjects = Lists.newArrayList(subject1, subject2, subject2a, subject3, subject4, subject5);
List<Subject> sorted = printWeights(subjects.stream().sorted((x, y) -> ListPlayerCommand.groupComparison(ListPlayerCommand.weightingFunction, x, y)).collect(Collectors.toList()));
List<Subject> expectedOrder = Lists.newArrayList(subject3, subject2, subject5, subject4, subject1, subject2a);
for (int i = 0; i < sorted.size(); i++) {
Assert.assertEquals("Index " + i + " is wrong! (expected: " + expectedOrder.get(i).getIdentifier() + ", got: " + sorted.get(i).getIdentifier() + ")", expectedOrder.get(i), sorted.get(i));
}
}
use of org.spongepowered.api.service.permission.Subject in project SpongeCommon by SpongePowered.
the class MixinDedicatedPlayerList method onCanJoin.
@Inject(method = "canJoin", at = @At("HEAD"), cancellable = true)
private void onCanJoin(GameProfile profile, CallbackInfoReturnable<Boolean> ci) {
if (!isWhiteListEnabled() || getWhitelistedPlayers().isWhitelisted(profile)) {
ci.setReturnValue(true);
return;
}
final PermissionService permissionService = Sponge.getServiceManager().provideUnchecked(PermissionService.class);
final Subject subject = permissionService.getUserSubjects().getSubject(profile.getId().toString()).orElse(permissionService.getDefaults());
ci.setReturnValue(subject.hasPermission(LoginPermissions.BYPASS_WHITELIST_PERMISSION));
}
use of org.spongepowered.api.service.permission.Subject in project SpongeCommon by SpongePowered.
the class MixinSubject method hasPermission.
@Override
public boolean hasPermission(Set<Context> contexts, String permission) {
Subject subj = resolveNullable();
if (subj == null) {
return permDefault(permission).asBoolean();
}
// these calls are not directly forwarded to the subject, so we can
// apply permission defaults.
Tristate ret = getPermissionValue(contexts, permission);
switch(ret) {
case UNDEFINED:
return permDefault(permission).asBoolean();
default:
return ret.asBoolean();
}
}
use of org.spongepowered.api.service.permission.Subject 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