Search in sources :

Example 16 with Subject

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));
}
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 17 with Subject

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));
    }
}
Also used : Subject(org.spongepowered.api.service.permission.Subject) Test(org.junit.Test)

Example 18 with Subject

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));
}
Also used : SpongePermissionService(org.spongepowered.common.service.permission.SpongePermissionService) PermissionService(org.spongepowered.api.service.permission.PermissionService) Subject(org.spongepowered.api.service.permission.Subject) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 19 with Subject

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();
    }
}
Also used : Tristate(org.spongepowered.api.util.Tristate) Subject(org.spongepowered.api.service.permission.Subject) IMixinSubject(org.spongepowered.common.interfaces.IMixinSubject)

Example 20 with Subject

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;
}
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

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