Package com.jpoweredcart.common.security

Source Code of com.jpoweredcart.common.security.AdminUserDetailService

package com.jpoweredcart.common.security;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.jpoweredcart.common.entity.user.User;
import com.jpoweredcart.common.entity.user.UserGroup;
import com.jpoweredcart.common.service.UserService;


public class AdminUserDetailService implements UserDetailsService {

  private static final Logger logger = LoggerFactory.getLogger(AdminUserDetailService.class);
 
  @Inject
  private UserService userService;
 
  @Override
  public UserDetails loadUserByUsername(String username)
      throws UsernameNotFoundException {
   
    try{
      User user = userService.getSystemUserByUsername(username);
      if(user==null) throw new EmptyResultDataAccessException(1);
     
      CartUserDetails userDetails = new CartUserDetails();
      userDetails.userId = user.getId();
      userDetails.salt = user.getSalt();
      userDetails.password = user.getPassword();
      userDetails.username = user.getUsername();
      userDetails.enabled = user.getStatus()==1;
      UserGroup userGroup = userService.getSystemUserGroupById(user.getUserGroupId());
      if(userGroup!=null){
        userDetails.authorities.add(userGroup.getName());
        String permString = userGroup.getPermission();
        userDetails.permissions = UserPermissions.unserializePermissions(permString);
      }
     
      logger.debug("Found user username={}, enabled={}", username, userDetails.enabled);
     
      return userDetails;
     
    }catch(EmptyResultDataAccessException e){
      logger.debug("Username: {} not found", username);
      throw new UsernameNotFoundException(username+" not found");
    }
  }
 
}
TOP

Related Classes of com.jpoweredcart.common.security.AdminUserDetailService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.