Browse Source

Activation key structure

main
ddangelorb 3 years ago
parent
commit
8c49aed6e5
  1. 2
      db/ddl.sql
  2. 6
      src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java
  3. 10
      src/main/java/org/waterproofingdata/wpdauth/dto/UsersResponseDTO.java
  4. 2
      src/main/java/org/waterproofingdata/wpdauth/model/EduCemadenOrganizations.java
  5. 1
      src/main/java/org/waterproofingdata/wpdauth/model/Users.java
  6. 31
      src/main/java/org/waterproofingdata/wpdauth/model/UsersRolesproviderActivationKey.java
  7. 11
      src/main/java/org/waterproofingdata/wpdauth/repository/EduCemadenOrganizationsRepository.java
  8. 8
      src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java
  9. 10
      src/main/java/org/waterproofingdata/wpdauth/repository/UsersRolesproviderActivationKeyRepository.java
  10. 17
      src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java

2
db/ddl.sql

@ -22,6 +22,7 @@ CREATE TABLE IF NOT EXISTS users_roles (
); );
CREATE TABLE IF NOT EXISTS users_rolesprovider_activationkey ( CREATE TABLE IF NOT EXISTS users_rolesprovider_activationkey (
id SERIAL PRIMARY KEY,
users_id INT NOT NULL, users_id INT NOT NULL,
roles_id INT NOT NULL, roles_id INT NOT NULL,
activationkey uuid 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 ( CREATE TABLE IF NOT EXISTS users_educemaden_organizations (
id SERIAL PRIMARY KEY,
users_id INT NOT NULL, users_id INT NOT NULL,
educemaden_organizations_id INT NOT NULL, educemaden_organizations_id INT NOT NULL,
activationkey uuid NOT NULL, activationkey uuid NOT NULL,

6
src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java

@ -42,7 +42,7 @@ public class UsersController {
public String login(// public String login(//
@ApiParam("Username") @RequestParam String username, // @ApiParam("Username") @RequestParam String username, //
@ApiParam("Password") @RequestParam String password) { @ApiParam("Password") @RequestParam String password) {
return userService.signin(username, password);
return userService.login(username, password);
} }
@PostMapping("/signup") @PostMapping("/signup")
@ -63,8 +63,8 @@ public class UsersController {
@ApiResponse(code = 403, message = "Access denied"), // @ApiResponse(code = 403, message = "Access denied"), //
@ApiResponse(code = 404, message = "The user doesn't exist"), // @ApiResponse(code = 404, message = "The user doesn't exist"), //
@ApiResponse(code = 500, message = "Expired or invalid JWT token")}) @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; return username;
} }

10
src/main/java/org/waterproofingdata/wpdauth/dto/UsersResponseDTO.java

@ -6,15 +6,25 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.waterproofingdata.wpdauth.model.EduCemadenOrganizations;
import org.waterproofingdata.wpdauth.model.Roles; import org.waterproofingdata.wpdauth.model.Roles;
import org.waterproofingdata.wpdauth.model.UsersRolesproviderActivationKey;
@Getter @Getter
@Setter @Setter
public class UsersResponseDTO { public class UsersResponseDTO {
@ApiModelProperty(position = 0) @ApiModelProperty(position = 0)
private Integer id; private Integer id;
@ApiModelProperty(position = 1) @ApiModelProperty(position = 1)
private String username; private String username;
@ApiModelProperty(position = 2) @ApiModelProperty(position = 2)
List<Roles> roles; List<Roles> roles;
@ApiModelProperty(position = 3)
EduCemadenOrganizations eduCemadenOrganization;
@ApiModelProperty(position = 4)
List<UsersRolesproviderActivationKey> rolesProviderActivationKey;
} }

2
src/main/java/org/waterproofingdata/wpdauth/model/EduCemadenOrganizations.java

@ -45,4 +45,6 @@ public class EduCemadenOrganizations {
@Column(nullable = true) @Column(nullable = true)
private String responsible; private String responsible;
private String activationkey;
} }

1
src/main/java/org/waterproofingdata/wpdauth/model/Users.java

@ -46,6 +46,7 @@ public class Users {
@Column(nullable = true) @Column(nullable = true)
private String surname; private String surname;
@Column(nullable = true)
private String avatar; private String avatar;
@Column(nullable = false) @Column(nullable = false)

31
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;
}

11
src/main/java/org/waterproofingdata/wpdauth/repository/EduCemadenOrganizationsRepository.java

@ -1,8 +1,19 @@
package org.waterproofingdata.wpdauth.repository; 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.springframework.data.jpa.repository.JpaRepository;
import org.waterproofingdata.wpdauth.model.EduCemadenOrganizations; import org.waterproofingdata.wpdauth.model.EduCemadenOrganizations;
public interface EduCemadenOrganizationsRepository extends JpaRepository<EduCemadenOrganizations, Integer> { public interface EduCemadenOrganizationsRepository extends JpaRepository<EduCemadenOrganizations, Integer> {
EduCemadenOrganizations findByPhone(String phone); 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);
} }

8
src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java

@ -15,12 +15,4 @@ public interface UsersRepository extends JpaRepository<Users, Integer> {
@Transactional @Transactional
@Query(value = "UPDATE users SET active = ?2 WHERE username = ?1", nativeQuery = true) @Query(value = "UPDATE users SET active = ?2 WHERE username = ?1", nativeQuery = true)
void activateByUsername(String username, Integer active); 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);
} }

10
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<UsersRolesproviderActivationKey, Integer> {
List<UsersRolesproviderActivationKey> findByUsersid(Integer usersid);
}

17
src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java

@ -1,5 +1,6 @@
package org.waterproofingdata.wpdauth.service; package org.waterproofingdata.wpdauth.service;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; 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.exception.CustomException;
import org.waterproofingdata.wpdauth.model.Users; import org.waterproofingdata.wpdauth.model.Users;
import org.waterproofingdata.wpdauth.repository.EduCemadenOrganizationsRepository;
import org.waterproofingdata.wpdauth.repository.UsersRepository; import org.waterproofingdata.wpdauth.repository.UsersRepository;
import org.waterproofingdata.wpdauth.security.JwtTokenProvider; import org.waterproofingdata.wpdauth.security.JwtTokenProvider;
@ -19,6 +21,9 @@ import org.waterproofingdata.wpdauth.security.JwtTokenProvider;
public class UsersService { public class UsersService {
@Autowired @Autowired
private UsersRepository userRepository; private UsersRepository userRepository;
@Autowired
private EduCemadenOrganizationsRepository eduCemadenOrganizationsRepository;
@Autowired @Autowired
private PasswordEncoder passwordEncoder; private PasswordEncoder passwordEncoder;
@ -33,11 +38,12 @@ public class UsersService {
return userRepository.existsByUsername(username); return userRepository.existsByUsername(username);
} }
public String signin(String username, String password) {
public String login(String username, String password) {
try { try {
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password)); authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
return jwtTokenProvider.createToken(username, userRepository.findByUsername(username).getRoles()); return jwtTokenProvider.createToken(username, userRepository.findByUsername(username).getRoles());
} catch (AuthenticationException e) {
}
catch (AuthenticationException e) {
throw new CustomException("Invalid username/password supplied", HttpStatus.UNPROCESSABLE_ENTITY); throw new CustomException("Invalid username/password supplied", HttpStatus.UNPROCESSABLE_ENTITY);
} }
} }
@ -45,14 +51,17 @@ public class UsersService {
public String signup(Users user) { public String signup(Users user) {
if (!existsByUsername(user.getUsername())) { if (!existsByUsername(user.getUsername())) {
user.setPassword(passwordEncoder.encode(user.getPassword())); user.setPassword(passwordEncoder.encode(user.getPassword()));
user.setActive(0);
userRepository.save(user); userRepository.save(user);
return jwtTokenProvider.createToken(user.getUsername(), user.getRoles()); return jwtTokenProvider.createToken(user.getUsername(), user.getRoles());
} else {
}
else {
throw new CustomException("Username is already in use", HttpStatus.UNPROCESSABLE_ENTITY); 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); //userRepository.activateByUsername(username);
} }

Loading…
Cancel
Save