@Test public void updateUserRolesTest() { final Long userId = 1L; final String roleName = "roleName"; final Long roleId = 1L; RoleEntity roleEntity = new RoleEntity(roleName, true); roleEntity.setId(1L); UserRoleModel userRoleModel = createUserRoleModel(roleId, roleName, true); Collection<UserRoleModel> userRoleModelCollection = List.of(userRoleModel); Mockito.when(roleRepository.findRoleEntitiesByRoleNames(Mockito.any())).thenReturn(List.of(roleEntity)); DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository); authorizationUtility.updateUserRoles(userId, userRoleModelCollection); Mockito.verify(userRoleRepository).deleteAllByUserId(Mockito.any()); Mockito.verify(userRoleRepository).saveAll(Mockito.any()); }
@Override public void updateUserRoles(Long userId, Collection<UserRoleModel> roles) { if (null != userId) { userRoleRepository.deleteAllByUserId(userId); if (null != roles && !roles.isEmpty()) { Collection<String> roleNames = roles.stream().map(UserRoleModel::getName).collect(Collectors.toSet()); List<RoleEntity> roleEntities = roleRepository.findRoleEntitiesByRoleNames(roleNames); List<UserRoleRelation> roleRelations = new LinkedList<>(); for (RoleEntity role : roleEntities) { roleRelations.add(new UserRoleRelation(userId, role.getId())); } userRoleRepository.saveAll(roleRelations); } } }
DefaultRoleAccessor implements RoleAccessor { @Override public void updateUserRoles(Long userId, Collection<UserRoleModel> roles) { if (null != userId) { userRoleRepository.deleteAllByUserId(userId); if (null != roles && !roles.isEmpty()) { Collection<String> roleNames = roles.stream().map(UserRoleModel::getName).collect(Collectors.toSet()); List<RoleEntity> roleEntities = roleRepository.findRoleEntitiesByRoleNames(roleNames); List<UserRoleRelation> roleRelations = new LinkedList<>(); for (RoleEntity role : roleEntities) { roleRelations.add(new UserRoleRelation(userId, role.getId())); } userRoleRepository.saveAll(roleRelations); } } } }
DefaultRoleAccessor implements RoleAccessor { @Override public void updateUserRoles(Long userId, Collection<UserRoleModel> roles) { if (null != userId) { userRoleRepository.deleteAllByUserId(userId); if (null != roles && !roles.isEmpty()) { Collection<String> roleNames = roles.stream().map(UserRoleModel::getName).collect(Collectors.toSet()); List<RoleEntity> roleEntities = roleRepository.findRoleEntitiesByRoleNames(roleNames); List<UserRoleRelation> roleRelations = new LinkedList<>(); for (RoleEntity role : roleEntities) { roleRelations.add(new UserRoleRelation(userId, role.getId())); } userRoleRepository.saveAll(roleRelations); } } } @Autowired  DefaultRoleAccessor(RoleRepository roleRepository, UserRoleRepository userRoleRepository, PermissionMatrixRepository permissionMatrixRepository,
        RegisteredDescriptorRepository registeredDescriptorRepository, ConfigContextRepository configContextRepository); }
DefaultRoleAccessor implements RoleAccessor { @Override public void updateUserRoles(Long userId, Collection<UserRoleModel> roles) { if (null != userId) { userRoleRepository.deleteAllByUserId(userId); if (null != roles && !roles.isEmpty()) { Collection<String> roleNames = roles.stream().map(UserRoleModel::getName).collect(Collectors.toSet()); List<RoleEntity> roleEntities = roleRepository.findRoleEntitiesByRoleNames(roleNames); List<UserRoleRelation> roleRelations = new LinkedList<>(); for (RoleEntity role : roleEntities) { roleRelations.add(new UserRoleRelation(userId, role.getId())); } userRoleRepository.saveAll(roleRelations); } } } @Autowired  DefaultRoleAccessor(RoleRepository roleRepository, UserRoleRepository userRoleRepository, PermissionMatrixRepository permissionMatrixRepository,
        RegisteredDescriptorRepository registeredDescriptorRepository, ConfigContextRepository configContextRepository); @Override Set<UserRoleModel> getRoles(); @Override Set<UserRoleModel> getRoles(Collection<Long> roleIds); @Override boolean doesRoleNameExist(String name); @Override UserRoleModel createRole(String roleName); @Override UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix); @Override void updateRoleName(Long roleId, String roleName); @Override PermissionMatrixModel updatePermissionsForRole(String roleName, PermissionMatrixModel permissionMatrix); @Override void deleteRole(Long roleId); @Override void updateUserRoles(Long userId, Collection<UserRoleModel> roles); @Override PermissionMatrixModel mergePermissionsForRoles(Collection<String> roleNames); @Override PermissionMatrixModel readPermissionsForRole(Long roleId); }
DefaultRoleAccessor implements RoleAccessor { @Override public void updateUserRoles(Long userId, Collection<UserRoleModel> roles) { if (null != userId) { userRoleRepository.deleteAllByUserId(userId); if (null != roles && !roles.isEmpty()) { Collection<String> roleNames = roles.stream().map(UserRoleModel::getName).collect(Collectors.toSet()); List<RoleEntity> roleEntities = roleRepository.findRoleEntitiesByRoleNames(roleNames); List<UserRoleRelation> roleRelations = new LinkedList<>(); for (RoleEntity role : roleEntities) { roleRelations.add(new UserRoleRelation(userId, role.getId())); } userRoleRepository.saveAll(roleRelations); } } } @Autowired  DefaultRoleAccessor(RoleRepository roleRepository, UserRoleRepository userRoleRepository, PermissionMatrixRepository permissionMatrixRepository,
        RegisteredDescriptorRepository registeredDescriptorRepository, ConfigContextRepository configContextRepository); @Override Set<UserRoleModel> getRoles(); @Override Set<UserRoleModel> getRoles(Collection<Long> roleIds); @Override boolean doesRoleNameExist(String name); @Override UserRoleModel createRole(String roleName); @Override UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix); @Override void updateRoleName(Long roleId, String roleName); @Override PermissionMatrixModel updatePermissionsForRole(String roleName, PermissionMatrixModel permissionMatrix); @Override void deleteRole(Long roleId); @Override void updateUserRoles(Long userId, Collection<UserRoleModel> roles); @Override PermissionMatrixModel mergePermissionsForRoles(Collection<String> roleNames); @Override PermissionMatrixModel readPermissionsForRole(Long roleId);  }