-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainController.java
More file actions
96 lines (77 loc) · 3.02 KB
/
MainController.java
File metadata and controls
96 lines (77 loc) · 3.02 KB
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
91
92
93
94
95
96
package com.trello.controller;
import java.security.Principal;
import com.trello.model.AppUser;
import com.trello.model.Board;
import com.trello.service.AppUserService;
import com.trello.service.BoardService;
import com.trello.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import static com.trello.utils.EncrytedPasswordUtils.encrytePassword;
@Controller
@RequestMapping("/")
public class MainController {
@Autowired
private BoardService boardService;
@Autowired
private AppUserService appUserService;
public String getCurrentUsername() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
return auth.getName();
}
@RequestMapping(method = RequestMethod.GET)
public String mainPage(Model model) {
String username = getCurrentUsername();
if ("anonymousUser".equalsIgnoreCase(username)) {
model.addAttribute("check", "anonim");
model.addAttribute("message", "hello");
} else {
model.addAttribute("check", "userloginned");
model.addAttribute("message", "hello " + username);
}
model.addAttribute("board", boardService.getAll());
return "main";
}
@RequestMapping(value = "/board")
public String boardPage(Model model, Principal principal) {
User loginedUser = (User) ((Authentication) principal).getPrincipal();
//TODO create board with creator_id
model.addAttribute("board", new Board());
return "board";
}
@RequestMapping(value = "/board/view")
public String boardcontentPage(Model model, @RequestParam Long id) {
model.addAttribute("board", boardService.findOne(id));
return "boardcontent";
}
@RequestMapping(value = "/board/submit", method = RequestMethod.POST)
public String submitBoard(@ModelAttribute Board board) {
boardService.save(board);
return "redirect:/";
}
@RequestMapping(value = "/reg")
public String reg(Model model) {
model.addAttribute("user", new AppUser());
return "reg";
}
@RequestMapping(value = "/reg/submit", method = RequestMethod.POST)
public String submitUser(Model model, @ModelAttribute AppUser user) {
String encrytedPassword = encrytePassword(user.getEncrytedPassword());
user.setEncrytedPassword(encrytedPassword);
short enable = 1;
user.setEnabled(enable);
appUserService.save(user);
model.addAttribute("check", "anonim");
model.addAttribute("message", "Registration successful");
return "main";
}
@RequestMapping(value = "/login")
public String loginPage() {
return "login";
}
}