cz.muni.fi.editor.services.api.users.impl.UserServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.services.api.users.impl.UserServiceImpl.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*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.
*/
/*
 * Copyright  2016 Dominik Szalai (emptulik@gmail.com)
 *
 * 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.
 */
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.editor.services.api.users.impl;

import cz.muni.fi.editor.api.UserService;
import cz.muni.fi.editor.api.dto.OrganizationDTO;
import cz.muni.fi.editor.api.dto.UserDTO;
import cz.muni.fi.editor.api.exceptions.FieldException;
import cz.muni.fi.editor.database.dao.OrganizationDAO;
import cz.muni.fi.editor.database.dao.UserDAO;
import cz.muni.fi.editor.database.domain.user.Organization;
import cz.muni.fi.editor.database.domain.user.User;
import cz.muni.fi.editor.services.commons.EditorPasswordEncoder;
import cz.muni.fi.editor.services.commons.Mapper;
import cz.muni.fi.editor.services.commons.annotations.Administrator;
import cz.muni.fi.editor.services.commons.security.SecurityService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.context.SecurityContextHolder;
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.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Dominik Szalai - emptulik at gmail.com
 */
@CacheConfig(cacheNames = "userCache")
@Service("userService")
@Log4j2
public class UserServiceImpl implements UserService, UserDetailsService {
    @Autowired
    private Mapper mapper;
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private OrganizationDAO organizationDAO;
    @Autowired
    private EditorPasswordEncoder passwordEncoder;
    @Autowired
    private SecurityService securityService;

    @Override
    @Administrator
    @Transactional
    @CacheEvict(cacheNames = "userCache", allEntries = true)
    public void create(UserDTO user) throws IllegalArgumentException, FieldException {
        if (userDAO.getByUsername(user.getUsername()).isPresent()) {
            throw new FieldException("username", "Such username already exists");
        }
        if (userDAO.getUserByEmail(user.getEmail()).isPresent()) {
            throw new FieldException("email", "Such email already eixsts.");
        }
        User u = mapper.map(user, User.class);
        userDAO.create(u);
        user.setId(userDAO.create(u));
        user.setPassword(u.getPassword());

        Organization defaultPublic = organizationDAO.getById(2L);
        defaultPublic.getMembers().add(u);

        organizationDAO.update(defaultPublic);
    }

    @Override
    @Administrator
    @Transactional
    public void update(UserDTO user) throws IllegalArgumentException, FieldException {
        User dao = userDAO.getById(user.getId());

        if (!dao.getEmail().equals(user.getEmail()) && userDAO.getUserByEmail(user.getEmail()).isPresent()) {
            throw new FieldException("email", "Such email already exists.");
        }

        if (!dao.getUsername().equals(user.getUsername())
                && userDAO.getByUsername(user.getUsername()).isPresent()) {
            throw new FieldException("username", "such username already exists.");
        }

        if (!StringUtils.isEmpty(user.getPassword())
                && !passwordEncoder.matches(user.getPassword(), dao.getPassword())) {
            dao.setPassword(passwordEncoder.getPassswordHash(user.getPassword()));
        }

        dao.setUsername(user.getUsername());
        dao.setDisplayName(user.getDisplayName());
        dao.setEmail(user.getEmail());

        userDAO.update(dao);
    }

    @Override
    @Administrator
    @Transactional
    public void disable(UserDTO user) throws IllegalArgumentException, FieldException {
        User u = userDAO.getById(user.getId());
        u.setActive(Boolean.FALSE);
        userDAO.update(u);
    }

    @Override
    @Administrator
    @Transactional
    public void enable(UserDTO user) throws IllegalArgumentException, FieldException {
        User u = userDAO.getById(user.getId());
        u.setActive(Boolean.TRUE);
        userDAO.update(u);
    }

    @Override
    @Transactional(readOnly = true)
    @Administrator
    public UserDTO getUserByID(Long id) throws IllegalArgumentException {
        return mapper.map(userDAO.getById(id), UserDTO.class);
    }

    @Transactional(readOnly = true)
    @Administrator
    @Cacheable
    public List<UserDTO> getAll() {
        return mapper.map(userDAO.getAll(), UserDTO.class);
    }

    @Override
    @Transactional
    public void updateProfile(UserDTO user) throws IllegalArgumentException, FieldException {
        UserDTO loggedUser = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (!loggedUser.getId().equals(user.getId())) {
            throw new AccessDeniedException("Access is notifyDeny.");
        }
        User dao = userDAO.getById(loggedUser.getId());

        if (!dao.getEmail().equals(user.getEmail()) && userDAO.getUserByEmail(user.getEmail()).isPresent()) {
            throw new FieldException("email", "Such email already exists.");
        }

        if (!dao.getUsername().equals(user.getUsername())
                && userDAO.getByUsername(user.getUsername()).isPresent()) {
            throw new FieldException("username", "such username already exists.");
        }

        if (!StringUtils.isEmpty(user.getPassword())
                && !passwordEncoder.matches(user.getPassword(), dao.getPassword())) {
            dao.setPassword(passwordEncoder.getPassswordHash(user.getPassword()));
        }
        dao.setUsername(user.getUsername());
        dao.setEmail(user.getEmail());
        dao.setDisplayName(user.getDisplayName());

        // send event
        //        loggedUser.setEmail(user.getEmail());
        //        loggedUser.setDisplayName(user.getDisplayName());
        //        loggedUser.set

        userDAO.update(dao);
        // no need to setup password
    }

    @Override
    @Transactional(readOnly = true)
    public UserDTO getProfile() {
        return mapper.map(userDAO.getById(securityService.getPrincipal().getId()), UserDTO.class);
    }

    @Override
    @Administrator
    @Transactional(readOnly = true)
    public List<UserDTO> getActiveUsers() {
        return mapper.map(userDAO.getActiveUsers(), UserDTO.class);
    }

    @Override
    public Collection<UserDTO> getAllForPreview() {
        return userDAO.getAll().stream().map(u -> {
            UserDTO dto = new UserDTO();
            dto.setDisplayName(u.getDisplayName());
            dto.setUsername(u.getDisplayName());
            dto.setId(u.getId());

            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional(readOnly = true)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Optional<User> optional = this.userDAO.getByUsername(username);
        if (!optional.isPresent()) {
            throw new UsernameNotFoundException(username);
        } else {
            List<OrganizationDTO> member = organizationDAO.getOrganizationForUser(optional.get(), true).stream()
                    .map(o -> {
                        OrganizationDTO dto = new OrganizationDTO();
                        dto.setId(o.getId());
                        return dto;
                    }).collect(Collectors.toList());

            List<OrganizationDTO> owner = organizationDAO.ownedBy(optional.get()).stream().map(o -> {
                OrganizationDTO dto = new OrganizationDTO();
                dto.setId(o.getId());
                return dto;
            }).collect(Collectors.toList());

            UserDTO result = mapper.map(optional.get(), UserDTO.class);

            result.init(owner, member);

            return result;
        }
    }
}