Interface UserRepository
- All Superinterfaces:
org.springframework.data.repository.CrudRepository<User,,Long> org.springframework.data.jpa.repository.JpaRepository<User,,Long> org.springframework.data.repository.ListCrudRepository<User,,Long> org.springframework.data.repository.ListPagingAndSortingRepository<User,,Long> org.springframework.data.repository.PagingAndSortingRepository<User,,Long> org.springframework.data.repository.query.QueryByExampleExecutor<User>,org.springframework.data.repository.Repository<User,Long>
@ConditionalOnProperty(name="lastshield.basicauth",
havingValue="true")
public interface UserRepository
extends org.springframework.data.jpa.repository.JpaRepository<User,Long>
Repository interface for handling User-related database operations.
This interface extends JpaRepository for CRUD operations on User entities and provides custom queries
for fetching user data with specific roles or by specific fields.
The repository is conditionally activated based on the 'lastshield.basicauth' property. It is only available when this property is set to true.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanexistsUserByUsername(String username) Checks if a user exists with the given username.Retrieves a list of all users with the role 'USER', returning only the necessary fields in a UserDTO.findUserById(Long id) Find a user by their unique ID.findUserByRole(String role) Find a user by their role.findUserByUsername(String username) Find a user by their unique username.findUsersByRole(String role) Find all users with the specified role.Methods inherited from interface org.springframework.data.repository.CrudRepository
count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, saveMethods inherited from interface org.springframework.data.jpa.repository.JpaRepository
deleteAllByIdInBatch, deleteAllInBatch, deleteAllInBatch, deleteInBatch, findAll, findAll, flush, getById, getOne, getReferenceById, saveAllAndFlush, saveAndFlushMethods inherited from interface org.springframework.data.repository.ListCrudRepository
findAll, findAllById, saveAllMethods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.PagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor
count, exists, findAll, findBy, findOne
-
Method Details
-
findUserById
Find a user by their unique ID.- Parameters:
id- the unique identifier of the user.- Returns:
- an Optional containing the User if found, empty if not.
-
findUserByUsername
Find a user by their unique username.- Parameters:
username- the username of the user.- Returns:
- an Optional containing the User if found, empty if not.
-
existsUserByUsername
Checks if a user exists with the given username.- Parameters:
username- the username to check.- Returns:
- true if a user with the given username exists, false otherwise.
-
findUserByRole
Find a user by their role.- Parameters:
role- the role of the user.- Returns:
- an Optional containing the User if found, empty if not.
-
findUsersByRole
Find all users with the specified role.- Parameters:
role- the role of the users.- Returns:
- a List of Users with the specified role.
-
findAllUsersAsDTO
@Query("SELECT new tech.lastbox.lastshield.basicauth.dto.UserDTO(u.id, u.name, u.username, u.role) FROM User u WHERE u.role = \'USER\'") List<UserDTO> findAllUsersAsDTO()Retrieves a list of all users with the role 'USER', returning only the necessary fields in a UserDTO.- Returns:
- a list of UserDTO objects representing users with the 'USER' role.
-