package uk.ac.ebi.spot.goci.curation.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import uk.ac.ebi.spot.goci.curation.model.CurrentUser;
import uk.ac.ebi.spot.goci.model.SecureUser;
import uk.ac.ebi.spot.goci.repository.SecureUserRepository;

/**
 * Created by emma on 10/02/15.
 *
 * @author emma
 *         <p>
 *         Implementation of springs UserDetailsService
 */
@Service
public class CurrentUserDetailsService implements UserDetailsService {

    // Repository used to find users
    private SecureUserRepository secureUserRepository;

    @Autowired
    public CurrentUserDetailsService(SecureUserRepository secureUserRepository) {
        this.secureUserRepository = secureUserRepository;
    }

    @Override
    public CurrentUser loadUserByUsername(String email) throws UsernameNotFoundException {
        // Check user with email exists

        if (secureUserRepository.findByEmail(email) != null) {
            SecureUser secureUser = secureUserRepository.findByEmail(email);
            return new CurrentUser(secureUser);
        }
        else {
            throw new UsernameNotFoundException("User with email: " + email + " " +
                                                        "was not found");
        }
    }
}

@@ -464,16 +464,95 @@
 itory;%0A%0A
+import javax.servlet.http.HttpServletRequest;%0Aimport java.security.Principal;%0A%0A
 /**%0A * C
@@ -1541,11 +1541,428 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Update a study status%0A     *%0A     * @param request request from which to obtain current user%0A     * @return SecureUser that represents currently logged in user%0A     */%0A    public SecureUser getUserFromRequest(HttpServletRequest request) %7B%0A        Principal principal = request.getUserPrincipal();%0A        String name = principal.getName();%0A        return secureUserRepository.findByEmail(name);%0A    %7D
 %0A%7D%0A

Add method to get currently logged in user from http request