fr.mael.microrss.service.impl.UserServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for fr.mael.microrss.service.impl.UserServiceImpl.java

Source

/*
   Copyright  2013 Mael Le Guvel
   This work is free. You can redistribute it and/or modify it under the
   terms of the Do What The Fuck You Want To Public License, Version 2,
   as published by Sam Hocevar. See the COPYING file for more details.
*/
package fr.mael.microrss.service.impl;

import java.beans.PropertyDescriptor;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.dao.SaltSource;
import org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import fr.mael.microrss.dao.GenericDao;
import fr.mael.microrss.dao.UserDao;
import fr.mael.microrss.domain.Article;
import fr.mael.microrss.domain.Category;
import fr.mael.microrss.domain.Feed;
import fr.mael.microrss.domain.User;
import fr.mael.microrss.domain.UserConfig;
import fr.mael.microrss.service.UserService;
import fr.mael.microrss.util.SecurityUtil;
import fr.mael.microrss.web.beans.WebUser;

@Service(value = "userService")
@Transactional(propagation = Propagation.REQUIRED)
public class UserServiceImpl extends GenericServiceImpl<User, WebUser> implements UserService, UserDetailsService {

    private static Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserDao userDao;

    @Autowired
    private SecurityUtil cryptoUtils;

    @Autowired
    private MessageDigestPasswordEncoder passwordEncoder;

    @Autowired
    private SaltSource salt;

    @Autowired
    private SecurityUtil securityUtil;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDao.loadByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("No user for username " + username);
        }
        return user;
    }

    @Override
    public User save(User entity) {
        if (entity.getId() == null && entity.getSalt() == null) {
            try {
                entity.setSalt(cryptoUtils.random());
                Object mySalt = this.salt.getSalt(entity);
                String encodedPassword = passwordEncoder.encodePassword(entity.getPassword(), mySalt);
                entity.setPassword(encodedPassword);
            } catch (NoSuchAlgorithmException e) {
                LOG.error("Error creating salt", e);
            }
        }
        return super.save(entity);
    }

    @Override
    public boolean hasRightOn(Article article) {
        User user = securityUtil.getCurrentUser();
        return userDao.userHasArticle(user, article);
    }

    @Override
    public boolean hasRightOn(Feed feed) {
        User user = securityUtil.getCurrentUser();
        return userDao.userHasFeed(user, feed);
    }

    @Override
    public boolean hasRightOnUserA(Integer userArticleId) {
        User user = securityUtil.getCurrentUser();
        return userDao.userHasUserArticle(user, userArticleId);
    }

    @Override
    public boolean hasRightOnLabel(Integer labelId) {
        User user = securityUtil.getCurrentUser();
        return userDao.userHasLabel(user, labelId);
    }

    @Override
    public GenericDao<User> getDao() {
        return userDao;
    }

    @Override
    public Class<User> getTClass() {
        return User.class;
    }

    @Override
    public boolean hasRightOn(Category category) {
        User user = securityUtil.getCurrentUser();
        return userDao.userHasCategory(user, category);
    }

    @Override
    public Class<WebUser> getUClass() {
        return WebUser.class;
    }

    @Override
    public List<User> usersForFeed(Feed feed) {
        return userDao.usersForFeed(feed);
    }

    @Override
    public UserConfig getUserConfig() {
        return userDao.getConfig(securityUtil.getCurrentUser());
    }

    @Override
    public void updateOption(String name, String value) {
        UserConfig conf = new UserConfig();
        try {
            PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(conf, name);
            if (desc != null) {
                userDao.updateConfig(securityUtil.getCurrentUser(), name, value);
            }
        } catch (Exception e) {
            LOG.error("Error reading property " + name, e);
        }
    }

}