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