Projekt

Obecné

Profil

« Předchozí | Další » 

Revize f6f42f72

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

re #37 implement WS endpoint for getting current user info

Zobrazit rozdíly:

server/src/main/java/org/danekja/ymanager/dto/FullUserProfile.java
1 1
package org.danekja.ymanager.dto;
2 2

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

  
3 5
import java.time.LocalDateTime;
4 6

  
5 7
/**
......
62 64
     */
63 65
    private String email;
64 66

  
67
    public FullUserProfile() {
68
    }
69

  
70
    public FullUserProfile(User user) {
71
        this(user.getId(), user.getFirstName(), user.getLastName(), user.getPhoto(), user.getVacationCount(), user.getTotalSickDayCount(), user.getTakenSickDayCount(), user.getStatus(), user.getRole(), user.getNotification(), user.getEmail());
72
    }
73

  
74
    public FullUserProfile(Long id, String firstName, String lastName, String photo, Float vacationCount, Integer sickDayCount, Integer takenSickDayCount, Status status, UserRole role, LocalDateTime notification, String email) {
75
        this.id = id;
76
        this.firstName = firstName;
77
        this.lastName = lastName;
78
        this.photo = photo;
79
        this.vacationCount = vacationCount;
80
        this.sickDayCount = sickDayCount;
81
        this.takenSickDayCount = takenSickDayCount;
82
        this.status = status;
83
        this.role = role;
84
        this.notification = notification;
85
        this.email = email;
86
    }
87

  
65 88
    /**
66 89
     * Returns the user's email address.
67 90
     *
server/src/main/java/org/danekja/ymanager/ws/rest/UserController.java
1
package org.danekja.ymanager.ws.rest;
2

  
3
import org.danekja.ymanager.business.UserManager;
4
import org.danekja.ymanager.domain.User;
5
import org.danekja.ymanager.dto.FullUserProfile;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.security.authentication.AnonymousAuthenticationToken;
8
import org.springframework.security.core.Authentication;
9
import org.springframework.web.bind.annotation.GetMapping;
10
import org.springframework.web.bind.annotation.RestController;
11

  
12
/**
13
 * Controller for Users collection of WS API.
14
 */
15
@RestController("/users")
16
public class UserController {
17

  
18
    private final UserManager manager;
19

  
20
    @Autowired
21
    public UserController(UserManager manager) {
22
        this.manager = manager;
23
    }
24

  
25
    /**
26
     * Returns currently authenticated user.
27
     *
28
     * @param auth current authentication object
29
     * @return user information object
30
     */
31
    @GetMapping("/current/profile")
32
    public FullUserProfile getCurrentUser(Authentication auth) {
33

  
34
        if (auth instanceof AnonymousAuthenticationToken) {
35
            return null;
36
        }
37

  
38
        User user = manager.getUser(auth.getName());
39
        return user != null ? new FullUserProfile(user) : null;
40
    }
41
}

Také k dispozici: Unified diff