com.chutneytesting.security.infra.SpringUserService Maven / Gradle / Ivy
/*
* SPDX-FileCopyrightText: 2017-2024 Enedis
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package com.chutneytesting.security.infra;
import com.chutneytesting.security.api.UserDto;
import com.chutneytesting.security.domain.CurrentUserNotFoundException;
import com.chutneytesting.server.core.domain.security.UserService;
import java.util.Optional;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
@Component
public class SpringUserService implements UserService {
public UserDto currentUser() {
final Optional authentication = Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication());
return (UserDto) authentication
.map(Authentication::getPrincipal)
.orElseThrow(CurrentUserNotFoundException::new);
}
@Override
public String currentUserId() {
return currentUser().getId();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy