be.bittich.quote.service.impl.UserServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for be.bittich.quote.service.impl.UserServiceImpl.java

Source

/*
 * Copyright 2014 nateriver.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package be.bittich.quote.service.impl;

import be.bittich.quote.dao.UserDAO;
import be.bittich.quote.model.User;
import be.bittich.quote.security.SecurityUser;
import be.bittich.quote.service.UserService;
import static java.lang.String.format;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author nateriver
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    private static final long serialVersionUID = 5686236302350242146L;
    private static final Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class.getName());

    @Autowired
    private UserDAO repository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    public UserServiceImpl() {
        super();
    }

    @Override
    public User insert(User user) {
        User exist = this.repository.findOneByUsername(user.getUsername());
        if (exist != null && exist.getUsername().equals(user.getUsername())) {
            throw new DuplicateKeyException(format("User %s already exist.", user.getUsername()));
        }
        String encodedPassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodedPassword);
        return this.saveOrUpdate(user);
    }

    public void setRepository(UserDAO userDAO) {
        this.repository = userDAO;
    }

    @Override
    public UserDAO getRepository() {
        return this.repository;
    }

    @Override
    public User findOneByUsername(String username) {
        return repository.findOneByUsername(username);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = this.findOneByUsername(username);
        if (user == null) {
            LOG.info(String.join(" ", "Tentative de connexion: l'utilisateur", username, "est introuvable."));
            throw new UsernameNotFoundException(String.join(" ", "L'utilisateur", username, "est introuvable."));
        }
        UserDetails userDetails = SecurityUser.getBuilder().username(user.getUsername())
                .password(user.getPassword()).authorities(user.getRoleSet()).build();
        return userDetails;

    }
}