-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
ProfileResource.java
90 lines (83 loc) · 3.64 KB
/
ProfileResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.nonononoki.alovoa.html;
import com.nonononoki.alovoa.Tools;
import com.nonononoki.alovoa.component.TextEncryptorConverter;
import com.nonononoki.alovoa.entity.User;
import com.nonononoki.alovoa.model.AlovoaException;
import com.nonononoki.alovoa.model.UserDto;
import com.nonononoki.alovoa.repo.GenderRepository;
import com.nonononoki.alovoa.repo.UserIntentionRepository;
import com.nonononoki.alovoa.service.AuthService;
import com.nonononoki.alovoa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
@Controller
public class ProfileResource {
public static final String URL = "/profile";
@Autowired
private AuthService authService;
@Autowired
private UserService userService;
@Autowired
private GenderRepository genderRepo;
@Autowired
private UserIntentionRepository userIntentionRepo;
@Autowired
private TextEncryptorConverter textEncryptor;
@Value("${app.profile.image.max}")
private int imageMax;
@Value("${app.image.max-size}")
private int mediaMaxSize;
@Value("${app.interest.max}")
private int interestMaxSize;
@Value("${app.intention.delay}")
private long intentionDelay;
@Value("${app.domain}")
private String domain;
@Value("${app.referral.max}")
private int maxReferrals;
@Value("${app.search.ignore-intention}")
private boolean ignoreIntention;
@GetMapping(URL)
public ModelAndView profile() throws AlovoaException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException,
UnsupportedEncodingException {
User user = authService.getCurrentUser(true);
if (user.isAdmin()) {
return new ModelAndView("redirect:" + AdminResource.URL);
} else {
int age = Tools.calcUserAge(user);
boolean isLegal = age >= Tools.AGE_LEGAL;
int referralsLeft = maxReferrals - user.getNumberReferred();
ModelAndView mav = new ModelAndView("profile");
mav.addObject("user", UserDto.userToUserDto(UserDto.DtoBuilder.builder().ignoreIntention(ignoreIntention)
.currentUser(user).user(user).userService(userService).build()));
mav.addObject("genders", genderRepo.findAll());
mav.addObject("intentions", userIntentionRepo.findAll());
mav.addObject("imageMax", imageMax);
mav.addObject("isLegal", isLegal);
mav.addObject("mediaMaxSize", mediaMaxSize);
mav.addObject("interestMaxSize", interestMaxSize);
mav.addObject("domain", domain);
mav.addObject("referralsLeft", referralsLeft);
boolean showIntention = false;
Date now = new Date();
if (user.getDates().getIntentionChangeDate() == null
|| now.getTime() >= user.getDates().getIntentionChangeDate().getTime() + intentionDelay) {
showIntention = true;
}
mav.addObject("showIntention", showIntention);
return mav;
}
}
}