Skip to content

Commit

Permalink
Merge branch 'MPS-task-access-control' into 'develop'
Browse files Browse the repository at this point in the history
Mps task access control

See merge request !11
  • Loading branch information
Evgenii Aleksandrov committed Feb 2, 2018
2 parents b49a4c1 + edfff7f commit 975b7aa
Show file tree
Hide file tree
Showing 47 changed files with 1,136 additions and 934 deletions.
2 changes: 2 additions & 0 deletions Implemented operations
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
- Просмотр оплаченных и неоплаченных счетов
- Интернационализация
- что-то еще?

- add 404 page
12 changes: 12 additions & 0 deletions application.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[2018-02-02 10:47:05] ERROR There is an error: java.lang.NullPointerException Default message: null
[2018-02-02 10:55:32] ERROR [THYMELEAF][http-nio-80-exec-6] Exception processing template "service/service_user_list": An error happened during template parsing (template: "class path resource [templates/service/service_user_list.html]" - line 122, col 37)
[2018-02-02 10:55:32] ERROR Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/service/service_user_list.html]" - line 122, col 37)] with root cause
[2018-02-02 10:55:32] ERROR [THYMELEAF][http-nio-80-exec-6] Exception processing template "error": Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers
[2018-02-02 10:55:32] ERROR Servlet.service() for servlet [dispatcherServlet] threw exception
[2018-02-02 10:55:32] ERROR Exception Processing ErrorPage[errorCode=0, location=/error]
[2018-02-02 10:59:24] ERROR An internal error occurred while trying to authenticate the user.
[2018-02-02 12:21:57] ERROR An internal error occurred while trying to authenticate the user.
[2018-02-02 12:37:13] ERROR There is an error: java.lang.NullPointerException Default message: null
[2018-02-02 12:37:30] ERROR [THYMELEAF][http-nio-80-exec-2] Exception processing template "error": Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers
[2018-02-02 12:37:30] ERROR Servlet.service() for servlet [dispatcherServlet] threw exception
[2018-02-02 12:37:30] ERROR Exception Processing ErrorPage[errorCode=0, location=/error]
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.2.RELEASE</version>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.epam.lab.mobilepaymentsystem;

import com.epam.lab.mobilepaymentsystem.controller.ServiceUnitController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@ControllerAdvice
class GlobalControllerExceptionHandler {

private static final String DEFAULT_ERROR_VIEW = "errorhandler";
// TODO: make a pretty page with error stuff and no stack trace
private Logger logger = LoggerFactory.getLogger(ServiceUnitController.class);

@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(Exception.class)
public ModelAndView defaultErrorHandler(Exception e) throws Exception {
ModelAndView mav = new ModelAndView();
Expand All @@ -29,10 +27,10 @@ private void introduceErrors(Exception e, ModelAndView mav) throws Exception {
throw e;
}

String s = Arrays.deepToString(e.getStackTrace());
mav.addObject("exception", e.toString());
mav.addObject("stackTrace", s);
mav.addObject("message", e.getMessage());
mav.setViewName(DEFAULT_ERROR_VIEW);
e.printStackTrace();
logger.error("There is an error: " + e.toString() + " Default message: " + e.getMessage(), e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

@SpringBootApplication
public class MobilePaymentSystemApplication extends WebMvcConfigurerAdapter{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
slr.setDefaultLocale(new Locale("ru"));
return slr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@ protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/h2/**",
"/registration",
"/profile",
"/bills/**",
"/services/**",
"/users/**",
"/h2/**",
"/test/**",
"/registration", "/h2**",
"/resources/**", "/static/**", "/css/**", "/js/**", "/images/**",
"/data/**", "/dist/**", "/js/**", "/vendor/**").permitAll()
// .antMatchers(
// "/users").access("hasRole('ROLE_ADMIN')")
.antMatchers(
"/users**", "/bills**").access("hasRole('ADMIN')")
.antMatchers("/profile**",
"/services**").access("hasAnyRole('USER', 'SUBSCRIBER', 'ADMIN')")
.anyRequest().authenticated()
.and()
.formLogin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,135 @@

import com.epam.lab.mobilepaymentsystem.model.Bill;
import com.epam.lab.mobilepaymentsystem.service.BillService;
import com.epam.lab.mobilepaymentsystem.service.ServiceUnitService;
import com.epam.lab.mobilepaymentsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Controller
public class BillController {

private final BillService billService;
private final ServiceUnitService serviceUnitService;
private final UserService userService;

@Autowired
public BillController(BillService billService) {
public BillController(BillService billService, ServiceUnitService serviceUnitService, UserService userService) {
this.billService = billService;
this.serviceUnitService = serviceUnitService;
this.userService = userService;
}

@GetMapping("/bills")
public String getBills(Model model) {
Iterable<Bill> oldBills = billService.listAllPaidBillsOfUser();
Iterable<Bill> unpaidBills = billService.listAllUnpaidBillsOfUser();
List<Bill> oldBills = billService.getAllPaidBillsOfAllUsersOrderedById(); // todo: check
List<Bill> unpaidBills = billService.getAllUnpaidBillsOfAllUsersOrderedById();
model.addAttribute("paidBills", oldBills);
model.addAttribute("unpaidBills", unpaidBills);
return "bill/bill_list";
}

@GetMapping("/users/{uid}/bills")
public String getBillListByUser(@PathVariable final long uid, Model model) {
List<Bill> userBills = billService.getAllUnpaidBillsOfUserByUserId(uid);
model.addAttribute("currentUserId", userService.getCurrentUserId());
model.addAttribute("empty", userBills.isEmpty());
model.addAttribute("userBills", userBills);
return "bill/bill_user_list";
}

@GetMapping("/users/{uid}/services")
public String getServiceListByUser(@PathVariable final long uid, Model model) {
List<Bill> userBills = billService.getAllNonExpiredActivePaidServiceOfUserByUserId(uid);
List<Bill> userInactiveBills = billService.getAllExpiredActiveServicesOfUserByUserId(uid);
model.addAttribute("currentUserId", userService.getCurrentUserId());
model.addAttribute("empty", userBills.isEmpty());
model.addAttribute("userBills", userBills);
model.addAttribute("oldUserServices", userInactiveBills);
model.addAttribute("empty2", userInactiveBills.isEmpty());
return "service/service_user_list";
}

@GetMapping("/profile/bills")
public String getBillListByCurrentUser(Model model) {
final long id = userService.getCurrentUserId();
List<Bill> userBills = billService.getAllUnpaidBillsOfUserByUserId(id);
model.addAttribute("currentUserId", userService.getCurrentUserId());
model.addAttribute("empty", userBills.isEmpty());
model.addAttribute("userBills", userBills);
return "bill/bill_user_list";
}

@GetMapping("/profile/services")
public String getServiceListByCurrentUser(Model model) {
final long id = userService.getCurrentUserId();
List<Bill> userBills = billService.getAllNonExpiredActivePaidServiceOfUserByUserId(id);
List<Bill> userInactiveBills = billService.getAllExpiredActiveServicesOfUserByUserId(id);
model.addAttribute("currentUserId", userService.getCurrentUserId());
model.addAttribute("empty", userBills.isEmpty());
model.addAttribute("userBills", userBills);
model.addAttribute("oldUserServices", userInactiveBills);
model.addAttribute("empty2", userInactiveBills.isEmpty());
return "service/service_user_list";
}

@GetMapping("/profile/bills/{bid}")
public String getBillUnit(@PathVariable("bid") final long bid,
Model model) {
Bill currentBill = billService.getById(bid);
model.addAttribute("bill", currentBill);
model.addAttribute("service", currentBill.getServiceUnit());
model.addAttribute("userId", userService.getCurrentUserId());
model.addAttribute("unsubscribeAviable", billService.isNewBill(currentBill));
return "bill/bill_item";
}

@GetMapping("/users/{uid}/bills/{bid}")
public String getBillUnit(@PathVariable("uid") final long uid,
@PathVariable("bid") final long bid,
Model model) {
Bill currentBill = billService.getById(bid);
model.addAttribute("bill", currentBill);
model.addAttribute("service", currentBill.getServiceUnit());
model.addAttribute("userId", uid);
model.addAttribute("currentUserId", userService.getCurrentUserId());
model.addAttribute("unsubscribeAviable", billService.isNewBill(currentBill));
return "bill/bill_item";
}

@PostMapping("/profile/bills/{bid}/unsub")
public String unsubscribeFromUnpaidBill(@PathVariable("bid") final long bid) {
long uid = userService.getCurrentUserId();
serviceUnitService.unsubscribeUserFromServiceByBillAndUserId(billService.getById(bid), uid);
return "redirect:/profile/bills";
}

@PostMapping("/users/{uid}/bills/{bid}/unsub")
public String unsubscribeFromUnpaidBill(@PathVariable("uid") final long uid,
@PathVariable("bid") final long bid) {
serviceUnitService.unsubscribeUserFromServiceByBillAndUserId(billService.getById(bid), uid);
return "redirect:/users/" + uid + "/bills";
}

@PostMapping("/profile/bills/{bid}/pay")
public String payUnpaidBill(@PathVariable("bid") final long bid) {
long uid = userService.getCurrentUserId();
billService.withdrawCashToPayForOneBill(billService.getById(bid),
userService.getUserById(uid));
return "redirect:/profile/bills";
}

@PostMapping("/users/{uid}/bills/{bid}/pay")
public String payUnpaidBill(@PathVariable("uid") final long uid,
@PathVariable("bid") final long bid) {
billService.withdrawCashToPayForOneBill(billService.getById(bid),
userService.getUserById(uid));
return "redirect:/users/" + uid + "/bills";
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.epam.lab.mobilepaymentsystem.controller;

import com.epam.lab.mobilepaymentsystem.model.Role;
import com.epam.lab.mobilepaymentsystem.service.BillService;
import com.epam.lab.mobilepaymentsystem.service.ServiceUnitService;
import com.epam.lab.mobilepaymentsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class HomeController {
Expand All @@ -26,11 +28,21 @@ public HomeController(UserService userService,

@GetMapping("/")
public String showHomePage(Model model) {
model.addAttribute("numberOfUsers", userService.numberOfUsers());
model.addAttribute("numberOfServices", serviceUnitService.numberOfServices());
model.addAttribute("numberOfBills", billService.numberOfUnpaidBills());
model.addAttribute("numberOfUnpaidBills", billService.numberOfUnpaidBills());
model.addAttribute("numberOfSubs", userService.numberOfUsersByRole(
Role.ROLE_SUBSCRIBER.getDisplayName()));
model.addAttribute("numberOfUsers", userService.numberOfUsersByRole(
Role.ROLE_USER.getDisplayName()));
model.addAttribute("numberOfServices", serviceUnitService.numberOfAllService());
model.addAttribute("numberOfBills", billService.numberOfAllBills());
model.addAttribute("numberOfUnpaidBills", billService.numberOfAllUnpaidBills());
model.addAttribute("userId", userService.getCurrentUserId());
model.addAttribute("userRole", userService.getCurrentUser().getRole());
return "index";
}

@PostMapping("/")
public String checkBills() {
serviceUnitService.globalCheckBill();
return "redirect:/";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public String serviceAdding(@Valid @ModelAttribute("service") ServiceUnit servic

@GetMapping("/services")
public String listAllServices(Model model, @ModelAttribute("selectedService") ServiceUnit serviceUnit) {
List<ServiceUnit> inactiveServices = serviceUnitService.getAllServicesWithoutSubscribe();
List<ServiceUnit> inactiveServices = serviceUnitService.getAllServicesWithoutSubscribeOfUserByUserId(userService.getCurrentUserId());
model.addAttribute("inactiveServices", inactiveServices);
model.addAttribute("services", serviceUnitService.getAllServices());
model.addAttribute("userRole", userService.getCurrentUser().getRole());
return "service/service_list";
}

Expand All @@ -54,22 +55,7 @@ public String serviceUnitPage(@PathVariable Long id, Model model) {

@PostMapping("/services")
public String subscribeToService(@ModelAttribute("selectedService") ServiceUnit serviceUnit) {
serviceUnitService.subscribeUserToService(serviceUnit.getId());
serviceUnitService.subscribeUserToServiceByUserAndServiceId(userService.getCurrentUserId(), serviceUnit.getId());
return "redirect:/services";
}

// TODO: unsubscribe process doesn't return actual data after pressing a button
@GetMapping("service/my")
public String listActiveServices(@ModelAttribute("selectedService") ServiceUnit serviceUnit, Model model) {
List<ServiceUnit> activeServices = userService.getActiveServicesByUserId();
model.addAttribute("activeServices", activeServices);
return "service/my";
}

@PostMapping("users/{id}/services")
public String unsubscribeFromService(@PathVariable Long id,
@ModelAttribute("selectedService") ServiceUnit serviceUnit) {
serviceUnitService.unsubscribeUserFromService(serviceUnit.getId());
return "redirect:/users/" + id + "/services";
}
}

This file was deleted.

Loading

0 comments on commit 975b7aa

Please sign in to comment.