Search in sources :

Example 16 with User

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

the class AuthorizationApiImpl method createAuthorizationPkgRule.

@Override
public CreateAuthorizationPkgRuleResult createAuthorizationPkgRule(CreateAuthorizationPkgRuleRequest request) throws AuthorizationRuleConflictException {
    Preconditions.checkNotNull(request);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.permissionCode), "the permission code is required");
    Preconditions.checkState(org.haiku.haikudepotserver.security.model.Permission.valueOf(request.permissionCode.toUpperCase()).getRequiredTargetType() == TargetType.PKG, "the permission should have a target type of; " + TargetType.PKG);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.userNickname), "the user nickname must be supplied");
    ensureCanAuthorizationManipulate();
    ObjectContext context = serverRuntime.newContext();
    org.haiku.haikudepotserver.dataobjects.Permission permission = ensurePermission(context, request.permissionCode);
    User user = ensureUser(context, request.userNickname);
    if (user.getIsRoot()) {
        throw new ValidationException(new ValidationFailure("user", "root"));
    }
    Pkg pkg = null;
    if (null != request.pkgName) {
        pkg = ensurePkg(context, request.pkgName);
    }
    if (authorizationPkgRulesService.wouldConflict(context, user, permission, pkg)) {
        throw new AuthorizationRuleConflictException();
    }
    authorizationPkgRulesService.create(context, user, permission, pkg);
    context.commitChanges();
    return new CreateAuthorizationPkgRuleResult();
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) ObjectContext(org.apache.cayenne.ObjectContext) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 17 with User

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

the class AuthorizationApiImpl method removeAuthorizationPkgRule.

@Override
public RemoveAuthorizationPkgRuleResult removeAuthorizationPkgRule(RemoveAuthorizationPkgRuleRequest request) {
    Preconditions.checkNotNull(request);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.permissionCode), "the permission code is required");
    Preconditions.checkState(!Strings.isNullOrEmpty(request.userNickname), "the user nickname is required");
    ensureCanAuthorizationManipulate();
    ObjectContext context = serverRuntime.newContext();
    org.haiku.haikudepotserver.dataobjects.Permission permission = ensurePermission(context, request.permissionCode);
    User user = null;
    if (null != request.userNickname) {
        user = ensureUser(context, request.userNickname);
    }
    Pkg pkg = null;
    if (!Strings.isNullOrEmpty(request.pkgName)) {
        pkg = ensurePkg(context, request.pkgName);
    }
    authorizationPkgRulesService.remove(context, user, permission, pkg);
    context.commitChanges();
    return new RemoveAuthorizationPkgRuleResult();
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg)

Example 18 with User

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

the class AuthorizationJobApiImpl method queueAuthorizationRulesSpreadsheet.

@Override
public QueueAuthorizationRulesSpreadsheetResult queueAuthorizationRulesSpreadsheet(QueueAuthorizationRulesSpreadsheetRequest request) {
    Preconditions.checkArgument(null != request, "a request objects is required");
    final ObjectContext context = serverRuntime.newContext();
    User user = obtainAuthenticatedUser(context);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.AUTHORIZATION_CONFIGURE)) {
        String msg = "attempt to queue authorization spreadsheet without sufficient authorization";
        LOGGER.warn(msg);
        throw new AccessDeniedException(msg);
    }
    QueueAuthorizationRulesSpreadsheetResult result = new QueueAuthorizationRulesSpreadsheetResult();
    result.guid = jobService.submit(new AuthorizationRulesSpreadsheetJobSpecification(), JobSnapshot.COALESCE_STATUSES_QUEUED_STARTED);
    return result;
}
Also used : QueueAuthorizationRulesSpreadsheetResult(org.haiku.haikudepotserver.api1.model.authorization.job.QueueAuthorizationRulesSpreadsheetResult) AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) AuthorizationRulesSpreadsheetJobSpecification(org.haiku.haikudepotserver.security.model.AuthorizationRulesSpreadsheetJobSpecification) ObjectContext(org.apache.cayenne.ObjectContext)

Example 19 with User

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

the class UserApiIT method testCreateUser.

@Test
public void testCreateUser() {
    Captcha captcha = captchaService.generate();
    CreateUserRequest request = new CreateUserRequest();
    request.captchaToken = captcha.getToken();
    request.captchaResponse = captcha.getResponse();
    request.nickname = "testuser";
    request.passwordClear = "Ue4nI92Rw";
    request.naturalLanguageCode = "en";
    request.userUsageConditionsCode = "UUC2021V01";
    // ------------------------------------
    CreateUserResult result = userApi.createUser(request);
    // ------------------------------------
    Assertions.assertThat(result).isNotNull();
    ObjectContext context = serverRuntime.newContext();
    Optional<User> userOptional = User.tryGetByNickname(context, "testuser");
    Assertions.assertThat(userOptional.isPresent()).isTrue();
    User user = userOptional.get();
    Assertions.assertThat(user.getActive()).isTrue();
    Assertions.assertThat(user.getIsRoot()).isFalse();
    Assertions.assertThat(user.getNickname()).isEqualTo("testuser");
    Assertions.assertThat(user.getNaturalLanguage().getCode()).isEqualTo("en");
    Assertions.assertThat(user.getLastAuthenticationTimestamp()).isNull();
    Assertions.assertThat(user.tryGetUserUsageConditionsAgreement().get().getUserUsageConditions().getCode()).isEqualTo("UUC2021V01");
    Assertions.assertThat(userAuthenticationService.authenticateByNicknameAndPassword("testuser", "Ue4nI92Rw").get()).isEqualTo(userOptional.get().getObjectId());
}
Also used : Captcha(org.haiku.haikudepotserver.captcha.model.Captcha) User(org.haiku.haikudepotserver.dataobjects.User) CreateUserResult(org.haiku.haikudepotserver.api1.model.user.CreateUserResult) CreateUserRequest(org.haiku.haikudepotserver.api1.model.user.CreateUserRequest) ObjectContext(org.apache.cayenne.ObjectContext) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 20 with User

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

the class UserApiIT method testRenewToken.

@Test
public void testRenewToken() {
    String token;
    ObjectId userOid;
    {
        ObjectContext context = serverRuntime.newContext();
        User user = integrationTestSupportService.createBasicUser(context, "testuser", "U7vqpsu6BB");
        userOid = user.getObjectId();
        token = userAuthenticationService.generateToken(user);
    }
    RenewTokenRequest renewTokenRequest = new RenewTokenRequest();
    renewTokenRequest.token = token;
    // ------------------------------------
    RenewTokenResult result = userApi.renewToken(renewTokenRequest);
    // ------------------------------------
    {
        Optional<ObjectId> afterUserObjectId = userAuthenticationService.authenticateByToken(result.token);
        Assertions.assertThat(userOid).isEqualTo(afterUserObjectId.get());
    }
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) Optional(java.util.Optional) ObjectId(org.apache.cayenne.ObjectId) RenewTokenRequest(org.haiku.haikudepotserver.api1.model.user.RenewTokenRequest) RenewTokenResult(org.haiku.haikudepotserver.api1.model.user.RenewTokenResult) ObjectContext(org.apache.cayenne.ObjectContext) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

User (org.haiku.haikudepotserver.dataobjects.User)51 ObjectContext (org.apache.cayenne.ObjectContext)47 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)16 Test (org.junit.jupiter.api.Test)16 AccessDeniedException (org.springframework.security.access.AccessDeniedException)14 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)7 ObjectId (org.apache.cayenne.ObjectId)5 ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)5 org.haiku.haikudepotserver.dataobjects.auto._User (org.haiku.haikudepotserver.dataobjects.auto._User)5 PermissionUserPkg (org.haiku.haikudepotserver.dataobjects.PermissionUserPkg)4 AuthenticateUserRequest (org.haiku.haikudepotserver.api1.model.user.AuthenticateUserRequest)3 AuthenticateUserResult (org.haiku.haikudepotserver.api1.model.user.AuthenticateUserResult)3 Captcha (org.haiku.haikudepotserver.captcha.model.Captcha)3 Preconditions (com.google.common.base.Preconditions)2 SignedJWT (com.nimbusds.jwt.SignedJWT)2 CSVWriter (com.opencsv.CSVWriter)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Instant (java.time.Instant)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2