Skip to content

Commit

Permalink
moved the modules of user to a single place, added few members, added…
Browse files Browse the repository at this point in the history
… uniqueness of email at the db level
  • Loading branch information
surajcm committed Feb 15, 2024
1 parent ffedc2a commit b4baefd
Show file tree
Hide file tree
Showing 31 changed files with 294 additions and 282 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.quiz.darkhold.challenge.entity;

import com.quiz.darkhold.login.entity.User;
import com.quiz.darkhold.user.entity.User;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.quiz.darkhold.challenge.exception.ChallengeException;
import com.quiz.darkhold.challenge.repository.ChallengeRepository;
import com.quiz.darkhold.challenge.repository.QuestionSetRepository;
import com.quiz.darkhold.login.repository.UserRepository;
import com.quiz.darkhold.user.repository.UserRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.quiz.darkhold.home.model.GameInfo;
import com.quiz.darkhold.home.service.HomeService;
import com.quiz.darkhold.login.service.SecurityService;
import com.quiz.darkhold.user.service.SecurityService;
import com.quiz.darkhold.util.CommonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/quiz/darkhold/login/model/ROLE.java

This file was deleted.

52 changes: 0 additions & 52 deletions src/main/java/com/quiz/darkhold/login/model/UserVO.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.quiz.darkhold.challenge.entity.Challenge;
import com.quiz.darkhold.challenge.repository.ChallengeRepository;
import com.quiz.darkhold.login.repository.UserRepository;
import com.quiz.darkhold.user.repository.UserRepository;
import com.quiz.darkhold.options.model.ChallengeInfo;
import com.quiz.darkhold.options.model.ChallengeSummary;
import org.apache.commons.logging.Log;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.quiz.darkhold.login.controller;
package com.quiz.darkhold.user.controller;

import com.quiz.darkhold.login.entity.User;
import com.quiz.darkhold.login.service.SecurityService;
import com.quiz.darkhold.login.service.UserService;
import com.quiz.darkhold.user.entity.User;
import com.quiz.darkhold.user.service.SecurityService;
import com.quiz.darkhold.user.service.UserService;
import com.quiz.darkhold.util.CommonUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
Expand All @@ -13,7 +13,6 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.AbstractMap;
import java.util.Map;
Expand Down Expand Up @@ -131,36 +130,6 @@ public String loginMe2() {
return LOGIN;
}

/**
* on to create challenge page.
*
* @return create challenge page
*/
@GetMapping("/userManagement")
public String manageUsers(final Model model) {
logger.info("Into the manageUsers method");
var listUsers = userService.listAll();
model.addAttribute("listusers", listUsers);
return "user/usermanagement";
}

@GetMapping("/user/create")
public String createUser(final Model model) {
logger.info("Into the createUser method");
var roles = userService.listRoles();
var user = new User();
user.setEnabled(true);
model.addAttribute("userForm", user);
model.addAttribute("listRoles", roles);
return "user/createuser";
}

@PostMapping("/users/save")
public String saveUser(final User user, final RedirectAttributes redirectAttributes) {
logger.info("Into the saveUser method, user is {}", user);
userService.save(user);
redirectAttributes.addFlashAttribute("message", "The user has been saved successfully");
return "redirect:/userManagement";
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.quiz.darkhold.login.controller;
package com.quiz.darkhold.user.controller;

import com.quiz.darkhold.home.model.GameInfo;
import com.quiz.darkhold.login.entity.User;
import com.quiz.darkhold.login.service.UserService;
import com.quiz.darkhold.user.entity.User;
import com.quiz.darkhold.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.quiz.darkhold.user.controller;

import com.quiz.darkhold.user.entity.User;
import com.quiz.darkhold.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class UserController {
private final Logger logger = LogManager.getLogger(UserController.class);
private final UserService userService;

public UserController(final UserService userService) {
this.userService = userService;
}

/**
* on to create challenge page.
*
* @return create challenge page
*/
@GetMapping("/userManagement")
public String manageUsers(final Model model) {
logger.info("Into the manageUsers method");
var listUsers = userService.listAll();
model.addAttribute("listusers", listUsers);
return "user/usermanagement";
}

@GetMapping("/user/create")
public String createUser(final Model model) {
logger.info("Into the createUser method");
var roles = userService.listRoles();
var user = new User();
user.setEnabled(true);
model.addAttribute("userForm", user);
model.addAttribute("listRoles", roles);
return "user/createuser";
}

@PostMapping("/users/save")
public String saveUser(final User user, final RedirectAttributes redirectAttributes) {
logger.info("Into the saveUser method, user is {}", user);
userService.save(user);
redirectAttributes.addFlashAttribute("message", "The user has been saved successfully");
return "redirect:/userManagement";
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.quiz.darkhold.user.controller;

import com.quiz.darkhold.user.service.UserService;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserRestController {
private final UserService userService;

public UserRestController(final UserService userService) {
this.userService = userService;
}

@PostMapping("/users/check_email")
public String checkDuplicateEmail(@Param("email") final String email) {
return userService.isEmailUnique(email) ? "OK" : "DUPLICATED";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.quiz.darkhold.login.entity;
package com.quiz.darkhold.user.entity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.quiz.darkhold.login.entity;
package com.quiz.darkhold.user.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.quiz.darkhold.login.entity;
package com.quiz.darkhold.user.entity;


import jakarta.persistence.Column;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.quiz.darkhold.login.repository;
package com.quiz.darkhold.user.repository;

import com.quiz.darkhold.login.entity.Role;
import com.quiz.darkhold.user.entity.Role;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.quiz.darkhold.login.repository;
package com.quiz.darkhold.user.repository;

import com.quiz.darkhold.login.entity.User;
import com.quiz.darkhold.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.quiz.darkhold.login.service;
package com.quiz.darkhold.user.service;

public interface SecurityService {
String findLoggedInUsername();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.quiz.darkhold.login.service;
package com.quiz.darkhold.user.service;

import com.quiz.darkhold.util.CommonUtils;
import org.apache.logging.log4j.LogManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.quiz.darkhold.login.service;
package com.quiz.darkhold.user.service;

import com.quiz.darkhold.login.entity.DarkholdUserDetails;
import com.quiz.darkhold.login.repository.UserRepository;
import com.quiz.darkhold.user.entity.DarkholdUserDetails;
import com.quiz.darkhold.user.repository.UserRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.core.userdetails.UserDetails;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.quiz.darkhold.login.service;
package com.quiz.darkhold.user.service;

import com.quiz.darkhold.login.entity.Role;
import com.quiz.darkhold.login.entity.User;
import com.quiz.darkhold.user.entity.Role;
import com.quiz.darkhold.user.entity.User;

import java.util.List;

Expand All @@ -13,5 +13,7 @@ public interface UserService {
List<User> listAll();

List<Role> listRoles();

Boolean isEmailUnique(String email);
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.quiz.darkhold.login.service;
package com.quiz.darkhold.user.service;

import com.quiz.darkhold.login.entity.Role;
import com.quiz.darkhold.login.entity.User;
import com.quiz.darkhold.login.repository.RoleRepository;
import com.quiz.darkhold.login.repository.UserRepository;
import com.quiz.darkhold.user.entity.Role;
import com.quiz.darkhold.user.entity.User;
import com.quiz.darkhold.user.repository.RoleRepository;
import com.quiz.darkhold.user.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
Expand All @@ -13,8 +13,6 @@

@Service
public class UserServiceImpl implements UserService {

private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
private final UserRepository userRepository;
private final RoleRepository roleRepository;
private final BCryptPasswordEncoder encoder;
Expand Down Expand Up @@ -48,4 +46,9 @@ public List<User> listAll() {
public List<Role> listRoles() {
return (List<Role>) roleRepository.findAll();
}

@Override
public Boolean isEmailUnique(String email) {
return userRepository.findByEmail(email) == null;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.quiz.darkhold.login.validator;
package com.quiz.darkhold.user.validator;

import com.quiz.darkhold.login.entity.User;
import com.quiz.darkhold.login.service.UserService;
import com.quiz.darkhold.user.entity.User;
import com.quiz.darkhold.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
Expand Down
15 changes: 15 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,22 @@ insert into member (id, firstname, lastname, email, password, enabled, createdOn
(1, 'Administrator','Of Darkhold', 'admin@admin.com', '$2a$10$l0Y76CoFAdXlkZZYkVDsIeRP..4UcpuhQ01H0B4QvTNF2uATZx9Gq',
true, '2022-12-27 00:00:00','2022-12-27 00:00:00','admin','admin');

insert into member (id, firstname, lastname, email, password, enabled, createdOn, modifiedOn, createdBy, modifiedBy) values
(2, 'Guest','Of Darkhold', 'guest@guest.com', '$2a$10$l0Y76CoFAdXlkZZYkVDsIeRP..4UcpuhQ01H0B4QvTNF2uATZx9Gq',
true, '2024-02-14 00:00:00','2024-02-14 00:00:00','admin','admin');

insert into member (id, firstname, lastname, email, password, enabled, createdOn, modifiedOn, createdBy, modifiedBy) values
(3, 'Manager','Of Darkhold', 'manager@guest.com', '$2a$10$l0Y76CoFAdXlkZZYkVDsIeRP..4UcpuhQ01H0B4QvTNF2uATZx9Gq',
true, '2024-02-14 00:00:00','2024-02-14 00:00:00','admin','admin');

insert into member (id, firstname, lastname, email, password, enabled, createdOn, modifiedOn, createdBy, modifiedBy) values
(4, 'Participant','Of Darkhold', 'participant@guest.com', '$2a$10$l0Y76CoFAdXlkZZYkVDsIeRP..4UcpuhQ01H0B4QvTNF2uATZx9Gq',
true, '2024-02-14 00:00:00','2024-02-14 00:00:00','admin','admin');

insert into member_roles(users_id, roles_id) values (1,1);
insert into member_roles(users_id, roles_id) values (2,2);
insert into member_roles(users_id, roles_id) values (3,3);
insert into member_roles(users_id, roles_id) values (4,4);

insert into challenge (challenge_id, title, description, challengeowner, createdOn, modifiedOn, createdBy, modifiedBy) values
(1, 'Challenge 01', 'new Challenges', 1, '2020-04-08 00:00:00','2020-04-08 00:00:00','admin','admin');
Expand Down
Loading

0 comments on commit b4baefd

Please sign in to comment.