package gal.udc.fic.muei.tfm.dap.flipper.service; import gal.udc.fic.muei.tfm.dap.flipper.domain.User; import gal.udc.fic.muei.tfm.dap.flipper.repository.UserRepository; import gal.udc.fic.muei.tfm.dap.flipper.security.AuthoritiesConstants; import gal.udc.fic.muei.tfm.dap.flipper.security.SecurityUtils; import gal.udc.fic.muei.tfm.dap.flipper.service.util.RandomUtil; import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import javax.inject.Inject; import java.util.*; /** * Service class for managing users. * * This file is part of Flipper Open Reverse Image Search. Flipper Open Reverse Image Search is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Flipper Open Reverse Image Search is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Flipper Open Reverse Image Search. If not, see <http://www.gnu.org/licenses/>. */ @Service public class UserService { private final Logger log = LoggerFactory.getLogger(UserService.class); @Inject private PasswordEncoder passwordEncoder; @Inject private UserRepository userRepository; public Optional<User> activateRegistration(String key) { log.debug("Activating user for activation key {}", key); userRepository.findOneByActivationKey(key) .map(user -> { // activate given user for the registration key. user.setActivated(true); user.setActivationKey(null); userRepository.save(user); log.debug("Activated user: {}", user); return user; }); return Optional.empty(); } public Optional<User> completePasswordReset(String newPassword, String key) { log.debug("Reset user password for reset key {}", key); return userRepository.findOneByResetKey(key) .filter(user -> { DateTime oneDayAgo = DateTime.now().minusHours(24); return user.getResetDate().after(oneDayAgo.toDate()); }) .map(user -> { user.setPassword(passwordEncoder.encode(newPassword)); user.setResetKey(null); user.setResetDate(null); userRepository.save(user); return user; }); } public Optional<User> requestPasswordReset(String mail) { return userRepository.findOneByEmail(mail) .filter(user -> user.getActivated() == true) .map(user -> { user.setResetKey(RandomUtil.generateResetKey()); user.setResetDate(new Date()); userRepository.save(user); return user; }); } public User createUserInformation(String login, String password, String firstName, String lastName, String email, String langKey) { User newUser = new User(); newUser.setId(UUID.randomUUID().toString()); Set<String> authorities = new HashSet<>(); String encryptedPassword = passwordEncoder.encode(password); newUser.setLogin(login); // new user gets initially a generated password newUser.setPassword(encryptedPassword); newUser.setFirstName(firstName); newUser.setLastName(lastName); newUser.setEmail(email); newUser.setLangKey(langKey); // new user is not active newUser.setActivated(false); // new user gets registration key newUser.setActivationKey(RandomUtil.generateActivationKey()); authorities.add(AuthoritiesConstants.USER); newUser.setAuthorities(authorities); userRepository.save(newUser); log.debug("Created Information for User: {}", newUser); return newUser; } public void updateUserInformation(String firstName, String lastName, String email, String langKey) { userRepository.findOneByLogin(SecurityUtils.getCurrentLogin()).ifPresent(u -> { u.setFirstName(firstName); u.setLastName(lastName); u.setEmail(email); u.setLangKey(langKey); userRepository.save(u); log.debug("Changed Information for User: {}", u); }); } public void changePassword(String password) { userRepository.findOneByLogin(SecurityUtils.getCurrentLogin()).ifPresent(u -> { String encryptedPassword = passwordEncoder.encode(password); u.setPassword(encryptedPassword); userRepository.save(u); log.debug("Changed password for User: {}", u); }); } public Optional<User> getUserWithAuthoritiesByLogin(String login) { return userRepository.findOneByLogin(login).map(u -> { u.getAuthorities().size(); return u; }); } public User getUserWithAuthorities() { User user = userRepository.findOneByLogin(SecurityUtils.getCurrentLogin()).get(); user.getAuthorities().size(); // eagerly load the association return user; } }