Convert login by id into login by email.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package org.codiki.domain.user.model;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public record User(
|
||||
@@ -10,4 +14,21 @@ public record User(
|
||||
String password,
|
||||
UUID photoId,
|
||||
List<UserRole> roles
|
||||
) {}
|
||||
) {
|
||||
public Map<String, Object> toJwtPayload() {
|
||||
Map<String, Object> result = new HashMap<>(4);
|
||||
|
||||
result.put("pseudo", pseudo);
|
||||
result.put("email", email);
|
||||
if (!isNull(photoId)) {
|
||||
result.put("photoId", photoId.toString());
|
||||
}
|
||||
|
||||
String rolesAsString = roles.stream()
|
||||
.map(UserRole::name)
|
||||
.collect(joining(","));
|
||||
result.put("roles", rolesAsString);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
package org.codiki.domain.user.model;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public enum UserRole {
|
||||
STANDARD,
|
||||
ADMIN
|
||||
ADMIN;
|
||||
|
||||
public static Optional<UserRole> from(String roleAsString) {
|
||||
return Stream.of(UserRole.values())
|
||||
.filter(role -> role.name().equals(roleAsString))
|
||||
.findFirst();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package org.codiki.domain.user.model.builder;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.codiki.domain.user.exception.UserCreationException;
|
||||
import org.codiki.domain.user.model.User;
|
||||
import org.codiki.domain.user.model.UserRole;
|
||||
|
||||
@@ -59,6 +61,14 @@ public class UserBuilder {
|
||||
}
|
||||
|
||||
public User build() {
|
||||
return new User(id, pseudo,email, password, photoId, new LinkedList<>(roles));
|
||||
if (isNull(id) || isNull(pseudo) || isNull(email) || isNull(password) || isEmpty(roles)) {
|
||||
throw new UserCreationException();
|
||||
}
|
||||
|
||||
return new User(id, pseudo, email, password, photoId, new LinkedList<>(roles));
|
||||
}
|
||||
|
||||
private static boolean isEmpty(Set<UserRole> roles) {
|
||||
return isNull(roles) || roles.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import org.codiki.domain.user.model.User;
|
||||
public interface UserPort {
|
||||
Optional<User> findById(UUID userId);
|
||||
|
||||
Optional<User> findByEmail(String userEmail);
|
||||
|
||||
List<User> findAll();
|
||||
|
||||
void save(User user);
|
||||
|
||||
Reference in New Issue
Block a user