From 8c49aed6e5a2e8b2b12e19df7b5316b12f492217 Mon Sep 17 00:00:00 2001 From: ddangelorb Date: Mon, 16 Aug 2021 14:39:38 -0300 Subject: [PATCH] Activation key structure --- db/ddl.sql | 2 ++ .../wpdauth/controller/UsersController.java | 6 ++-- .../wpdauth/dto/UsersResponseDTO.java | 10 ++++++ .../model/EduCemadenOrganizations.java | 2 ++ .../wpdauth/model/Users.java | 1 + .../UsersRolesproviderActivationKey.java | 31 +++++++++++++++++++ .../EduCemadenOrganizationsRepository.java | 11 +++++++ .../wpdauth/repository/UsersRepository.java | 8 ----- ...sRolesproviderActivationKeyRepository.java | 10 ++++++ .../wpdauth/service/UsersService.java | 17 +++++++--- 10 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 src/main/java/org/waterproofingdata/wpdauth/model/UsersRolesproviderActivationKey.java create mode 100644 src/main/java/org/waterproofingdata/wpdauth/repository/UsersRolesproviderActivationKeyRepository.java diff --git a/db/ddl.sql b/db/ddl.sql index a8dde35..57e5359 100644 --- a/db/ddl.sql +++ b/db/ddl.sql @@ -22,6 +22,7 @@ CREATE TABLE IF NOT EXISTS users_roles ( ); CREATE TABLE IF NOT EXISTS users_rolesprovider_activationkey ( + id SERIAL PRIMARY KEY, users_id INT NOT NULL, roles_id INT NOT NULL, activationkey uuid NOT NULL, @@ -44,6 +45,7 @@ CREATE TABLE IF NOT EXISTS educemaden_organizations ( ); CREATE TABLE IF NOT EXISTS users_educemaden_organizations ( + id SERIAL PRIMARY KEY, users_id INT NOT NULL, educemaden_organizations_id INT NOT NULL, activationkey uuid NOT NULL, diff --git a/src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java b/src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java index 1e4b58b..092e39b 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java +++ b/src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java @@ -42,7 +42,7 @@ public class UsersController { public String login(// @ApiParam("Username") @RequestParam String username, // @ApiParam("Password") @RequestParam String password) { - return userService.signin(username, password); + return userService.login(username, password); } @PostMapping("/signup") @@ -63,8 +63,8 @@ public class UsersController { @ApiResponse(code = 403, message = "Access denied"), // @ApiResponse(code = 404, message = "The user doesn't exist"), // @ApiResponse(code = 500, message = "Expired or invalid JWT token")}) - public String activate(@ApiParam("Username") @PathVariable String username) { - userService.activate(username); + public String activate(@ApiParam("Username") @PathVariable String username, @ApiParam("ActivationKey") @PathVariable String activationkey) { + userService.activate(username, activationkey); return username; } diff --git a/src/main/java/org/waterproofingdata/wpdauth/dto/UsersResponseDTO.java b/src/main/java/org/waterproofingdata/wpdauth/dto/UsersResponseDTO.java index a580822..269de36 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/dto/UsersResponseDTO.java +++ b/src/main/java/org/waterproofingdata/wpdauth/dto/UsersResponseDTO.java @@ -6,15 +6,25 @@ import io.swagger.annotations.ApiModelProperty; import lombok.Getter; import lombok.Setter; +import org.waterproofingdata.wpdauth.model.EduCemadenOrganizations; import org.waterproofingdata.wpdauth.model.Roles; +import org.waterproofingdata.wpdauth.model.UsersRolesproviderActivationKey; @Getter @Setter public class UsersResponseDTO { @ApiModelProperty(position = 0) private Integer id; + @ApiModelProperty(position = 1) private String username; + @ApiModelProperty(position = 2) List roles; + + @ApiModelProperty(position = 3) + EduCemadenOrganizations eduCemadenOrganization; + + @ApiModelProperty(position = 4) + List rolesProviderActivationKey; } diff --git a/src/main/java/org/waterproofingdata/wpdauth/model/EduCemadenOrganizations.java b/src/main/java/org/waterproofingdata/wpdauth/model/EduCemadenOrganizations.java index 90e4f40..3312631 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/model/EduCemadenOrganizations.java +++ b/src/main/java/org/waterproofingdata/wpdauth/model/EduCemadenOrganizations.java @@ -45,4 +45,6 @@ public class EduCemadenOrganizations { @Column(nullable = true) private String responsible; + + private String activationkey; } diff --git a/src/main/java/org/waterproofingdata/wpdauth/model/Users.java b/src/main/java/org/waterproofingdata/wpdauth/model/Users.java index 71c85a4..cefcdc9 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/model/Users.java +++ b/src/main/java/org/waterproofingdata/wpdauth/model/Users.java @@ -46,6 +46,7 @@ public class Users { @Column(nullable = true) private String surname; + @Column(nullable = true) private String avatar; @Column(nullable = false) diff --git a/src/main/java/org/waterproofingdata/wpdauth/model/UsersRolesproviderActivationKey.java b/src/main/java/org/waterproofingdata/wpdauth/model/UsersRolesproviderActivationKey.java new file mode 100644 index 0000000..eedabbe --- /dev/null +++ b/src/main/java/org/waterproofingdata/wpdauth/model/UsersRolesproviderActivationKey.java @@ -0,0 +1,31 @@ +package org.waterproofingdata.wpdauth.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + + +@Entity(name = "users_rolesprovider_activationkey") +@Getter +@Setter +@NoArgsConstructor +public class UsersRolesproviderActivationKey { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(nullable = false, name="users_id") + private Integer usersid; + + @Column(nullable = false, name="roles_id") + private Integer rolesid; + + @Column(nullable = false) + private String activationkey; +} diff --git a/src/main/java/org/waterproofingdata/wpdauth/repository/EduCemadenOrganizationsRepository.java b/src/main/java/org/waterproofingdata/wpdauth/repository/EduCemadenOrganizationsRepository.java index 3100e34..9f706f8 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/repository/EduCemadenOrganizationsRepository.java +++ b/src/main/java/org/waterproofingdata/wpdauth/repository/EduCemadenOrganizationsRepository.java @@ -1,8 +1,19 @@ package org.waterproofingdata.wpdauth.repository; +import org.springframework.data.jpa.repository.Query; + +import javax.transaction.Transactional; + import org.springframework.data.jpa.repository.JpaRepository; import org.waterproofingdata.wpdauth.model.EduCemadenOrganizations; public interface EduCemadenOrganizationsRepository extends JpaRepository { EduCemadenOrganizations findByPhone(String phone); + + @Query(value = "SELECT e.*, ueo.activationkey FROM educemaden_organizations e INNER JOIN users_educemaden_organizations ueo ON e.id = ueo.educemaden_organizations_id WHERE ueo.users_id = ?1;", nativeQuery = true) + EduCemadenOrganizations findByUserId(Integer idUser); + + @Transactional + @Query(value = "INSERT INTO users_educemaden_organizations(id, users_id, educemaden_organizations_id, activationkey) VALUES (DEFAULT, ?1, ?2, ?3);", nativeQuery = true) + void insertUsersEduCemadenOrganizations(Integer idUser, Integer idEduCemadenOrganizations, String activationkey); } diff --git a/src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java b/src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java index 9aa98c8..a1ab469 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java +++ b/src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java @@ -15,12 +15,4 @@ public interface UsersRepository extends JpaRepository { @Transactional @Query(value = "UPDATE users SET active = ?2 WHERE username = ?1", nativeQuery = true) void activateByUsername(String username, Integer active); - - @Transactional - @Query(value = "INSERT INTO users_rolesprovider_activationkey(users_id, roles_id, activationkey) VALUES VALUES (?1, ?2, ?3);", nativeQuery = true) - void insertUsersRolesProvider(Integer idUser, Integer idRoles, String activationkey); - - @Transactional - @Query(value = "INSERT INTO users_educemaden_organizations(users_id, educemaden_organizations_id, activationkey) VALUES (?1, ?2, ?3);", nativeQuery = true) - void insertUsersEduCemadenOrganizations(Integer idUser, Integer idEduCemadenOrganizations, String activationkey); } diff --git a/src/main/java/org/waterproofingdata/wpdauth/repository/UsersRolesproviderActivationKeyRepository.java b/src/main/java/org/waterproofingdata/wpdauth/repository/UsersRolesproviderActivationKeyRepository.java new file mode 100644 index 0000000..b793d7b --- /dev/null +++ b/src/main/java/org/waterproofingdata/wpdauth/repository/UsersRolesproviderActivationKeyRepository.java @@ -0,0 +1,10 @@ +package org.waterproofingdata.wpdauth.repository; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.waterproofingdata.wpdauth.model.UsersRolesproviderActivationKey; + +public interface UsersRolesproviderActivationKeyRepository extends JpaRepository { + List findByUsersid(Integer usersid); +} diff --git a/src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java b/src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java index 8c3864e..81a6795 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java +++ b/src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java @@ -1,5 +1,6 @@ package org.waterproofingdata.wpdauth.service; +import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; @@ -12,6 +13,7 @@ import org.springframework.stereotype.Service; import org.waterproofingdata.wpdauth.exception.CustomException; import org.waterproofingdata.wpdauth.model.Users; +import org.waterproofingdata.wpdauth.repository.EduCemadenOrganizationsRepository; import org.waterproofingdata.wpdauth.repository.UsersRepository; import org.waterproofingdata.wpdauth.security.JwtTokenProvider; @@ -19,6 +21,9 @@ import org.waterproofingdata.wpdauth.security.JwtTokenProvider; public class UsersService { @Autowired private UsersRepository userRepository; + + @Autowired + private EduCemadenOrganizationsRepository eduCemadenOrganizationsRepository; @Autowired private PasswordEncoder passwordEncoder; @@ -33,11 +38,12 @@ public class UsersService { return userRepository.existsByUsername(username); } - public String signin(String username, String password) { + public String login(String username, String password) { try { authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password)); return jwtTokenProvider.createToken(username, userRepository.findByUsername(username).getRoles()); - } catch (AuthenticationException e) { + } + catch (AuthenticationException e) { throw new CustomException("Invalid username/password supplied", HttpStatus.UNPROCESSABLE_ENTITY); } } @@ -45,14 +51,17 @@ public class UsersService { public String signup(Users user) { if (!existsByUsername(user.getUsername())) { user.setPassword(passwordEncoder.encode(user.getPassword())); + user.setActive(0); userRepository.save(user); return jwtTokenProvider.createToken(user.getUsername(), user.getRoles()); - } else { + } + else { throw new CustomException("Username is already in use", HttpStatus.UNPROCESSABLE_ENTITY); } } - public void activate(String username) { + public void activate(String username, String activationkey) { + String uuid = UUID.randomUUID().toString(); //userRepository.activateByUsername(username); }