1 | package mk.finki.ukim.mk.vehiclerent.web;
|
---|
2 |
|
---|
3 | import mk.finki.ukim.mk.vehiclerent.enumerations.ROLES;
|
---|
4 | import mk.finki.ukim.mk.vehiclerent.generated_model.*;
|
---|
5 | import mk.finki.ukim.mk.vehiclerent.model.Bookings;
|
---|
6 | import mk.finki.ukim.mk.vehiclerent.model.User;
|
---|
7 | import mk.finki.ukim.mk.vehiclerent.repository.VozaciRepozitory;
|
---|
8 | import mk.finki.ukim.mk.vehiclerent.service.MenadzerService;
|
---|
9 | import mk.finki.ukim.mk.vehiclerent.service.RezervacijVozilaService;
|
---|
10 | import mk.finki.ukim.mk.vehiclerent.service.impl.*;
|
---|
11 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
12 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
---|
13 | import org.springframework.security.core.Authentication;
|
---|
14 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
---|
15 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
16 | import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
|
---|
17 | import org.springframework.stereotype.Controller;
|
---|
18 | import org.springframework.ui.Model;
|
---|
19 | import org.springframework.web.bind.annotation.*;
|
---|
20 |
|
---|
21 | import javax.servlet.http.HttpServletRequest;
|
---|
22 | import javax.servlet.http.HttpServletResponse;
|
---|
23 | import java.awt.print.Book;
|
---|
24 | import java.util.ArrayList;
|
---|
25 | import java.util.List;
|
---|
26 | import java.util.SimpleTimeZone;
|
---|
27 |
|
---|
28 | @Controller
|
---|
29 | public class HomeController {
|
---|
30 | @Autowired
|
---|
31 | KlientServiceImpl klientService;
|
---|
32 | @Autowired
|
---|
33 | MenadzerServiceImpl menadzerService;
|
---|
34 | @Autowired
|
---|
35 | VraborenServiceImpl vraborenService;
|
---|
36 |
|
---|
37 | @Autowired
|
---|
38 | VoziloServiceImpl voziloService;
|
---|
39 |
|
---|
40 | @Autowired
|
---|
41 | RezervacijaServiceImpl rezervacijaService;
|
---|
42 |
|
---|
43 | @Autowired
|
---|
44 | DogovorServiceImpl dogovorService;
|
---|
45 |
|
---|
46 | @Autowired
|
---|
47 | RezervacijaVozilaServiceImpl rezervacijVozilaService;
|
---|
48 |
|
---|
49 | @Autowired
|
---|
50 | VozaciRepozitory vozaciRepozitory;
|
---|
51 |
|
---|
52 | public HomeController() {
|
---|
53 |
|
---|
54 |
|
---|
55 | }
|
---|
56 |
|
---|
57 | @RequestMapping(value = "/", method = RequestMethod.GET)
|
---|
58 | public String index(Model model) {
|
---|
59 | Object principal = SecurityContextHolder. getContext(). getAuthentication(). getPrincipal();
|
---|
60 | String embg = "";
|
---|
61 | ROLES role = null;
|
---|
62 | if(principal instanceof KlientiEntity){
|
---|
63 | role = ROLES.KLIENT;
|
---|
64 | embg = ((KlientiEntity) principal).getEmbgk();
|
---|
65 | }else if(principal instanceof VraboteniEntity){
|
---|
66 | role = ROLES.VRABOTEN;
|
---|
67 | embg = ((VraboteniEntity) principal).getEmbgv();
|
---|
68 | }
|
---|
69 | if(role==ROLES.KLIENT){
|
---|
70 | List<RezervaciiEntity> rezervacije = rezervacijaService.findByEmbgvoza(embg);
|
---|
71 | List<Bookings> bookings = new ArrayList<>();
|
---|
72 | for(RezervaciiEntity rez : rezervacije){
|
---|
73 | VozilaEntity vozilo = voziloService.findById(
|
---|
74 | rezervacijVozilaService.findByIdrez(rez.getIdrez())
|
---|
75 | .getIdvozi()
|
---|
76 | );
|
---|
77 | Bookings book = new Bookings();
|
---|
78 | book.setRezervacija(rez);
|
---|
79 | book.setVozilo(vozilo);
|
---|
80 | bookings.add(book);
|
---|
81 | }
|
---|
82 | model.addAttribute("bookings",bookings);
|
---|
83 | }
|
---|
84 |
|
---|
85 | return "index.html";
|
---|
86 | }
|
---|
87 |
|
---|
88 | @RequestMapping(value="/logout", method=RequestMethod.GET)
|
---|
89 | public String logoutPage(HttpServletRequest request, HttpServletResponse response) {
|
---|
90 | Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
---|
91 | if (auth != null){
|
---|
92 | new SecurityContextLogoutHandler().logout(request, response, auth);
|
---|
93 | }
|
---|
94 | return "redirect:/";
|
---|
95 | }
|
---|
96 |
|
---|
97 | @RequestMapping(value = "/login", method = RequestMethod.GET)
|
---|
98 | public String showLogin(Model model) {
|
---|
99 | model.addAttribute("user",new User(null,null,null, ROLES.KLIENT,0));
|
---|
100 | return "login.html";
|
---|
101 | }
|
---|
102 |
|
---|
103 | @RequestMapping(value = "/login", method = RequestMethod.POST)
|
---|
104 | public String login(User user,Model model,HttpServletRequest request) {
|
---|
105 | boolean login = authenticateUserAndInitializeSessionByUsername(user.getEMBG(),user.getRole(),request);
|
---|
106 | if(login){
|
---|
107 | System.out.println("LOGIN OK");
|
---|
108 | }else{
|
---|
109 | System.out.println("LOGIN NOT OK");
|
---|
110 | }
|
---|
111 | Object principal = SecurityContextHolder. getContext(). getAuthentication(). getPrincipal();
|
---|
112 | String embg = "";
|
---|
113 | ROLES role = null;
|
---|
114 | if(principal instanceof KlientiEntity){
|
---|
115 | role = ROLES.KLIENT;
|
---|
116 | embg = ((KlientiEntity) principal).getEmbgk();
|
---|
117 | }else if(principal instanceof VraboteniEntity){
|
---|
118 | role = ROLES.VRABOTEN;
|
---|
119 | embg = ((VraboteniEntity) principal).getEmbgv();
|
---|
120 | }
|
---|
121 | List<RezervaciiEntity> rezervacije = rezervacijaService.findByEmbgvoza(embg);
|
---|
122 | List<Bookings> bookings = new ArrayList<>();
|
---|
123 | for(RezervaciiEntity rez : rezervacije){
|
---|
124 | VozilaEntity vozilo = voziloService.findById(
|
---|
125 | rezervacijVozilaService.findByIdrez(rez.getIdrez())
|
---|
126 | .getIdvozi()
|
---|
127 | );
|
---|
128 | Bookings book = new Bookings();
|
---|
129 | book.setRezervacija(rez);
|
---|
130 | book.setVozilo(vozilo);
|
---|
131 | bookings.add(book);
|
---|
132 | }
|
---|
133 | model.addAttribute("bookings",bookings);
|
---|
134 |
|
---|
135 | return "index.html";
|
---|
136 | }
|
---|
137 |
|
---|
138 | public boolean authenticateUserAndInitializeSessionByUsername(String EMBG, ROLES role, HttpServletRequest request)
|
---|
139 | {
|
---|
140 | boolean result = true;
|
---|
141 | ArrayList<SimpleGrantedAuthority> roles = new ArrayList<>();
|
---|
142 |
|
---|
143 | try
|
---|
144 | {
|
---|
145 | // generate session if one doesn't exist
|
---|
146 | request.getSession();
|
---|
147 |
|
---|
148 | // Authenticate the user
|
---|
149 | switch (role){
|
---|
150 | case KLIENT :
|
---|
151 | System.out.println("Klient login "+EMBG);
|
---|
152 | roles.add(new SimpleGrantedAuthority("ROLE_KLIENT"));
|
---|
153 | KlientiEntity user = klientService.findByEmbg(EMBG);
|
---|
154 | Authentication auth = new UsernamePasswordAuthenticationToken(user, null, roles);
|
---|
155 | SecurityContextHolder.getContext().setAuthentication(auth);
|
---|
156 | break;
|
---|
157 | case MENAGER:
|
---|
158 | /**System.out.println("Menage login");
|
---|
159 | UserDetails user = userDetailsManager.loadUserByUsername(username);
|
---|
160 | Authentication auth = new UsernamePasswordAuthenticationToken(user, null, null);
|
---|
161 | SecurityContextHolder.getContext().setAuthentication(auth);*/
|
---|
162 | break;
|
---|
163 | case VRABOTEN:
|
---|
164 | System.out.println("Vraboten login");
|
---|
165 | roles.add(new SimpleGrantedAuthority("ROLE_VRABOTEN"));
|
---|
166 | VraboteniEntity userVra = vraborenService.findByEmbg(EMBG);
|
---|
167 | Authentication authVra = new UsernamePasswordAuthenticationToken(userVra, null, roles);
|
---|
168 | SecurityContextHolder.getContext().setAuthentication(authVra);
|
---|
169 | break;
|
---|
170 | default: break;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | catch (Exception e)
|
---|
174 | {
|
---|
175 | System.out.println(e.getMessage());
|
---|
176 |
|
---|
177 | result = false;
|
---|
178 | }
|
---|
179 |
|
---|
180 | return result;
|
---|
181 | }
|
---|
182 |
|
---|
183 | @RequestMapping(value = "/register", method = RequestMethod.GET)
|
---|
184 | public String showRegister(Model model) {
|
---|
185 | model.addAttribute("user",new User(null,null,null, ROLES.KLIENT,0));
|
---|
186 | return "register.html";
|
---|
187 | }
|
---|
188 |
|
---|
189 | @RequestMapping(value = "/register", method = RequestMethod.POST)
|
---|
190 | public String register(User user) {
|
---|
191 | switch (user.getRole()){
|
---|
192 | case KLIENT :
|
---|
193 | klientService.register(user.getEMBG(),user.getImeK(),user.getAdresaK(),false);
|
---|
194 | vozaciRepozitory.save(new VozaciEntity(user.getEMBG(),user.getImeK(),user.getAdresaK(),"23232323"));
|
---|
195 | break;
|
---|
196 | case MENAGER:
|
---|
197 | //menadzerService.register(user.getEMBG());
|
---|
198 | break;
|
---|
199 | case VRABOTEN:
|
---|
200 | vraborenService.register(user.getEMBG(), user.getIdis(),user.getImeK());
|
---|
201 | break;
|
---|
202 | default: break;
|
---|
203 | }
|
---|
204 | System.out.println(user.getEMBG());
|
---|
205 | return "index.html";
|
---|
206 | }
|
---|
207 | }
|
---|