com.chutneytesting.security.domain.UserRoles Maven / Gradle / Ivy
package com.chutneytesting.security.domain;
import static com.chutneytesting.security.domain.Role.roleByNamePredicate;
import static com.chutneytesting.security.domain.User.userByIdPredicate;
import static com.chutneytesting.security.domain.User.userByRoleNamePredicate;
import static java.util.Collections.emptySet;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toMap;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
public class UserRoles {
private final Map> userRoleMap;
private UserRoles(Map> userRoleMap) {
this.userRoleMap = userRoleMap;
}
public Optional userById(String userId) {
return users().stream()
.filter(userByIdPredicate(userId))
.findFirst();
}
public Role roleByName(String roleName) {
return roles().stream()
.filter(roleByNamePredicate(roleName))
.findFirst()
.orElseThrow(() -> new RoleNotFoundException("Role [" + roleName + "] is not defined"));
}
public User addNewUser(String id) {
Objects.requireNonNull(id);
User newUser = User.builder()
.withId(id)
.withRole(Role.DEFAULT.name)
.build();
this.userRoleMap.get(Role.DEFAULT).add(newUser);
return newUser;
}
public Set roles() {
return this.userRoleMap.keySet();
}
public Set users() {
return this.userRoleMap.values().stream()
.flatMap(Collection::stream)
.collect(toCollection(LinkedHashSet::new));
}
public Set usersByRole(Role role) {
Objects.requireNonNull(role);
return users().stream()
.filter(userByRoleNamePredicate(role.name))
.collect(toCollection(LinkedHashSet::new));
}
public static UserRolesBuilder builder() {
return new UserRolesBuilder();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserRoles userRoles = (UserRoles) o;
return userRoleMap.equals(userRoles.userRoleMap);
}
@Override
public int hashCode() {
return Objects.hash(userRoleMap);
}
@Override
public String toString() {
return "UserRoles{" +
"userRoleMap=" + userRoleMap +
'}';
}
public static class UserRolesBuilder {
private Set roles = emptySet();
private Set users = emptySet();
private UserRolesBuilder() {
}
public UserRoles build() {
checkDefaultRole();
checkUserRoles();
return new UserRoles(
roles.stream().collect(toMap(
r -> r,
r -> users.stream().filter(userByRoleNamePredicate(r.name)).collect(toCollection(LinkedHashSet::new)),
(x, y) -> y,
LinkedHashMap::new
))
);
}
private void checkUserRoles() {
for (User user : users) {
Optional userRole = roles.stream()
.filter(roleByNamePredicate(user.roleName))
.findFirst();
if (userRole.isEmpty()) {
throw new IllegalArgumentException("Role [" + user.roleName + "] declared for user [" + user.id + "] is not defined");
}
}
}
private void checkDefaultRole() {
if (roles == null || roles.isEmpty()) {
roles = new LinkedHashSet<>();
roles.add(Role.DEFAULT);
}
roles.stream()
.filter(Role.DEFAULT::equals)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Role [" + Role.DEFAULT.name + "] must be defined"));
}
public UserRolesBuilder withRoles(Collection roles) {
if (ofNullable(roles).isPresent()) {
this.roles = new LinkedHashSet<>(roles);
}
return this;
}
public UserRolesBuilder withUsers(Collection users) {
if (ofNullable(users).isPresent()) {
this.users = new LinkedHashSet<>(users);
}
return this;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy