Search in sources :

Example 1 with PermissionUserPkg

use of org.haiku.haikudepotserver.dataobjects.PermissionUserPkg in project haikudepotserver by haiku.

the class AuthorizationApiIT method testRemoveAuthorizationRule_permissionUserPkg.

@Test
public void testRemoveAuthorizationRule_permissionUserPkg() {
    integrationTestSupportService.createStandardTestData();
    setAuthenticatedUserToRoot();
    {
        ObjectContext context = serverRuntime.newContext();
        User user = integrationTestSupportService.createBasicUser(context, "testuser", "fakepassword");
        PermissionUserPkg permissionUserPkg = context.newObject(PermissionUserPkg.class);
        permissionUserPkg.setPermission(org.haiku.haikudepotserver.dataobjects.Permission.getByCode(context, Permission.PKG_EDITICON.name().toLowerCase()).get());
        permissionUserPkg.setUser(user);
        permissionUserPkg.setPkg(Pkg.tryGetByName(context, "pkg1").get());
        context.commitChanges();
    }
    RemoveAuthorizationPkgRuleRequest request = new RemoveAuthorizationPkgRuleRequest();
    request.userNickname = "testuser";
    request.permissionCode = Permission.PKG_EDITICON.name().toLowerCase();
    request.pkgName = "pkg1";
    // ------------------------------------
    authorizationApi.removeAuthorizationPkgRule(request);
    // ------------------------------------
    {
        ObjectContext context = serverRuntime.newContext();
        Assertions.assertThat(PermissionUserPkg.getByPermissionUserAndPkg(context, org.haiku.haikudepotserver.dataobjects.Permission.getByCode(context, Permission.PKG_EDITICON.name().toLowerCase()).get(), User.tryGetByNickname(context, "testuser").get(), Pkg.tryGetByName(context, "pkg1").get()).isPresent()).isFalse();
    }
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) RemoveAuthorizationPkgRuleRequest(org.haiku.haikudepotserver.api1.model.authorization.RemoveAuthorizationPkgRuleRequest) ObjectContext(org.apache.cayenne.ObjectContext) PermissionUserPkg(org.haiku.haikudepotserver.dataobjects.PermissionUserPkg) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with PermissionUserPkg

use of org.haiku.haikudepotserver.dataobjects.PermissionUserPkg in project haikudepotserver by haiku.

the class AuthorizationApiIT method createSearchAuthorizationRuleTestData.

private void createSearchAuthorizationRuleTestData() {
    ObjectContext context = serverRuntime.newContext();
    User user1 = integrationTestSupportService.createBasicUser(context, "testuser1", "fakepassword");
    User user2 = integrationTestSupportService.createBasicUser(context, "testuser2", "fakepassword");
    User user3 = integrationTestSupportService.createBasicUser(context, "testuser3", "fakepassword");
    Pkg pkg1 = Pkg.tryGetByName(context, "pkg1").get();
    Pkg pkg2 = Pkg.tryGetByName(context, "pkg2").get();
    org.haiku.haikudepotserver.dataobjects.Permission permission = org.haiku.haikudepotserver.dataobjects.Permission.getByCode(context, Permission.PKG_EDITICON.name().toLowerCase()).get();
    PermissionUserPkg pup_u1p1 = context.newObject(PermissionUserPkg.class);
    pup_u1p1.setPkg(pkg1);
    pup_u1p1.setUser(user1);
    pup_u1p1.setPermission(permission);
    PermissionUserPkg pup_u2p1 = context.newObject(PermissionUserPkg.class);
    pup_u2p1.setPkg(pkg1);
    pup_u2p1.setUser(user2);
    pup_u2p1.setPermission(permission);
    PermissionUserPkg pup_u3p1 = context.newObject(PermissionUserPkg.class);
    pup_u3p1.setPkg(pkg1);
    pup_u3p1.setUser(user3);
    pup_u3p1.setPermission(permission);
    PermissionUserPkg pup_u2p2 = context.newObject(PermissionUserPkg.class);
    pup_u2p2.setPkg(pkg2);
    pup_u2p2.setUser(user2);
    pup_u2p2.setPermission(permission);
    context.commitChanges();
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) PermissionUserPkg(org.haiku.haikudepotserver.dataobjects.PermissionUserPkg) PermissionUserPkg(org.haiku.haikudepotserver.dataobjects.PermissionUserPkg)

Example 3 with PermissionUserPkg

use of org.haiku.haikudepotserver.dataobjects.PermissionUserPkg in project haikudepotserver by haiku.

the class AuthorizationPkgRuleServiceImpl method create.

@Override
public AuthorizationPkgRule create(ObjectContext context, User user, Permission permission, Pkg pkg) {
    Preconditions.checkArgument(null != context, "the context must be provided");
    Preconditions.checkArgument(null != permission, "the permission must be provided");
    Preconditions.checkArgument(null != user, "the user must be provided");
    if (user.getIsRoot()) {
        throw new IllegalStateException("when creating an authorization rule, the rule is not able to be applied to a root user");
    }
    PermissionUserPkg rule = context.newObject(PermissionUserPkg.class);
    rule.setPermission(permission);
    user.addToManyTarget(User.PERMISSION_USER_PKGS.getName(), rule, true);
    rule.setPkg(pkg);
    LOGGER.info("did create permission user repository; {},{},{}", permission, user, pkg);
    return rule;
}
Also used : PermissionUserPkg(org.haiku.haikudepotserver.dataobjects.PermissionUserPkg)

Example 4 with PermissionUserPkg

use of org.haiku.haikudepotserver.dataobjects.PermissionUserPkg in project haikudepotserver by haiku.

the class AuthorizationRulesSpreadsheetJobRunner method run.

@Override
public void run(JobService jobService, AuthorizationRulesSpreadsheetJobSpecification specification) throws IOException, JobRunnerException {
    final ObjectContext context = serverRuntime.newContext();
    DateTimeFormatter dateTimeFormatter = DateTimeHelper.createStandardDateTimeFormat();
    // this will register the outbound data against the job.
    JobDataWithByteSink jobDataWithByteSink = jobService.storeGeneratedData(specification.getGuid(), "download", MediaType.CSV_UTF_8.toString());
    try (OutputStream outputStream = jobDataWithByteSink.getByteSink().openBufferedStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        CSVWriter writer = new CSVWriter(outputStreamWriter, ',')) {
        writer.writeNext(new String[] { "create-timestamp", "user-nickname", "user-active", "permission-code", "permission-name", "pkg-name" });
        ObjectSelect<PermissionUserPkg> objectSelect = ObjectSelect.query(PermissionUserPkg.class).orderBy(PermissionUserPkg.USER.dot(User.NICKNAME).asc(), PermissionUserPkg.PERMISSION.dot(Permission.CODE).asc());
        try (ResultBatchIterator<PermissionUserPkg> batchIterator = objectSelect.batchIterator(context, 50)) {
            batchIterator.forEach((pups) -> pups.forEach((pup) -> writer.writeNext(new String[] { dateTimeFormatter.format(Instant.ofEpochMilli(pup.getCreateTimestamp().getTime())), pup.getUser().getNickname(), Boolean.toString(pup.getUser().getActive()), pup.getPermission().getCode(), pup.getPermission().getName(), null != pup.getPkg() ? pup.getPkg().getName() : "" })));
        }
        writer.flush();
        outputStreamWriter.flush();
    }
}
Also used : JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) OutputStream(java.io.OutputStream) MediaType(com.google.common.net.MediaType) ObjectContext(org.apache.cayenne.ObjectContext) AbstractJobRunner(org.haiku.haikudepotserver.job.AbstractJobRunner) Resource(javax.annotation.Resource) CSVWriter(com.opencsv.CSVWriter) ResultBatchIterator(org.apache.cayenne.ResultBatchIterator) IOException(java.io.IOException) Instant(java.time.Instant) AuthorizationRulesSpreadsheetJobSpecification(org.haiku.haikudepotserver.security.model.AuthorizationRulesSpreadsheetJobSpecification) Component(org.springframework.stereotype.Component) Permission(org.haiku.haikudepotserver.dataobjects.Permission) DateTimeFormatter(java.time.format.DateTimeFormatter) PermissionUserPkg(org.haiku.haikudepotserver.dataobjects.PermissionUserPkg) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) OutputStreamWriter(java.io.OutputStreamWriter) Preconditions(com.google.common.base.Preconditions) ObjectSelect(org.apache.cayenne.query.ObjectSelect) User(org.haiku.haikudepotserver.dataobjects.User) JobService(org.haiku.haikudepotserver.job.model.JobService) JobRunnerException(org.haiku.haikudepotserver.job.model.JobRunnerException) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) DateTimeHelper(org.haiku.haikudepotserver.support.DateTimeHelper) OutputStream(java.io.OutputStream) CSVWriter(com.opencsv.CSVWriter) OutputStreamWriter(java.io.OutputStreamWriter) ObjectContext(org.apache.cayenne.ObjectContext) DateTimeFormatter(java.time.format.DateTimeFormatter) PermissionUserPkg(org.haiku.haikudepotserver.dataobjects.PermissionUserPkg)

Example 5 with PermissionUserPkg

use of org.haiku.haikudepotserver.dataobjects.PermissionUserPkg in project haikudepotserver by haiku.

the class AuthorizationApiIT method checkAuthorizationRequest_asUserWithRule.

/**
 * <P>With a user-pkg rule in place we should see this authorization come through in a check
 * for that permission against the package being true.</P>
 */
@Test
public void checkAuthorizationRequest_asUserWithRule() {
    integrationTestSupportService.createStandardTestData();
    {
        ObjectContext context = serverRuntime.newContext();
        User user1 = integrationTestSupportService.createBasicUser(context, "testuser1", "fakepassword");
        integrationTestSupportService.agreeToUserUsageConditions(context, user1);
        Pkg pkg1 = Pkg.tryGetByName(context, "pkg1").get();
        org.haiku.haikudepotserver.dataobjects.Permission permission = org.haiku.haikudepotserver.dataobjects.Permission.getByCode(context, Permission.PKG_EDITICON.name().toLowerCase()).get();
        PermissionUserPkg pup_u1p1 = context.newObject(PermissionUserPkg.class);
        pup_u1p1.setPkg(pkg1);
        pup_u1p1.setUser(user1);
        pup_u1p1.setPermission(permission);
        context.commitChanges();
    }
    CheckAuthorizationRequest request = new CheckAuthorizationRequest();
    request.targetAndPermissions = new ArrayList<>();
    request.targetAndPermissions.add(new CheckAuthorizationRequest.AuthorizationTargetAndPermission(AuthorizationTargetType.PKG, "pkg1", Permission.PKG_EDITICON.name()));
    setAuthenticatedUser("testuser1");
    // ------------------------------------
    CheckAuthorizationResult result = authorizationApi.checkAuthorization(request);
    // ------------------------------------
    Assertions.assertThat(result.targetAndPermissions.size()).isEqualTo(1);
    Assertions.assertThat(result.targetAndPermissions.get(0).authorized).isTrue();
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) Permission(org.haiku.haikudepotserver.security.model.Permission) CheckAuthorizationResult(org.haiku.haikudepotserver.api1.model.authorization.CheckAuthorizationResult) ObjectContext(org.apache.cayenne.ObjectContext) CheckAuthorizationRequest(org.haiku.haikudepotserver.api1.model.authorization.CheckAuthorizationRequest) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) PermissionUserPkg(org.haiku.haikudepotserver.dataobjects.PermissionUserPkg) PermissionUserPkg(org.haiku.haikudepotserver.dataobjects.PermissionUserPkg) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

PermissionUserPkg (org.haiku.haikudepotserver.dataobjects.PermissionUserPkg)5 ObjectContext (org.apache.cayenne.ObjectContext)4 User (org.haiku.haikudepotserver.dataobjects.User)4 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)2 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)2 Test (org.junit.jupiter.api.Test)2 Preconditions (com.google.common.base.Preconditions)1 MediaType (com.google.common.net.MediaType)1 CSVWriter (com.opencsv.CSVWriter)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Instant (java.time.Instant)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Resource (javax.annotation.Resource)1 ResultBatchIterator (org.apache.cayenne.ResultBatchIterator)1 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)1 ObjectSelect (org.apache.cayenne.query.ObjectSelect)1 CheckAuthorizationRequest (org.haiku.haikudepotserver.api1.model.authorization.CheckAuthorizationRequest)1 CheckAuthorizationResult (org.haiku.haikudepotserver.api1.model.authorization.CheckAuthorizationResult)1