Search in sources :

Example 11 with User

use of org.haiku.haikudepotserver.dataobjects.User 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 12 with User

use of org.haiku.haikudepotserver.dataobjects.User 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 13 with User

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

the class PasswordResetServiceImpl method createTokenAndInvite.

/**
 * <p>This method will create a user password reset token and will also email the user
 * about it so that they are able to click on a link in the email, visit the application
 * server and get their password changed.</p>
 */
private void createTokenAndInvite(User user) throws PasswordResetException {
    Preconditions.checkArgument(null != user, "the user must be provided");
    Preconditions.checkState(!Strings.isNullOrEmpty(user.getEmail()), "the user must have an email configured");
    ObjectContext contextLocal = serverRuntime.newContext();
    User userLocal = User.getByObjectId(contextLocal, user.getObjectId());
    UserPasswordResetToken userPasswordResetToken = contextLocal.newObject(UserPasswordResetToken.class);
    userPasswordResetToken.setUser(userLocal);
    userPasswordResetToken.setCode(UUID.randomUUID().toString());
    userPasswordResetToken.setCreateTimestamp(new java.sql.Timestamp(Clock.systemUTC().millis()));
    PasswordResetMail mailModel = new PasswordResetMail();
    mailModel.setPasswordResetBaseUrl(baseUrl + "/" + URL_SEGMENT_PASSWORDRESET + "/");
    mailModel.setUserNickname(user.getNickname());
    mailModel.setUserPasswordResetTokenCode(userPasswordResetToken.getCode());
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo(user.getEmail());
    message.setSubject(fillFreemarkerTemplate(mailModel, MAIL_SUBJECT, user.getNaturalLanguage()));
    message.setText(fillFreemarkerTemplate(mailModel, MAIL_PLAINTEXT, user.getNaturalLanguage()));
    contextLocal.commitChanges();
    try {
        this.mailSender.send(message);
    } catch (MailException me) {
        throw new PasswordResetException("the password reset email to " + user.toString() + " was not able to be sent", me);
    }
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) UserPasswordResetToken(org.haiku.haikudepotserver.dataobjects.UserPasswordResetToken) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) PasswordResetMail(org.haiku.haikudepotserver.passwordreset.model.PasswordResetMail) ObjectContext(org.apache.cayenne.ObjectContext) MailException(org.springframework.mail.MailException)

Example 14 with User

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

the class PasswordResetServiceImpl method initiate.

@Override
public void initiate(String email) throws PasswordResetException {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(email), "the email must be provided");
    // very basic sanity check
    Preconditions.checkArgument(-1 != email.indexOf('@'), "the email is malformed");
    ObjectContext context = serverRuntime.newContext();
    List<User> users = User.findByEmail(context, email);
    if (users.isEmpty()) {
        LOGGER.warn("attempt to send password reset token to {}, but there are no users associated with this email address", email);
    } else {
        int count = 0;
        LOGGER.info("will create tokens and invite; {}", email);
        for (User user : users) {
            if (!user.getActive()) {
                LOGGER.warn("it is not possible to send a password reset to an inactive user; {}", user.toString());
            } else {
                if (user.getIsRoot()) {
                    LOGGER.warn("it is not possible to send a password reset to a root user; {}", user.toString());
                } else {
                    createTokenAndInvite(user);
                    count++;
                }
            }
        }
        LOGGER.info("did create tokens and invite; {} - sent {}", email, count);
    }
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext)

Example 15 with User

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

the class AbstractIntegrationTest method clearDatabaseTables.

private void clearDatabaseTables() {
    for (DataNode dataNode : serverRuntime.getDataDomain().getDataNodes()) {
        LOGGER.debug("prep; will clear out data for data node; {}", dataNode.getName());
        try (Connection connection = dataNode.getDataSource().getConnection()) {
            connection.setAutoCommit(false);
            connection.rollback();
            String databaseProductName = connection.getMetaData().getDatabaseProductName();
            if (!databaseProductName.equals(DATABASEPRODUCTNAME_POSTGRES)) {
                throw new IllegalStateException(String.format("the system is designed to be tested against %s database product, but is '%s'", DATABASEPRODUCTNAME_POSTGRES, databaseProductName));
            }
            if (!getDatabaseName(connection).endsWith("_integrationtest")) {
                throw new IllegalStateException("unable to proceed with integration tests against a database which is not an integration test database");
            }
            for (DataMap dataMap : dataNode.getDataMaps()) {
                List<String> truncationNames = new ArrayList<>();
                for (ObjEntity objEntity : dataMap.getObjEntities()) {
                    if (!objEntity.isReadOnly() && !CDO_NAMES_RETAINED.contains(objEntity.getName())) {
                        truncationNames.add(objEntity.getDbEntity().getSchema() + "." + objEntity.getDbEntity().getName());
                    }
                }
                if (!truncationNames.isEmpty()) {
                    String sql = String.format("TRUNCATE %s CASCADE", String.join(",", truncationNames));
                    try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
                        preparedStatement.execute();
                    }
                }
            }
            // special case for the root user because we want to leave the root user in-situ
            {
                DbEntity userDbEntity = serverRuntime.getDataDomain().getEntityResolver().getObjEntity(User.class.getSimpleName()).getDbEntity();
                String sql = String.format("DELETE FROM %s.%s WHERE nickname <> 'root'", userDbEntity.getSchema(), userDbEntity.getName());
                try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
                    preparedStatement.execute();
                }
            }
            connection.commit();
        } catch (SQLException se) {
            throw new RuntimeException("unable to clear the data for the data node; " + dataNode.getName(), se);
        }
        LOGGER.debug("prep; did clear out data for data node; {}", dataNode.getName());
    }
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DataMap(org.apache.cayenne.map.DataMap) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) DataNode(org.apache.cayenne.access.DataNode)

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