@Override public void updateUser(UserDetails user) { inMemManager.updateUser(user); User userToUpdate = userRepository.findFirstByUsername(user.getUsername()); userToUpdate.setPassword(user.getPassword()); for (GrantedAuthority authority : user.getAuthorities()) { StringTokenizer stringTokenizer = new StringTokenizer(authority.getAuthority(), ":"); String rl = stringTokenizer.nextToken(); String pj = stringTokenizer.nextToken(); boolean found = false; for (Role role : userToUpdate.getRoles()) { if (role.getProject().equals(pj)) { role.setRole(Role.RoleEnum.valueOf(rl)); found = true; } } if (!found) { Role role = new Role(); role.setRole(Role.RoleEnum.valueOf(rl)); role.setProject(pj); userToUpdate.getRoles().add(role); } } userRepository.save(userToUpdate); }
@Override public void run(String... args) throws Exception { log.debug("Creating initial Users..."); if (userRepository.findFirstByUsername("admin") == null) { 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(Role.RoleEnum.NUBOMEDIA_ADMIN); role.setProject("*"); roles.add(role); ob_admin.setRoles(roles); userRepository.save(ob_admin); } if (!inMemManager.userExists("admin")) { UserDetails admin = new org.springframework.security.core.userdetails.User( "admin", BCrypt.hashpw(adminPwd, BCrypt.gensalt(12)), true, true, true, true, AuthorityUtils.createAuthorityList("NUBOMEDIA_ADMIN:*")); inMemManager.createUser(admin); } else { log.debug("Admin" + inMemManager.loadUserByUsername("admin")); } /*if (userRepository.findFirstByUsername("guest") == null) { User ob_guest = new User(); ob_guest.setUsername("guest"); ob_guest.setPassword(BCrypt.hashpw(guestPwd, BCrypt.gensalt(12))); ob_guest.setEnabled(true); Set<Role> roles = new HashSet<>(); Role role = new Role(); role.setRole(Role.RoleEnum.GUEST); role.setProject("*"); roles.add(role); ob_guest.setRoles(roles); userRepository.save(ob_guest); } if (!inMemManager.userExists("guest")) { UserDetails guest = new org.springframework.security.core.userdetails.User("guest", BCrypt.hashpw(guestPwd, BCrypt.gensalt(12)), true, true, true, true, AuthorityUtils.createAuthorityList("GUEST:*")); inMemManager.createUser(guest); }*/ log.debug("User in the DB: "); for (User user : userRepository.findAll()) { log.debug("" + user); } for (User user : userRepository.findAll()) { if (!user.getUsername().equals("admin") && !user.getUsername().equals("guest")) { String[] roles = new String[user.getRoles().size()]; for (int i = 0; i < user.getRoles().size(); i++) { roles[i] = user.getRoles().toArray(new Role[0])[i].getRole() + ":" + user.getRoles().toArray(new Role[0])[i].getProject(); } UserDetails userDetails = new org.springframework.security.core.userdetails.User( user.getUsername(), user.getPassword(), user.isEnabled(), true, true, true, AuthorityUtils.createAuthorityList(roles)); inMemManager.createUser(userDetails); } } log.debug("Users in UserDetailManager: "); log.info("ADMIN: " + inMemManager.loadUserByUsername("admin")); // log.debug("GUEST: " + inMemManager.loadUserByUsername("guest")); log.debug("Creating initial Project..."); if (projectManagement.queryByName(projectDefaultName) == null) { Project project = new Project(); project.setName(projectDefaultName); projectManagement.add(project); log.debug("Created project: " + project); } else log.debug("Project " + projectDefaultName + " already existing"); }