Skip to content

Commit

Permalink
cleaning white space
Browse files Browse the repository at this point in the history
  • Loading branch information
daveotengo committed Oct 23, 2024
1 parent 0028314 commit 57541fc
Show file tree
Hide file tree
Showing 7 changed files with 1,891 additions and 1,895 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public interface UserFacade {

String resetPassword(String uuid);

String updateUserPassword(String uuid, String newPassword, String currentPassword);
String updateUserPassword(String uuid, String newPassword, String currentPassword);

boolean validatePassword(String uuid, String password);
boolean validatePassword(String uuid, String password);

String checkPasswordStrength(String password);
String checkPasswordStrength(String password);

boolean isPasswordStrong(String password);
boolean isPasswordStrong(String password);

String generatePassword();
String generatePassword();

List<UserDto> getAllAfter(Date date);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,68 +28,68 @@

public final class PasswordHelper {

private PasswordHelper() {
// Hide Utility Class Constructor
}
private PasswordHelper() {
// Hide Utility Class Constructor
}

private static final char[] PASSWORD_CHARS = new char[26 - 2 + 26 - 3 + 8];
static {
int i = 0;
for (char ch = 'a'; ch <= 'z'; ch++) {
switch (ch) {
case 'l':
continue;
case 'v':
continue;
default:
PASSWORD_CHARS[i++] = ch;
}
}
for (char ch = 'A'; ch <= 'Z'; ch++) {
switch (ch) {
case 'I':
continue;
case 'O':
continue;
case 'V':
continue;
default:
PASSWORD_CHARS[i++] = ch;
}
}
for (char ch = '2'; ch <= '9'; ch++) {
PASSWORD_CHARS[i++] = ch;
}
private static final char[] PASSWORD_CHARS = new char[26 - 2 + 26 - 3 + 8];
static {
int i = 0;
for (char ch = 'a'; ch <= 'z'; ch++) {
switch (ch) {
case 'l':
continue;
case 'v':
continue;
default:
PASSWORD_CHARS[i++] = ch;
}
}
for (char ch = 'A'; ch <= 'Z'; ch++) {
switch (ch) {
case 'I':
continue;
case 'O':
continue;
case 'V':
continue;
default:
PASSWORD_CHARS[i++] = ch;
}
}
for (char ch = '2'; ch <= '9'; ch++) {
PASSWORD_CHARS[i++] = ch;
}

if (i != PASSWORD_CHARS.length) {
throw new ValidationException("Size of password char array does not match defined values.");
}
}
if (i != PASSWORD_CHARS.length) {
throw new ValidationException("Size of password char array does not match defined values.");
}
}

public static String createPass(final int length) {
public static String createPass(final int length) {

SecureRandom rnd = new SecureRandom();
SecureRandom rnd = new SecureRandom();

char[] chs = new char[length];
for (int i = 0; i < length; i++)
chs[i] = PASSWORD_CHARS[rnd.nextInt(PASSWORD_CHARS.length)];
final String val = new String(chs);
char[] chs = new char[length];
for (int i = 0; i < length; i++)
chs[i] = PASSWORD_CHARS[rnd.nextInt(PASSWORD_CHARS.length)];
final String val = new String(chs);

return val;
}
return val;
}

public static String encodePassword(String password, String seed) {
public static String encodePassword(String password, String seed) {

MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
digest.reset();
byte[] digested = digest.digest((password + seed).getBytes(StandardCharsets.UTF_8));
String encoded = Hex.encodeHexString(digested);
return encoded;
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
digest.reset();
byte[] digested = digest.digest((password + seed).getBytes(StandardCharsets.UTF_8));
String encoded = Hex.encodeHexString(digested);
return encoded;

} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,120 +33,120 @@
@AuditIgnore
public class CurrentUserService {

@Resource
private SessionContext context;

@PersistenceContext(unitName = ModelConstants.PERSISTENCE_UNIT_NAME)
private EntityManager em;

private final UserCache userCache;

public CurrentUserService() {
this.userCache = UserCache.getInstance();
}

/**
* Returns the User entity corresponding to the current user.
*
* @TransactionScoped would be better for performance, but is not supported by the CDI based testing framework
*/
@RequestScoped
public User getCurrentUser() {
final String currentUsername = context.getCallerPrincipal().getName();

if (currentUsername == null) {
return null;
}

User cachedUser = userCache.get(currentUsername);
if (cachedUser != null) {
return cachedUser;
}

// todo prohibit these names
if (currentUsername.equals("ANONYMOUS") || currentUsername.equals("SYSTEM")) {
return null;
}

final User currentUser = fetchUser(currentUsername);

if (currentUser == null) {
return null;
} else {
userCache.put(currentUsername, currentUser);
return currentUser;
}
}

public boolean hasUserRight(UserRight userRight) {
// this only works for user rights that are used in RolesAllowed or DeclareRoles annotations.
// return context.isCallerInRole(userRight.name());
// We don't want to have to do this for all the user rights, so we check against the user rights of the current user instead
if (getCurrentUser() == null || getCurrentUser().getUserRoles() == null) {
return false;
}

return getCurrentUser().hasUserRight(userRight); // todo cache this?
}

public boolean hasAnyUserRight(Set<UserRight> userRights) {
// this only works for user rights that are used in RolesAllowed or DeclareRoles annotations.
// return context.isCallerInRole(userRight.name());
// We don't want to have to do this for all the user rights, so we check against the user rights of the current user instead
if (getCurrentUser() == null || getCurrentUser().getUserRoles() == null) {
return false;
}

return getCurrentUser().hasAnyUserRight(userRights);
}

public boolean isRestrictedToAssignedEntities() {
return isRestrictedToAssignEntities(getCurrentUser());
}

// We need a clean transaction as we do not want call potential entity listeners which would lead to recursion
@Transactional(Transactional.TxType.REQUIRES_NEW)
User fetchUser(String userName) {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final ParameterExpression<String> userNameParam = cb.parameter(String.class, User.USER_NAME);
final CriteriaQuery<User> cq = cb.createQuery(User.class);

// avoid "Hibernate could not initialize proxy – no Session" Exception
// do eager loading in this case
final Root<User> user = cq.from(User.class);
user.fetch(User.ADDRESS);
Fetch<Object, Object> fetch = user.fetch(User.USER_ROLES);
fetch.fetch(UserRole.EMAIL_NOTIFICATIONS, JoinType.LEFT);
fetch.fetch(UserRole.SMS_NOTIFICATIONS, JoinType.LEFT);

final Predicate equal = cb.equal(cb.lower(user.get(User.USER_NAME)), userNameParam);
cq.select(user).distinct(true);
cq.where(equal);

final TypedQuery<User> q = em.createQuery(cq).setParameter(userNameParam, userName.toLowerCase());

User currentUser = q.getResultList().stream().findFirst().orElse(null);
if (currentUser != null) {
unproxy(currentUser.getRegion());
unproxy(currentUser.getDistrict());
unproxy(currentUser.getCommunity());
unproxy(currentUser.getHealthFacility());
unproxy(currentUser.getPointOfEntry());
unproxy(currentUser.getLaboratory());
unproxy(currentUser.getAssociatedOfficer());
}

return currentUser;
}

public static <T> T unproxy(T proxied) {
if (proxied instanceof HibernateProxy) {
Hibernate.initialize(proxied);
@SuppressWarnings("unchecked")
T obj = (T) ((HibernateProxy) proxied).getHibernateLazyInitializer().getImplementation();
return obj;
} else {
return proxied;
}
}
@Resource
private SessionContext context;

@PersistenceContext(unitName = ModelConstants.PERSISTENCE_UNIT_NAME)
private EntityManager em;

private final UserCache userCache;

public CurrentUserService() {
this.userCache = UserCache.getInstance();
}

/**
* Returns the User entity corresponding to the current user.
*
* @TransactionScoped would be better for performance, but is not supported by the CDI based testing framework
*/
@RequestScoped
public User getCurrentUser() {
final String currentUsername = context.getCallerPrincipal().getName();

if (currentUsername == null) {
return null;
}

User cachedUser = userCache.get(currentUsername);
if (cachedUser != null) {
return cachedUser;
}

// todo prohibit these names
if (currentUsername.equals("ANONYMOUS") || currentUsername.equals("SYSTEM")) {
return null;
}

final User currentUser = fetchUser(currentUsername);

if (currentUser == null) {
return null;
} else {
userCache.put(currentUsername, currentUser);
return currentUser;
}
}

public boolean hasUserRight(UserRight userRight) {
// this only works for user rights that are used in RolesAllowed or DeclareRoles annotations.
// return context.isCallerInRole(userRight.name());
// We don't want to have to do this for all the user rights, so we check against the user rights of the current user instead
if (getCurrentUser() == null || getCurrentUser().getUserRoles() == null) {
return false;
}

return getCurrentUser().hasUserRight(userRight); // todo cache this?
}

public boolean hasAnyUserRight(Set<UserRight> userRights) {
// this only works for user rights that are used in RolesAllowed or DeclareRoles annotations.
// return context.isCallerInRole(userRight.name());
// We don't want to have to do this for all the user rights, so we check against the user rights of the current user instead
if (getCurrentUser() == null || getCurrentUser().getUserRoles() == null) {
return false;
}

return getCurrentUser().hasAnyUserRight(userRights);
}

public boolean isRestrictedToAssignedEntities() {
return isRestrictedToAssignEntities(getCurrentUser());
}

// We need a clean transaction as we do not want call potential entity listeners which would lead to recursion
@Transactional(Transactional.TxType.REQUIRES_NEW)
User fetchUser(String userName) {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final ParameterExpression<String> userNameParam = cb.parameter(String.class, User.USER_NAME);
final CriteriaQuery<User> cq = cb.createQuery(User.class);

// avoid "Hibernate could not initialize proxy – no Session" Exception
// do eager loading in this case
final Root<User> user = cq.from(User.class);
user.fetch(User.ADDRESS);
Fetch<Object, Object> fetch = user.fetch(User.USER_ROLES);
fetch.fetch(UserRole.EMAIL_NOTIFICATIONS, JoinType.LEFT);
fetch.fetch(UserRole.SMS_NOTIFICATIONS, JoinType.LEFT);

final Predicate equal = cb.equal(cb.lower(user.get(User.USER_NAME)), userNameParam);
cq.select(user).distinct(true);
cq.where(equal);

final TypedQuery<User> q = em.createQuery(cq).setParameter(userNameParam, userName.toLowerCase());

User currentUser = q.getResultList().stream().findFirst().orElse(null);
if (currentUser != null) {
unproxy(currentUser.getRegion());
unproxy(currentUser.getDistrict());
unproxy(currentUser.getCommunity());
unproxy(currentUser.getHealthFacility());
unproxy(currentUser.getPointOfEntry());
unproxy(currentUser.getLaboratory());
unproxy(currentUser.getAssociatedOfficer());
}

return currentUser;
}

public static <T> T unproxy(T proxied) {
if (proxied instanceof HibernateProxy) {
Hibernate.initialize(proxied);
@SuppressWarnings("unchecked")
T obj = (T) ((HibernateProxy) proxied).getHibernateLazyInitializer().getImplementation();
return obj;
} else {
return proxied;
}
}
}
Loading

0 comments on commit 57541fc

Please sign in to comment.