@Override public boolean userExists(String username) { return (userRepository.findFirstByUsername(username) != null); } }
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findFirstByUsername(username); if (user == null) throw new UsernameNotFoundException(username); return user; }
@Override public void deleteUser(String username) { User user = userRepository.findFirstByUsername(username); if (user == null) { log.warn("User " + username + " does not exist and therefore cannot be deleted."); return; } userRepository.delete(userRepository.findFirstByUsername(username).getId()); log.debug("Successfully deleted user " + username); }
@Override public User queryByName(String username) throws NotFoundException { log.trace("Get user: " + username); User user = userRepository.findFirstByUsername(username); if (user == null) { throw new NotFoundException("Not found user " + username); } return user; }
@Override public void changePassword(String oldPassword, String newPassword) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String currentUserName = authentication.getName(); log.debug("Changing password of user: " + currentUserName); User user = userRepository.findFirstByUsername(currentUserName); if (!BCrypt.checkpw(oldPassword, user.getPassword())) { throw new UnauthorizedUserException("Old password is wrong."); } if (!(authentication instanceof AnonymousAuthenticationToken)) { // TODO is this line needed? user.setPassword(BCrypt.hashpw(newPassword, BCrypt.gensalt(12))); userRepository.save(user); log.debug("Password of user " + currentUserName + " has been changed successfully."); } }
@PostConstruct public void init() { log.debug("Creating initial Users..."); User admin = userRepository.findFirstByUsername("admin"); if (!userExists("admin")) { User ob_admin = new User(); ob_admin.setUsername("admin"); ob_admin.setEnabled(true); ob_admin.setPassword(BCrypt.hashpw(adminPwd, BCrypt.gensalt(12))); Set<Role> roles = new HashSet<>(); Role role = new Role(); role.setRole(RoleEnum.ADMIN); role.setProject("*"); roles.add(role); ob_admin.setRoles(roles); createUser(ob_admin); } else { log.debug("Admin user exists already."); } log.debug("Users in the DB: "); for (User user : userRepository.findAll()) { log.debug("" + user); } }
@Override public User changePasswordOf(String userName, String newPwd) throws PasswordWeakException, NotFoundException { User user = userRepository.findFirstByUsername(userName); if (user == null) { throw new NotFoundException("Not found user " + userName); } if (checkStrength) { Utils.checkPasswordIntegrity(newPwd); } user.setPassword(BCrypt.hashpw(newPwd, BCrypt.gensalt(12))); customUserDetailsService.updateUser(user); return user; }