use of org.bimserver.shared.exceptions.UserException 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.shared.exceptions.UserException in project BIMserver by opensourceBIM.
the class AutologinDatabaseAction method execute.
@Override
public String execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
try {
Authorization authorization = Authorization.fromToken(bimServer.getEncryptionKey(), token);
User user = getDatabaseSession().get(authorization.getUoid(), OldQuery.getDefault());
if (user.getState() == ObjectState.DELETED) {
throw new UserException("User account has been deleted");
} else if (user.getUserType() == UserType.SYSTEM) {
throw new UserException("System user cannot login");
}
if (bimServer.getServerSettingsCache().getServerSettings().isStoreLastLogin()) {
user.setLastSeen(new Date());
getDatabaseSession().store(user);
}
authorization.setUoid(user.getOid());
String asHexToken = authorization.asHexToken(bimServer.getEncryptionKey());
serviceMap.setAuthorization(authorization);
return asHexToken;
} catch (AuthenticationException e) {
LOGGER.error("", e);
}
try {
// Adding a random sleep to prevent timing attacks
Thread.sleep(LoginDatabaseAction.DEFAULT_LOGIN_ERROR_TIMEOUT + new java.security.SecureRandom().nextInt(1000));
} catch (InterruptedException e) {
LOGGER.error("", e);
}
throw new UserException("User not found or inccorrect autologin token");
}
use of org.bimserver.shared.exceptions.UserException in project BIMserver by opensourceBIM.
the class BranchToExistingProjectDatabaseAction method execute.
@Override
public ConcreteRevision execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
Revision oldRevision = getDatabaseSession().get(StorePackage.eINSTANCE.getRevision(), roid, OldQuery.getDefault());
Project oldProject = oldRevision.getProject();
User user = getDatabaseSession().get(StorePackage.eINSTANCE.getUser(), authorization.getUoid(), OldQuery.getDefault());
if (!authorization.hasRightsOnProjectOrSuperProjectsOrSubProjects(user, oldProject)) {
throw new UserException("User has insufficient rights to download revisions from this project");
}
IfcModelSet ifcModelSet = new IfcModelSet();
PackageMetaData lastMetaData = null;
for (ConcreteRevision subRevision : oldRevision.getConcreteRevisions()) {
PackageMetaData packageMetaData = bimServer.getMetaDataManager().getPackageMetaData(subRevision.getProject().getSchema());
IfcModel subModel = new BasicIfcModel(packageMetaData, null);
getDatabaseSession().getMap(subModel, new OldQuery(packageMetaData, subRevision.getProject().getId(), subRevision.getId(), -1, Deep.YES));
subModel.getModelMetaData().setDate(subRevision.getDate());
ifcModelSet.add(subModel);
lastMetaData = packageMetaData;
}
IfcModelInterface model = new BasicIfcModel(lastMetaData, null);
try {
model = bimServer.getMergerFactory().createMerger(getDatabaseSession(), authorization.getUoid()).merge(oldRevision.getProject(), ifcModelSet, new ModelHelper(bimServer.getMetaDataManager(), model));
} catch (MergeException e) {
throw new UserException(e);
}
model.resetOids();
CheckinDatabaseAction checkinDatabaseAction = new CheckinDatabaseAction(bimServer, getDatabaseSession(), getAccessMethod(), destPoid, authorization, model, comment, comment, false, -1);
return checkinDatabaseAction.execute();
}
use of org.bimserver.shared.exceptions.UserException in project BIMserver by opensourceBIM.
the class ChangePasswordDatabaseAction method execute.
@Override
public Boolean execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
User actingUser = getUserByUoid(authorization.getUoid());
User user = getUserByUoid(uoid);
if (user.getUserType() == UserType.SYSTEM) {
throw new UserException("Password of system user cannot be changed");
}
if (uoid == authorization.getUoid()) {
return changePassword(getDatabaseSession(), actingUser, false);
} else {
if (actingUser.getUserType() == UserType.ADMIN || actingUser.getUserType() == UserType.SYSTEM) {
return changePassword(getDatabaseSession(), actingUser, true);
} else {
throw new UserException("Insufficient rights to change the password of this user");
}
}
}
use of org.bimserver.shared.exceptions.UserException in project BIMserver by opensourceBIM.
the class ChangeUserTypeDatabaseAction method execute.
@Override
public Void execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
User actingUser = getUserByUoid(authorization.getUoid());
if (actingUser.getUserType() != UserType.ADMIN) {
throw new UserException("Only admin users can change other user's types");
}
User user = getUserByUoid(uoid);
if (user.getUserType() == UserType.SYSTEM) {
throw new UserException("Type of system user cannot be changed");
}
user.setUserType(UserType.get(userType.getOrdinal()));
final UserChanged userChanged = getDatabaseSession().create(UserChanged.class);
userChanged.setAccessMethod(getAccessMethod());
userChanged.setDate(new Date());
userChanged.setExecutor(actingUser);
userChanged.setUser(user);
getDatabaseSession().addPostCommitAction(new PostCommitAction() {
@Override
public void execute() throws UserException {
bimServer.getNotificationsManager().notify(new SConverter().convertToSObject(userChanged));
}
});
getDatabaseSession().store(user);
return null;
}
Aggregations