Search in sources :

Example 1 with UserEntity

use of ua.springweb.security.entity.UserEntity in project Logos_Materials_October_2017 by VolodymyrZavada.

the class SpringBootSecurityWebAppApplication method addAdmin.

static void addAdmin(ConfigurableApplicationContext context) {
    String email = "admin@gmail.com";
    String password = "123";
    UserRepository userRepository = context.getBean(UserRepository.class);
    UserEntity entity = userRepository.findUserByEmail(email);
    if (entity == null) {
        PasswordEncoder passwordEncoder = context.getBean(PasswordEncoder.class);
        entity = new UserEntity();
        entity.setEmail(email);
        entity.setPassword(passwordEncoder.encode(password));
        entity.setRole(Role.ROLE_ADMIN);
        userRepository.save(entity);
    }
}
Also used : UserRepository(ua.springweb.security.repository.UserRepository) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) UserEntity(ua.springweb.security.entity.UserEntity)

Example 2 with UserEntity

use of ua.springweb.security.entity.UserEntity in project Logos_Materials_October_2017 by VolodymyrZavada.

the class BaseController method verifyUser.

@GetMapping("/verify")
public String verifyUser(@RequestParam("token") String token, @RequestParam("userid") String userIdStr) {
    int userId = Integer.valueOf(userIdStr);
    UserEntity user = userService.findUserById(userId);
    if (user.getToken().equals(token)) {
        user.setToken("");
        user.setActivated(true);
        userService.updateUser(user);
    }
    return "redirect:/login";
}
Also used : UserEntity(ua.springweb.security.entity.UserEntity) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with UserEntity

use of ua.springweb.security.entity.UserEntity in project Logos_Materials_October_2017 by VolodymyrZavada.

the class BaseController method showProfile.

@GetMapping("/profile")
// @PreAuthorize("hasRole('ROLE_USER') OR hasRole('ROLE_ADMIN')")
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_USER')")
public String showProfile(Principal principal, Model model) {
    UserEntity entity = userService.findUserByEmail(principal.getName());
    model.addAttribute("user", entity);
    return "profile";
}
Also used : UserEntity(ua.springweb.security.entity.UserEntity) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with UserEntity

use of ua.springweb.security.entity.UserEntity in project Logos_Materials_October_2017 by VolodymyrZavada.

the class BaseController method saveUser.

@PostMapping("/register")
public String saveUser(@ModelAttribute("registerModel") RegisterRequest request) {
    UserEntity user = UserMapper.registerToUser(request);
    String token = RandomToken.generateRandom();
    user.setToken(token);
    user.setActivated(false);
    userService.saveUser(user);
    Mail mail = new Mail();
    mail.setTo(request.getEmail());
    mail.setSubject("You are successfully registered");
    mail.setContent("Please verify your email address by follow the link: " + "http://localhost:8090/verify?token=" + token + "&userid=" + user.getId());
    emailService.sendMessage(mail);
    return "redirect:/login";
}
Also used : Mail(ua.springweb.security.mail.Mail) UserEntity(ua.springweb.security.entity.UserEntity) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with UserEntity

use of ua.springweb.security.entity.UserEntity in project Logos_Materials_October_2017 by VolodymyrZavada.

the class UserMapper method registerToUser.

public static UserEntity registerToUser(RegisterRequest request) {
    UserEntity user = new UserEntity();
    user.setEmail(request.getEmail());
    user.setPassword(request.getPassword());
    user.setRole(Role.ROLE_USER);
    return user;
}
Also used : UserEntity(ua.springweb.security.entity.UserEntity)

Aggregations

UserEntity (ua.springweb.security.entity.UserEntity)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 PasswordEncoder (org.springframework.security.crypto.password.PasswordEncoder)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 Mail (ua.springweb.security.mail.Mail)1 UserRepository (ua.springweb.security.repository.UserRepository)1