Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 68bd3513

Přidáno uživatelem Jakub Danek před více než 5 roky(ů)

re #37 create separate interface & component for User-related business logic

Zobrazit rozdíly:

server/src/main/java/org/danekja/ymanager/business/UserManager.java
1
package org.danekja.ymanager.business;
2

  
3
import org.danekja.ymanager.domain.User;
4

  
5
/**
6
 * Interface for application logic handler of User entities.
7
 */
8
public interface UserManager {
9

  
10
    /**
11
     * Gets user by email (username)
12
     *
13
     * @param email email value, used as search key
14
     * @return found user Object or null
15
     */
16
    User getUser(String email);
17
}
server/src/main/java/org/danekja/ymanager/business/impl/DefaultUserManager.java
1
package org.danekja.ymanager.business.impl;
2

  
3
import org.danekja.ymanager.business.UserManager;
4
import org.danekja.ymanager.domain.User;
5
import org.danekja.ymanager.repository.UserRepository;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.security.core.userdetails.UserDetails;
8
import org.springframework.security.core.userdetails.UserDetailsService;
9
import org.springframework.security.core.userdetails.UsernameNotFoundException;
10
import org.springframework.stereotype.Service;
11

  
12
/**
13
 * Default implementation of User-related application logic.
14
 * <p>
15
 * Also implements {@link UserDetailsService} to allow integration with Spring Security framework.
16
 */
17
@Service
18
public class DefaultUserManager implements UserManager, UserDetailsService {
19

  
20
    private final UserRepository userRepository;
21

  
22
    @Autowired
23
    public DefaultUserManager(UserRepository userRepository) {
24
        this.userRepository = userRepository;
25
    }
26

  
27
    @Override
28
    public User getUser(String email) {
29
        return userRepository.getUser(email);
30
    }
31

  
32
    @Override
33
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
34
        UserDetails details = getUser(username);
35

  
36
        //interface contract enforces this behavior
37
        if (details == null) {
38
            throw new UsernameNotFoundException("Username " + username + " not found!");
39
        }
40

  
41
        return details;
42
    }
43
}

Také k dispozici: Unified diff