Skip to content

Commit

Permalink
[Minor] Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eitch committed Oct 5, 2023
1 parent e5bc843 commit f8c7727
Showing 1 changed file with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public PrivilegeCrudHandler(DefaultPrivilegeHandler privilegeHandler, Map<String
public RoleRep getRole(Certificate certificate, String roleName) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_GET_ROLE);

Role role = this.persistenceHandler.getRole(roleName);
Expand All @@ -54,7 +54,7 @@ public RoleRep getRole(Certificate certificate, String roleName) {
public UserRep getUser(Certificate certificate, String username) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_GET_USER);

User user = this.persistenceHandler.getUser(username);
Expand All @@ -69,7 +69,7 @@ public UserRep getUser(Certificate certificate, String username) {
public Map<String, String> getPolicyDefs(Certificate certificate) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.validateAction(new SimpleRestrictable(DefaultPrivilegeHandler.PRIVILEGE_ACTION,
DefaultPrivilegeHandler.PRIVILEGE_ACTION_GET_POLICIES));

Expand All @@ -83,7 +83,7 @@ public Map<String, String> getPolicyDefs(Certificate certificate) {
public List<RoleRep> getRoles(Certificate certificate) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_GET_ROLE);

Stream<Role> rolesStream = this.persistenceHandler.getAllRoles().stream();
Expand All @@ -98,7 +98,7 @@ public List<RoleRep> getRoles(Certificate certificate) {
public List<UserRep> getUsers(Certificate certificate) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_GET_USER);

Stream<User> usersStream = this.persistenceHandler.getAllUsers().stream();
Expand All @@ -113,7 +113,7 @@ public List<UserRep> getUsers(Certificate certificate) {
public List<UserRep> queryUsers(Certificate certificate, UserRep selectorRep) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_GET_USER);

String selUserId = selectorRep.getUserId();
Expand Down Expand Up @@ -244,7 +244,7 @@ public UserRep addUser(Certificate certificate, UserRep userRepParam, char[] pas
try {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_ADD_USER);

// make sure userId is not set
Expand Down Expand Up @@ -273,13 +273,13 @@ public UserRep addUser(Certificate certificate, UserRep userRepParam, char[] pas
if (password != null) {

// validate password meets basic requirements
privilegeHandler.validatePassword(certificate.getLocale(), password);
this.privilegeHandler.validatePassword(certificate.getLocale(), password);

// get new salt for user
byte[] salt = privilegeHandler.getEncryptionHandler().nextSalt();
byte[] salt = this.privilegeHandler.getEncryptionHandler().nextSalt();

// hash password
passwordCrypt = privilegeHandler.getEncryptionHandler().hashPassword(password, salt);
passwordCrypt = this.privilegeHandler.getEncryptionHandler().hashPassword(password, salt);

history = history.withLastPasswordChange(ZonedDateTime.now());
}
Expand Down Expand Up @@ -310,7 +310,7 @@ public UserRep addUser(Certificate certificate, UserRep userRepParam, char[] pas
public void addOrUpdateUsers(Certificate certificate, List<UserRep> userReps) throws PrivilegeException {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_ADD_USER);

List<User> toCreate = new ArrayList<>();
Expand Down Expand Up @@ -508,13 +508,13 @@ public UserRep updateUser(Certificate certificate, UserRep userRep, char[] passw
} else {

// validate password meets basic requirements
privilegeHandler.validatePassword(certificate.getLocale(), password);
this.privilegeHandler.validatePassword(certificate.getLocale(), password);

// get new salt for user
byte[] salt = privilegeHandler.getEncryptionHandler().nextSalt();
byte[] salt = this.privilegeHandler.getEncryptionHandler().nextSalt();

// hash password
passwordCrypt = privilegeHandler.getEncryptionHandler().hashPassword(password, salt);
passwordCrypt = this.privilegeHandler.getEncryptionHandler().hashPassword(password, salt);

history = history.withLastPasswordChange(ZonedDateTime.now());
}
Expand Down Expand Up @@ -544,7 +544,7 @@ public UserRep updateUser(Certificate certificate, UserRep userRep, char[] passw
public UserRep removeUser(Certificate certificate, String username) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_REMOVE_USER);

// validate user exists
Expand All @@ -559,7 +559,7 @@ public UserRep removeUser(Certificate certificate, String username) {
new SimpleRestrictable(DefaultPrivilegeHandler.PRIVILEGE_REMOVE_USER, new Tuple(null, existingUser)));

// delegate user removal to persistence handler
privilegeHandler.invalidSessionsFor(existingUser);
this.privilegeHandler.invalidSessionsFor(existingUser);
this.persistenceHandler.removeUser(username);
this.privilegeHandler.persistModelAsync();

Expand All @@ -571,7 +571,7 @@ public UserRep removeUser(Certificate certificate, String username) {
public UserRep setUserLocale(Certificate certificate, String username, Locale locale) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_SET_USER_LOCALE);

// get User
Expand Down Expand Up @@ -603,7 +603,7 @@ public UserRep setUserLocale(Certificate certificate, String username, Locale lo
public void requirePasswordChange(Certificate certificate, String username) throws PrivilegeException {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_REQUIRE_PASSWORD_CHANGE);

// get User
Expand Down Expand Up @@ -637,7 +637,7 @@ public void setUserPassword(Certificate certificate, String username, char[] pas
try {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_SET_USER_PASSWORD);

// get User
Expand All @@ -651,13 +651,13 @@ public void setUserPassword(Certificate certificate, String username, char[] pas
if (password != null) {

// validate password meets basic requirements
privilegeHandler.validatePassword(certificate.getLocale(), password);
this.privilegeHandler.validatePassword(certificate.getLocale(), password);

// get new salt for user
byte[] salt = privilegeHandler.getEncryptionHandler().nextSalt();
byte[] salt = this.privilegeHandler.getEncryptionHandler().nextSalt();

// hash password
passwordCrypt = privilegeHandler.getEncryptionHandler().hashPassword(password, salt);
passwordCrypt = this.privilegeHandler.getEncryptionHandler().hashPassword(password, salt);

history = history.withLastPasswordChange(ZonedDateTime.now());
}
Expand All @@ -680,7 +680,7 @@ public void setUserPassword(Certificate certificate, String username, char[] pas
this.privilegeHandler.persistModelAsync();

if (certificate.getUsage() == Usage.SET_PASSWORD)
privilegeHandler.invalidate(certificate);
this.privilegeHandler.invalidate(certificate);

if (password == null)
DefaultPrivilegeHandler.logger.info("Cleared password for " + newUser.getUsername());
Expand All @@ -695,7 +695,7 @@ public void setUserPassword(Certificate certificate, String username, char[] pas
public UserRep setUserState(Certificate certificate, String username, UserState state) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_SET_USER_STATE);

// get User
Expand Down Expand Up @@ -725,7 +725,7 @@ public UserRep setUserState(Certificate certificate, String username, UserState
public RoleRep addRole(Certificate certificate, RoleRep roleRep) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_ADD_ROLE);

// first validate role
Expand Down Expand Up @@ -759,7 +759,7 @@ public RoleRep addRole(Certificate certificate, RoleRep roleRep) {
public RoleRep replaceRole(Certificate certificate, RoleRep roleRep) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_MODIFY_ROLE);

// first validate role
Expand Down Expand Up @@ -800,13 +800,13 @@ public RoleRep replaceRole(Certificate certificate, RoleRep roleRep) {
public RoleRep removeRole(Certificate certificate, String roleName) {

// validate user actually has this type of privilege
PrivilegeContext prvCtx = privilegeHandler.validate(certificate);
PrivilegeContext prvCtx = this.privilegeHandler.validate(certificate);
prvCtx.assertHasPrivilege(DefaultPrivilegeHandler.PRIVILEGE_REMOVE_ROLE);

// validate no user is using this role
Set<String> roles = new HashSet<>(Collections.singletonList(roleName));
UserRep selector = new UserRep(null, null, null, null, null, null, roles, null, null, null);
List<UserRep> usersWithRole = privilegeHandler.queryUsers(certificate, selector);
List<UserRep> usersWithRole = this.privilegeHandler.queryUsers(certificate, selector);
if (!usersWithRole.isEmpty()) {
String usersS = usersWithRole.stream().map(UserRep::getUsername).collect(Collectors.joining(", "));
String msg = "The role {0} can not be removed as the following {1} user have the role assigned: {2}";
Expand Down

0 comments on commit f8c7727

Please sign in to comment.