com.miserablemind.butter.domain.model.user.user.UserManager.java Source code

Java tutorial

Introduction

Here is the source code for com.miserablemind.butter.domain.model.user.user.UserManager.java

Source

package com.miserablemind.butter.domain.model.user.user;

import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/*
 * Miserable Mind
 * http://www.butter.miserablemind.com
 * The MIT License (MIT)
 */

/**
 * Service Layer for {@link AppUser} object.
 * <p>It extends {@link UserDetailsService} for Spring Security Interface as it is used for authentication.</p>
 *
 * @author <a href="http://www.miserablemind.com" target="_blank">miserablemind</a>
 */
public interface UserManager extends UserDetailsService {

    /**
     * Registers a new user.
     * <p>Implementation should be transactional.</p>
     * <p>It requires a token for e-mail verification because verification e-mail is inserted in the same transaction.</p>
     *
     * @param appUser           user to register. It contains data that most likely comes from a user input form.
     * @param verificationToken a token for e-mail verification.
     * @throws UserTakenException
     */
    public void registerUser(AppUser appUser, String verificationToken) throws UserTakenException;

    /**
     * A method that Spring Security uses to authenticate and log in a user.
     *
     * @param userNameOrEmail username to load. It can be also e-mail, in that case user that has that e-mail will be loaded
     * @return a user object if found
     * @throws UsernameNotFoundException if no user was found with that e-mail or that username
     */
    @Override
    public AppUser loadUserByUsername(String userNameOrEmail) throws UsernameNotFoundException;

    /**
     * Verifies user's e-mail address if the provided token is correct.
     * <p>Is called when user clicks a link in verification e-mail.</p>
     * <p>Implementation should be transactional.</p>
     *
     * @param user  user that is verifying its e-mail address
     * @param token verification token that user got in verification the e-mail
     * @return {@code true} if verification was successful, otherwise {@code false}
     */
    public boolean verifyEmail(AppUser user, String token);

    /**
     * Gets {@link AppUser} by e-mail address.
     *
     * @param email e-mail address that user is being looked up by
     * @return returns AppUser if found, otherwise {@code false}
     */
    public AppUser getUserByEmail(String email);

    /**
     * Inserts password reset token when password reset is requested.
     * <p>The token should not be displayed to user on request time, but rather sent by e-mail and later
     * checked by {@link #getIsResetPasswordAuthorized(String, String)}.</p>
     *
     * @param userId     id of a user the password reset is requested
     * @param resetToken random token not displayed for user on request time
     */
    public void addPasswordResetToken(long userId, String resetToken);

    /**
     * Checks if user should be allowed to reset its password.
     * <p>User is authorized if the link in the e-mail sent to user was clicked and token has not expired.</p>
     *
     * @param email e-mail address the reset-password e-mail was sent to
     * @param token password token send to user by e-mail
     * @return {@code true} if not expired token exists for the e-mail provided, otherwise {@code false}
     */
    public boolean getIsResetPasswordAuthorized(String email, String token);

    /**
     * Changes user's password to a new one.
     *
     * @param password new user's password (not encoded)
     * @param user     user to change password for
     * @throws UsernameNotFoundException throws exception if user is not enabled
     */
    public void changePassword(String password, AppUser user) throws UsernameNotFoundException;

    /**
     * Inserts a token for user to verify e-mail against.
     * <p>The token should never be shown to user, but rather be sent to the e-mail address that is being verified.</p>
     *
     * @param email             e-mail address to insert verification token for
     * @param verificationToken token against which user will verify its e-mail address
     */
    public void insertEmailVerification(String email, String verificationToken);

    /**
     * Updates {@link AppUser}
     * <p>All the values provided {@link AppUser} except id get saved to persistence layer.</p>
     *
     * @param updatedUser dirty AppUser.
     * @throws UserTakenException if values that need to be unique, {@link AppUser#email} or {@link AppUser#username} already exist for another user
     */
    public void updateUser(AppUser updatedUser) throws UserTakenException;
}