| .. | .. |
|---|
| 1 | +/* |
|---|
| 2 | + * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved. |
|---|
| 3 | + */ |
|---|
| 1 | 4 | package net.curisit.securis.services.helpers; |
|---|
| 2 | 5 | |
|---|
| 3 | 6 | import jakarta.enterprise.context.ApplicationScoped; |
|---|
| .. | .. |
|---|
| 8 | 11 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| 9 | 12 | import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 10 | 13 | |
|---|
| 14 | +/** |
|---|
| 15 | + * UserHelper |
|---|
| 16 | + * <p> |
|---|
| 17 | + * Small helper to resolve the current user (from security context) or by username. |
|---|
| 18 | + * Throws a typed {@link SeCurisServiceException} if the user cannot be found. |
|---|
| 19 | + * |
|---|
| 20 | + * Thread-safety: ApplicationScoped, stateless. |
|---|
| 21 | + * |
|---|
| 22 | + * @author JRA |
|---|
| 23 | + * Last reviewed by JRA on Oct 5, 2025. |
|---|
| 24 | + */ |
|---|
| 11 | 25 | @ApplicationScoped |
|---|
| 12 | 26 | public class UserHelper { |
|---|
| 13 | 27 | |
|---|
| 28 | + /** |
|---|
| 29 | + * getUser |
|---|
| 30 | + * <p> |
|---|
| 31 | + * Resolve the current authenticated user from {@link BasicSecurityContext}. |
|---|
| 32 | + * |
|---|
| 33 | + * @param bsc Security context containing a principal. |
|---|
| 34 | + * @param em EntityManager to fetch the user. |
|---|
| 35 | + * @return Managed {@link User}. |
|---|
| 36 | + * @throws SeCurisServiceException if the principal is null or not found in DB. |
|---|
| 37 | + */ |
|---|
| 14 | 38 | public User getUser(BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException { |
|---|
| 15 | 39 | String username = bsc.getUserPrincipal().getName(); |
|---|
| 16 | 40 | return getUser(username, em); |
|---|
| 17 | 41 | } |
|---|
| 18 | 42 | |
|---|
| 43 | + /** |
|---|
| 44 | + * getUser |
|---|
| 45 | + * <p> |
|---|
| 46 | + * Resolve a user by username. |
|---|
| 47 | + * |
|---|
| 48 | + * @param username Username to look up (nullable allowed; returns null). |
|---|
| 49 | + * @param em EntityManager to fetch the user. |
|---|
| 50 | + * @return Managed {@link User} or null if username is null. |
|---|
| 51 | + * @throws SeCurisServiceException if a non-null username does not exist. |
|---|
| 52 | + */ |
|---|
| 19 | 53 | public User getUser(String username, EntityManager em) throws SeCurisServiceException { |
|---|
| 20 | 54 | User user = null; |
|---|
| 21 | 55 | if (username != null) { |
|---|