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