use of com.peterphi.usermanager.db.entity.UserEntity in project stdlib by petergeneric.
the class UserUIServiceImpl method getUserEdit.
@Override
@Transactional(readOnly = true)
public String getUserEdit(final int userId) {
final int localUser = login.getId();
if (localUser != userId && !login.isAdmin())
throw new AuthenticationFailureException("Only a User Admin can edit the profile of another user!");
TemplateCall call = templater.template("user_edit");
final UserEntity user = accountDao.getById(userId);
call.set("entity", user);
call.set("user", user);
call.set("timezones", Arrays.asList(TimeZone.getAvailableIDs()));
call.set("dateformats", Arrays.asList("YYYY-MM-dd HH:mm:ss zzz", "YYYY-MM-dd HH:mm:ss", "YYYY-MM-dd HH:mm"));
call.set("entityRoleIds", getRoles(user));
call.set("roles", roleDao.getAll());
call.set("nonce", nonceStore.getValue(NONCE_USE));
return call.process();
}
use of com.peterphi.usermanager.db.entity.UserEntity in project stdlib by petergeneric.
the class UserUIServiceImpl method editUserProfile.
@Override
@Transactional
public Response editUserProfile(final int userId, final String nonce, final String dateFormat, final String timeZone, final String name, final String email, final List<String> roles) {
nonceStore.validate(NONCE_USE, nonce);
final int localUser = login.getId();
if (localUser != userId && !login.isAdmin())
throw new AuthenticationFailureException("Only a User Admin can edit the profile of another user!");
// Change regular account settings
final UserEntity user = accountDao.changeProfile(userId, name, email, dateFormat, timeZone);
// Change roles (if we're admin and the user is local)
if (login.isAdmin() && user.isLocal()) {
final Set<String> currentRoles = getRoles(user);
// Roles to add to user
final Set<String> addRoles = new HashSet<>(roles);
addRoles.removeAll(currentRoles);
// Roles to remove from user
final Set<String> delRoles = new HashSet<>(currentRoles);
delRoles.removeAll(roles);
// Add roles as necessary
if (addRoles.size() > 0) {
for (String role : addRoles) {
RoleEntity entity = roleDao.getById(role);
entity.getMembers().add(user);
roleDao.update(entity);
}
}
// Remove roles as necessary
if (delRoles.size() > 0) {
for (String role : delRoles) {
RoleEntity entity = roleDao.getById(role);
entity.getMembers().removeIf(u -> u.getId() == user.getId());
roleDao.update(entity);
}
}
}
// Redirect back to the user page
return Response.seeOther(URI.create("/user/" + userId)).build();
}
use of com.peterphi.usermanager.db.entity.UserEntity in project stdlib by petergeneric.
the class LDAPUserAuthenticationService method registerOrUpdateUser.
private UserEntity registerOrUpdateUser(final LDAPUserRecord ldap) {
UserEntity existing = dao.getUserByEmail(ldap.username);
if (existing == null) {
dao.registerRemote(ldap.username, ldap.fullName);
existing = dao.getUserByEmail(ldap.username);
assert (existing != null);
}
// Now sync the role information
setRoles(existing, ldap);
return existing;
}
use of com.peterphi.usermanager.db.entity.UserEntity in project stdlib by petergeneric.
the class LDAPUserAuthenticationService method authenticate.
@Override
@Transactional
public UserEntity authenticate(final String username, final String password, final boolean basicAuth) {
// TODO Authenticate with LDAP and get user record
LDAPUserRecord record = ldapAuthenticate(username, password);
// Sync LDAP record into our database
UserEntity entity = ensureRolesFetched(registerOrUpdateUser(record));
// Update the last login timestamp
entity.setLastLogin(DateTime.now());
dao.update(entity);
return entity;
}
use of com.peterphi.usermanager.db.entity.UserEntity in project stdlib by petergeneric.
the class UserDaoImpl method register.
@Transactional
public int register(String name, String email, String password, final String dateFormat, final String timeZone) {
if (userExists(email))
throw new IllegalArgumentException("User '" + email + "' already exists!");
if (password.isEmpty())
throw new IllegalArgumentException("Must supply a password!");
final UserEntity account = new UserEntity();
account.setLocal(true);
account.setName(name);
account.setEmail(email);
account.setPassword(hashPassword(password));
account.setDateFormat(dateFormat);
account.setTimeZone(timeZone);
account.setSessionReconnectKey(UUID.randomUUID().toString());
return save(account);
}
Aggregations