// Path: spring-boot-oauth-authorization-server/src/main/java/com/sdcuike/practice/domain/Authority.java
// @Data
// @NoArgsConstructor
// @AllArgsConstructor
// public class Authority {
//     private String name;
//     
// }
// 
// Path: spring-boot-oauth-authorization-server/src/main/java/com/sdcuike/practice/domain/User.java
// @Data
// @NoArgsConstructor
// public class User extends AbstractAuditingEntity {
//     private Long id;
//     
//     private String loginName;
//     
//     private String phone;
//     
//     private String passwordHash;
//     
//     private String firstName;
//     
//     private String lastName;
//     
//     private String email;
//     
//     private String imageUrl;
//     
//     private Boolean activated;
//     
//     private String langKey;
//     
//     private String activationKey;
//     
//     private String resetKey;
//     
//     private Date resetTime;
//     
//     private Set<Authority> authorities = new HashSet<>();
//     
//     
// }
// 
// Path: spring-boot-oauth-authorization-server/src/main/java/com/sdcuike/practice/domain/UserAuthority.java
// public class UserAuthority {
//     private Long userId;
// 
//     private String authorityName;
// 
//     public Long getUserId() {
//         return userId;
//     }
// 
//     public void setUserId(Long userId) {
//         this.userId = userId;
//     }
// 
//     public String getAuthorityName() {
//         return authorityName;
//     }
// 
//     public void setAuthorityName(String authorityName) {
//         this.authorityName = authorityName == null ? null : authorityName.trim();
//     }
// }
// 
// Path: spring-boot-oauth-authorization-server/src/main/java/com/sdcuike/practice/mapper/UserAuthorityMapper.java
// @UseDataSourceTest
// public interface UserAuthorityMapper {
//     int deleteByPrimaryKey(UserAuthority key);
// 
//     int insert(UserAuthority record);
// 
//     int insertSelective(UserAuthority record);
//     
//     List<UserAuthority> findByUserId(Long userId);
// }
// 
// Path: spring-boot-oauth-authorization-server/src/main/java/com/sdcuike/practice/mapper/UserMapper.java
// @UseDataSourceTest
// public interface UserMapper {
//     int deleteByPrimaryKey(Long id);
//     
//     int insert(User record);
//     
//     int insertSelective(User record);
//     
//     User selectByPrimaryKey(Long id);
//     
//     int updateByPrimaryKeySelective(User record);
//     
//     int updateByPrimaryKey(User record);
//     User findByLoginName(String loginName);
// }


import com.sdcuike.practice.domain.Authority;
import com.sdcuike.practice.domain.User;
import com.sdcuike.practice.domain.UserAuthority;
import com.sdcuike.practice.mapper.UserAuthorityMapper;
import com.sdcuike.practice.mapper.UserMapper;
import com.sdcuike.spring.security.RichUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
package com.sdcuike.practice.config.security;



/**
 * Authenticate a user from the database.
 */
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private UserAuthorityMapper userAuthorityMapper;
    
    @Override
    @Transactional
    public UserDetails loadUserByUsername(final String login) {
        log.debug("Authenticating {}", login);
        
        Optional<User> userFromDatabase = findOneWithAuthoritiesByLogin(login);
        return userFromDatabase.map(user -> {
            if (!user.getActivated()) {
                throw new UserNotActivatedException("User " + login + " was not activated");
            }
// Path: spring-boot-oauth-authorization-server/src/main/java/com/sdcuike/practice/domain/Authority.java
// @Data
// @NoArgsConstructor
// @AllArgsConstructor
// public class Authority {
//     private String name;
//     
// }
// 
// Path: spring-boot-oauth-authorization-server/src/main/java/com/sdcuike/practice/domain/User.java
// @Data
// @NoArgsConstructor
// public class User extends AbstractAuditingEntity {
//     private Long id;
//     
//     private String loginName;
//     
//     private String phone;
//     
//     private String passwordHash;
//     
//     private String firstName;
//     
//     private String lastName;
//     
//     private String email;
//     
//     private String imageUrl;
//     
//     private Boolean activated;
//     
//     private String langKey;
//     
//     private String activationKey;
//     
//     private String resetKey;
//     
//     private Date resetTime;
//     
//     private Set<Authority> authorities = new HashSet<>();
//     
//     
// }
// 
// Path: spring-boot-oauth-authorization-server/src/main/java/com/sdcuike/practice/domain/UserAuthority.java
// public class UserAuthority {
//     private Long userId;
// 
//     private String authorityName;
// 
//     public Long getUserId() {
//         return userId;
//     }
// 
//     public void setUserId(Long userId) {
//         this.userId = userId;
//     }
// 
//     public String getAuthorityName() {
//         return authorityName;
//     }
// 
//     public void setAuthorityName(String authorityName) {
//         this.authorityName = authorityName == null ? null : authorityName.trim();
//     }
// }
// 
// Path: spring-boot-oauth-authorization-server/src/main/java/com/sdcuike/practice/mapper/UserAuthorityMapper.java
// @UseDataSourceTest
// public interface UserAuthorityMapper {
//     int deleteByPrimaryKey(UserAuthority key);
// 
//     int insert(UserAuthority record);
// 
//     int insertSelective(UserAuthority record);
//     
//     List<UserAuthority> findByUserId(Long userId);
// }
// 
// Path: spring-boot-oauth-authorization-server/src/main/java/com/sdcuike/practice/mapper/UserMapper.java
// @UseDataSourceTest
// public interface UserMapper {
//     int deleteByPrimaryKey(Long id);
//     
//     int insert(User record);
//     
//     int insertSelective(User record);
//     
//     User selectByPrimaryKey(Long id);
//     
//     int updateByPrimaryKeySelective(User record);
//     
//     int updateByPrimaryKey(User record);
//     User findByLoginName(String loginName);
// }


// Path: spring-boot-oauth-authorization-server/src/main/java/com/sdcuike/practice/config/security/UserDetailsServiceImpl.java
import com.sdcuike.practice.domain.Authority;
import com.sdcuike.practice.domain.User;
import com.sdcuike.practice.domain.UserAuthority;
import com.sdcuike.practice.mapper.UserAuthorityMapper;
import com.sdcuike.practice.mapper.UserMapper;
import com.sdcuike.spring.security.RichUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

package com.sdcuike.practice.config.security;



/**
 * Authenticate a user from the database.
 */
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private UserAuthorityMapper userAuthorityMapper;
    
    @Override
    @Transactional
    public UserDetails loadUserByUsername(final String login) {
        log.debug("Authenticating {}", login);
        
        Optional<User> userFromDatabase = findOneWithAuthoritiesByLogin(login);
        return userFromDatabase.map(user -> {
            if (!user.getActivated()) {
                throw new UserNotActivatedException("User " + login + " was not activated");
            }
            List<GrantedAuthority> grantedAuthorities =