Search in sources :

Example 1 with User

use of softuni.todolist.entity.User in project SoftUni by kostovhg.

the class TodoController method createProcess.

@PostMapping("/todo/create")
@PreAuthorize("isAuthenticated()")
public String createProcess(TodoBindingModel todoBindingModel) {
    UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    User userEntity = this.userRepository.findByEmail(user.getUsername());
    Todo todoEntity = new Todo(todoBindingModel.getTitle(), todoBindingModel.getContent(), userEntity);
    this.todoRepository.saveAndFlush(todoEntity);
    return "redirect:/";
}
Also used : Todo(softuni.todolist.entity.Todo) UserDetails(org.springframework.security.core.userdetails.UserDetails) User(softuni.todolist.entity.User) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with User

use of softuni.todolist.entity.User in project SoftUni by kostovhg.

the class UserController method registerProcess.

@PostMapping("/register")
public String registerProcess(UserBindingModel userBindingModel) {
    if (!userBindingModel.getPassword().equals(userBindingModel.getConfirmPassword())) {
        return "redirect:/register";
    }
    BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
    User user = new User(userBindingModel.getEmail(), userBindingModel.getFullName(), bCryptPasswordEncoder.encode(userBindingModel.getPassword()));
    Role userRole = this.roleRepository.findByName("ROLE_USER");
    user.addRole(userRole);
    this.userRepository.saveAndFlush(user);
    return "redirect:/login";
}
Also used : Role(softuni.todolist.entity.Role) User(softuni.todolist.entity.User) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with User

use of softuni.todolist.entity.User in project SoftUni by kostovhg.

the class UserController method profilePage.

@GetMapping("/profile")
@PreAuthorize("isAuthenticated()")
public String profilePage(Model model) {
    UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    User user = this.userRepository.findByEmail(principal.getUsername());
    model.addAttribute("user", user);
    model.addAttribute("view", "user/profile");
    return "base-layout";
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) User(softuni.todolist.entity.User) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

User (softuni.todolist.entity.User)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 Role (softuni.todolist.entity.Role)1 Todo (softuni.todolist.entity.Todo)1