use of org.bimserver.models.store.ServerSettings in project BIMserver by opensourceBIM.
the class SettingsServiceImpl method setProtocolBuffersPort.
@Override
public void setProtocolBuffersPort(final Integer port) throws ServerException, UserException {
requireAdminAuthenticationAndRunningServer();
DatabaseSession session = getBimServer().getDatabase().createSession();
try {
SetServerSettingDatabaseAction action = new SetServerSettingDatabaseAction(getBimServer(), session, getInternalAccessMethod(), new ServerSettingsSetter() {
@Override
public void set(ServerSettings serverSettings) {
serverSettings.setProtocolBuffersPort(port);
}
});
session.executeAndCommitAction(action);
} catch (BimserverDatabaseException e) {
handleException(e);
} finally {
session.close();
}
}
use of org.bimserver.models.store.ServerSettings in project BIMserver by opensourceBIM.
the class SettingsServiceImpl method setCheckinMergingEnabled.
@Override
public void setCheckinMergingEnabled(final Boolean checkinMergingEnabled) throws ServerException, UserException {
requireAdminAuthenticationAndRunningServer();
DatabaseSession session = getBimServer().getDatabase().createSession();
try {
SetServerSettingDatabaseAction action = new SetServerSettingDatabaseAction(getBimServer(), session, getInternalAccessMethod(), new ServerSettingsSetter() {
@Override
public void set(ServerSettings serverSettings) {
serverSettings.setCheckinMergingEnabled(checkinMergingEnabled);
}
});
session.executeAndCommitAction(action);
} catch (BimserverDatabaseException e) {
handleException(e);
} finally {
session.close();
}
}
use of org.bimserver.models.store.ServerSettings in project BIMserver by opensourceBIM.
the class AddUserDatabaseAction method execute.
public User execute() throws UserException, BimserverDatabaseException, BimserverLockConflictException {
String trimmedUserName = username.trim().toLowerCase();
String trimmedName = name.trim();
if (userType == UserType.SYSTEM && !createSystemUser) {
throw new UserException("Cannot create system users");
}
if (selfRegistration && userType == UserType.ADMIN) {
throw new UserException("Cannot create admin user with self registration");
}
if (trimmedUserName.equals("")) {
throw new UserException("Invalid username");
}
if (!MailSystem.isValidEmailAddress(trimmedUserName) && !(trimmedUserName.equals("test") || trimmedUserName.equals("system"))) {
throw new UserException("Username must be a valid e-mail address");
}
if (trimmedName.equals("")) {
throw new UserException("Invalid name");
}
if (getUserByUserName(trimmedUserName) != null) {
throw new UserException("A user with the username " + trimmedUserName + " already exists");
}
User actingUser = null;
// if (bimServer.getServerSettingsCache() != null && !bimServer.getServerSettingsCache().getServerSettings().isAllowCreateValidatedUser()) {
// if (authorization != null && !(authorization instanceof SystemAuthorization)) {
// actingUser = getUserByUoid(authorization.getUoid());
// if (actingUser == null || actingUser.getUserType() != UserType.SYSTEM) {
// if (authorization.getUoid() != -1 && actingUser.getUserType() != UserType.ADMIN) {
// throw new UserException("Only admin users can create other users");
// }
// }
// }
// }
final User user = getDatabaseSession().create(User.class);
if (password != null) {
byte[] salt = new byte[32];
secureRandom.nextBytes(salt);
user.setPasswordHash(new Authenticator().createHash(password, salt));
user.setPasswordSalt(salt);
}
user.setToken(GeneratorUtils.generateToken());
user.setName(trimmedName);
user.setUsername(trimmedUserName);
user.setCreatedOn(new Date());
user.setCreatedBy(actingUser);
user.setUserType(userType);
user.setLastSeen(null);
final String token = GeneratorUtils.generateToken();
user.setValidationToken(Hashers.getSha256Hash(token));
user.setValidationTokenCreated(new Date());
if (!createSystemUser) {
final NewUserAdded newUserAdded = getDatabaseSession().create(NewUserAdded.class);
newUserAdded.setUser(user);
newUserAdded.setExecutor(actingUser);
newUserAdded.setDate(new Date());
newUserAdded.setAccessMethod(getAccessMethod());
getDatabaseSession().store(newUserAdded);
getDatabaseSession().addPostCommitAction(new PostCommitAction() {
@Override
public void execute() throws UserException {
bimServer.getNotificationsManager().notify(new NewUserNotification(bimServer, user.getOid()));
}
});
bimServer.updateUserSettings(getDatabaseSession(), user);
}
getDatabaseSession().store(user);
if (bimServer != null && bimServer.getServerSettingsCache() != null) {
// this is only null on server/database initialization
final ServerSettings serverSettings = bimServer.getServerSettingsCache().getServerSettings();
if (serverSettings.isSendConfirmationEmailAfterRegistration()) {
getDatabaseSession().addPostCommitAction(new PostCommitAction() {
@Override
public void execute() throws UserException {
String body = null;
try {
if (MailSystem.isValidEmailAddress(user.getUsername())) {
EmailMessage message = bimServer.getMailSystem().createMessage();
String emailSenderAddress = serverSettings.getEmailSenderAddress();
InternetAddress addressFrom = new InternetAddress(emailSenderAddress);
message.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[1];
addressTo[0] = new InternetAddress(user.getUsername());
message.setRecipients(Message.RecipientType.TO, addressTo);
Map<String, Object> context = new HashMap<String, Object>();
context.put("name", user.getName());
context.put("username", user.getUsername());
context.put("siteaddress", serverSettings.getSiteAddress());
context.put("validationlink", resetUrl + "&username=" + user.getUsername() + "&uoid=" + user.getOid() + "&validationtoken=" + token + "&address=" + bimServer.getServerSettingsCache().getServerSettings().getSiteAddress());
String subject = null;
if (selfRegistration) {
body = bimServer.getTemplateEngine().process(context, TemplateIdentifier.SELF_REGISTRATION_EMAIL_BODY);
subject = bimServer.getTemplateEngine().process(context, TemplateIdentifier.SELF_REGISTRATION_EMAIL_SUBJECT);
} else {
body = bimServer.getTemplateEngine().process(context, TemplateIdentifier.ADMIN_REGISTRATION_EMAIL_BODY);
subject = bimServer.getTemplateEngine().process(context, TemplateIdentifier.ADMIN_REGISTRATION_EMAIL_SUBJECT);
}
message.setContent(body, "text/html");
message.setSubject(subject.trim());
LOGGER.info("Sending registration e-mail to " + user.getUsername());
message.send();
}
} catch (Exception e) {
LOGGER.error(body);
LOGGER.error("", e);
throw new UserException(e);
}
}
});
}
}
return user;
}
use of org.bimserver.models.store.ServerSettings in project BIMserver by opensourceBIM.
the class EmailMessage method send.
public void send() throws MessagingException, UserException {
Properties props = new Properties();
ServerSettings serverSettings = bimServer.getServerSettingsCache().getServerSettings();
props.put("mail.smtp.localhost", "bimserver.org");
String smtpProps = serverSettings.getSmtpProtocol() == SmtpProtocol.SMTPS ? "mail.smtps.port" : "mail.smtp.port";
props.put("mail.smtp.connectiontimeout", 10000);
props.put("mail.smtp.timeout", 10000);
props.put("mail.smtp.writetimeout", 10000);
props.put(smtpProps, serverSettings.getSmtpPort());
if (serverSettings.getSmtpProtocol() == SmtpProtocol.STARTTLS) {
props.put("mail.smtp.starttls.enable", "true");
}
Session mailSession = Session.getInstance(props);
Transport transport = null;
try {
if (serverSettings.getSmtpProtocol() == SmtpProtocol.SMTP) {
transport = new SMTPTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else if (serverSettings.getSmtpProtocol() == SmtpProtocol.SMTPS) {
transport = new SMTPSSLTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else if (serverSettings.getSmtpProtocol() == SmtpProtocol.STARTTLS) {
transport = new SMTPSSLTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else {
throw new RuntimeException("Unimplemented SMTP protocol: " + serverSettings.getSmtpProtocol());
}
transport.connect(serverSettings.getSmtpServer(), serverSettings.getSmtpPort(), serverSettings.getSmtpUsername(), serverSettings.getSmtpPassword());
Message message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setRecipients(to, addressTo);
message.setContent(body, contentType);
message.setFrom(from);
transport.sendMessage(message, addressTo);
} catch (MessagingException e) {
LOGGER.error("Error sending email " + body + " " + e.getMessage());
throw new UserException("Error sending email " + e.getMessage());
}
}
use of org.bimserver.models.store.ServerSettings in project BIMserver by opensourceBIM.
the class NewRevisionNotification method sendEmail.
private void sendEmail(DatabaseSession session, Project project, Revision revision) throws UserException {
Set<User> users = getUsers(session, project);
for (User user : users) {
String body = null;
try {
if (MailSystem.isValidEmailAddress(user.getUsername())) {
EmailMessage message = getBimServer().getMailSystem().createMessage();
ServerSettings serverSettings = getBimServer().getServerSettingsCache().getServerSettings();
String emailSenderAddress = serverSettings.getEmailSenderAddress();
InternetAddress addressFrom = new InternetAddress(emailSenderAddress);
message.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[1];
addressTo[0] = new InternetAddress(user.getUsername());
message.setRecipients(Message.RecipientType.TO, addressTo);
Map<String, Object> context = new HashMap<String, Object>();
context.put("name", user.getName());
context.put("username", user.getUsername());
context.put("siteaddress", serverSettings.getSiteAddress());
context.put("revisionId", revision.getId());
Authorization authorization = null;
if (user.getUserType() == UserType.ADMIN) {
authorization = new AdminAuthorization(getBimServer().getServerSettingsCache().getServerSettings().getSessionTimeOutSeconds(), TimeUnit.SECONDS);
} else {
authorization = new UserAuthorization(getBimServer().getServerSettingsCache().getServerSettings().getSessionTimeOutSeconds(), TimeUnit.SECONDS);
}
authorization.setUoid(user.getOid());
String asHexToken = authorization.asHexToken(getBimServer().getEncryptionKey());
context.put("token", asHexToken);
context.put("roid", revision.getOid());
context.put("comment", revision.getComment());
context.put("projectName", project.getName());
String subject = null;
body = getBimServer().getTemplateEngine().process(context, TemplateIdentifier.NEW_REVISION_EMAIL_BODY);
subject = getBimServer().getTemplateEngine().process(context, TemplateIdentifier.NEW_REVISION_EMAIL_SUBJECT);
message.setContent(body, "text/html");
message.setSubject(subject.trim());
LOGGER.info("Sending new revision e-mail to " + user.getUsername());
message.send();
}
} catch (Exception e) {
LOGGER.error(body);
LOGGER.error("", e);
throw new UserException(e);
}
}
}
Aggregations