public void save(User user) { userRepository.save(user); } }
public long countByEmail(String email) { return userRepository.countByEmail(email); }
public User findById(String id) throws EntityException { return userRepository.findById(id).orElseThrow(() -> new EntityException("No user found")); }
public User findByEmail(String email) throws EntityException { return userRepository.findByEmail(email).orElseThrow(() -> new EntityException("No user found")); }
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { final User user = userRepository.findByEmail(username).orElse(null); try { if (user == null) { throw new EntityException("No user found"); } else { boolean enabled = user.isActivated(); boolean accountNonExpired = true; boolean credentialsNonExpired = true; boolean accountNonLocked = true; return new org.springframework.security.core.userdetails.User(user.getEmail(), "{noop}" + user.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuthorities(userRepository.findRolesById(user.getId()))); } } catch (EntityException e) { throw new UsernameNotFoundException(e.getMessage()); } }
public List<Category> findByAncestor(String id) { final List<Category> categories = new ArrayList<>(); categoryRepo.findByParentId(id, null).forEach(category -> { categories.add(category); categories.addAll(findByAncestor(category.getId())); }); return categories; } }
private void activateUser(User user, Token token) { user.setActivated(true); userService.save(user); tokenRepository.delete(token); }
@GetMapping("register/activate") public String doActivate(@RequestParam(value = "uid", required = false) String userId, @RequestParam(value = "tid", required = false) String tokenId) { try { final HashMap<String, Object> parameters = new HashMap<>(); final User user = userService.findById(userId).getContent(); final Token token = tokenRepository.findById(tokenId).orElse(null); final Calendar calendar = Calendar.getInstance(); if (user == null) { throw new EntityException("No user found"); } if (user.isActivated()) { throw new EntityException("User is already activated"); } if (token == null || !(token.getExpiryDate().getTime() - calendar.getTime().getTime() > 0)) { throw new EntityException("Invalid token"); } parameters.put("do", RegistrationEvent.ACTIVATE_USER); parameters.put("user", user); parameters.put("token", token); publisher.publishEvent(new RegistrationEvent(parameters)); return "redirect:/catalog"; } catch (EntityException e) { return "error/500"; } }
@GetMapping(value = {"/{itemId}"}, produces = {"application/json", "application/hal+json"}) public ResponseEntity<?> findById(@PathVariable("itemId") String itemId) { final Mapper mapper = new Mapper(); final Optional<Item> item = itemRepo.findById(itemId); try { final ItemDto dto = mapper.from(item.orElseThrow(() -> new EntityException("No item found"))) .toInstanceOf(ItemDto.class); final Resource<ItemDto> resource = new Resource<>(dto); resource.add(linkTo(methodOn(getClass()).findById(itemId)).withSelfRel()); return new ResponseEntity<>(resource, HttpStatus.OK); } catch (EntityException e) { return ResponseEntity.notFound().build(); } }
@GetMapping(value = "{categoryId}", produces = {"application/json", "application/hal+json"}) public ResponseEntity<?> findById(@PathVariable("categoryId") String categoryId) { final Optional<Category> category = categoryRepo.findById(categoryId); final Mapper mapper = new Mapper(); try { final CategoryDto dto = mapper.from(category.orElseThrow(() -> new EntityException("No category found"))).toInstanceOf(CategoryDto.class); final Resource<CategoryDto> resource = new Resource<>(dto); resource.add(linkTo(methodOn(getClass()).findById(categoryId)).withSelfRel()); return new ResponseEntity<>(resource, HttpStatus.OK); } catch (EntityException e) { return ResponseEntity.notFound().build(); } }
private void sendConfirmationEmail(User user, String baseUrl) { Thread thread = new Thread(() -> { try { final Token token = new Token(); final StringBuilder builder = new StringBuilder(); final String address = "ermdev.io@gmail.com"; final String recipientAddress = user.getEmail(); final String title = "Cloth Shop"; final String subject = "Account Details for " + user.getName() + " at " + title; token.setKey(String.valueOf(IdGenerator.randomUUID())); token.setExpiryDate(new DateHelper().setTimeNow().addTimeInMinute(DateHelper.DAY_IN_MINUTE).getDate()); tokenRepository.save(token); builder.append(baseUrl); builder.append("register/activate"); builder.append("?uid="); builder.append(user.getId()); builder.append("&tid="); builder.append(token.getKey()); MimeMailMessage mailMessage = new MimeMailMessage(mailSender.createMimeMessage()); mailMessage.setTo(recipientAddress); mailMessage.setSubject(subject); mailMessage.getMimeMessage().setFrom(new InternetAddress(address, title)); mailMessage.getMimeMessage().setContent(builder.toString(), "text/html"); mailSender.send(mailMessage.getMimeMessage()); } catch (Exception e) { e.printStackTrace(); } }); thread.start(); }
pageCategory = categoryRepo.findAll(builder.build(), pageable); pageCategory.forEach(category -> { final CategoryDto dto = mapper.from(category).toInstanceOf(CategoryDto.class);
final Page<Item> pageItem = itemRepo.findByCategoryId(categoryIds, pageable);
final Page<Category> pageCategory = categoryRepo.findByParentIsNull(pageable);
pageItem = itemRepo.findAll(builder.build(), pageable); pageItem.forEach(item -> { final ItemDto dto = mapper.from(item).toInstanceOf(ItemDto.class);
final Page<Category> pageCategory = itemRepo.findCategoriesById(itemId, pageable);
@Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException { final String username = request.getParameter("username"); final Notification notification = new Notification(); final User user = userRepo.findByEmail(username).orElse(null);; notification.setTitle("Bad credentials"); notification.setMessage("Incorrect email or password."); if (user != null && !user.isActivated()) { final StringBuilder builder = new StringBuilder(); builder.append("<form class='notif-link' action='register/resend-confirmation' method='post'>"); builder.append("<input name='email' type='hidden' value='"); builder.append(username).append("'/>"); builder.append("<input type='submit' value='here'/>"); builder.append("</form>"); notification.setTitle("Account hasn't been activated"); notification.setMessage("Please activate your account " + builder.toString() + "."); } response.setContentType("application/json; charset=utf-8"); response.setStatus(500); response.getWriter().write(new ObjectMapper().writeValueAsString(notification)); response.getWriter().flush(); clearAuthenticationAttributes(request); }
@GetMapping(value = {"/{userId}"}, produces = {"application/json", "application/hal+json"}) public ResponseEntity<?> findById(@PathVariable("userId") String userId) { final Mapper mapper = new Mapper(); final Optional<User> item = userRepo.findById(userId); try { final UserDto dto = mapper.from(item.orElseThrow(() -> new EntityException("No item found"))) .toInstanceOf(UserDto.class); final Resource<UserDto> resource = new Resource<>(dto); resource.add(linkTo(methodOn(getClass()).findById(userId)).withSelfRel()); return new ResponseEntity<>(resource, HttpStatus.OK); } catch (EntityException e) { return ResponseEntity.notFound().build(); } }
final Page<Category> pageCategory = categoryRepo.findByParentId(categoryId, pageable);
@GetMapping(value = {"/search/findByEmail"}, produces = {"application/json", "application/hal+json"}) public ResponseEntity<?> findByEmail(@RequestParam("email") String email) { final Mapper mapper = new Mapper(); final Optional<User> item = userRepo.findByEmail(email); try { final UserDto dto = mapper.from(item.orElseThrow(() -> new EntityException("No item found"))) .toInstanceOf(UserDto.class); final Resource<UserDto> resource = new Resource<>(dto); resource.add(linkTo(methodOn(getClass()).findById(dto.getUid())).withSelfRel()); return new ResponseEntity<>(resource, HttpStatus.OK); } catch (EntityException e) { return ResponseEntity.notFound().build(); } } }