Package ua.com.jpy.services.auth.mongo.customer

Source Code of ua.com.jpy.services.auth.mongo.customer.MongoUserDetailService

package ua.com.jpy.services.auth.mongo.customer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

import ua.com.jpy.entity.customer.Customer;
import ua.com.jpy.entity.customer.dao.ICustomerDao;
import ua.com.jpy.entity.role.Role;

/**
* @author LSD25
*
*/
@SuppressWarnings("deprecation")
@Component
public class MongoUserDetailService implements UserDetailsService {
 
  private static final Log log = LogFactory.getLog(MongoUserDetailService.class);
 
  private static final String ROLE_USER = "ROLE_USER";
 
  private static final String ROLE_ADMIN = "ROLE_ADMIN";
 
  @Autowired
  private ICustomerDao customerDao;

  @Override
  public UserDetails loadUserByUsername(String username) {
    Customer customer = customerDao.find(username);
    if(customer == null) {
      return null;
    }
    List<Role> roles = customer.getRoles();
    Role role = roles.get(0);
    return new User(customer.getLogin(), customer.getPassword(), customer.isActive(), true, true, true, getAuthorities(role.getRoleInt()));
  }
 
  public Collection<GrantedAuthority> getAuthorities(Integer access) {
    List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
    log.debug("Grant ROLE_USER to this user");
    authList.add(new GrantedAuthorityImpl(ROLE_USER));
    if (access.compareTo(1) == 0) {
      log.debug("Grant ROLE_ADMIN to this user");
      authList.add(new GrantedAuthorityImpl(ROLE_ADMIN));
    }
    return authList;
  }
 
}
TOP

Related Classes of ua.com.jpy.services.auth.mongo.customer.MongoUserDetailService

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.