Browse Source

Adding existsBy methods

main
ddangelorb 3 years ago
parent
commit
7d4424f4e0
  1. 2
      index.html
  2. 46
      src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java
  3. 2
      src/main/java/org/waterproofingdata/wpdauth/dto/UsersRequestDTO.java
  4. 2
      src/main/java/org/waterproofingdata/wpdauth/security/WebSecurityConfig.java
  5. 6
      src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java
  6. 14
      src/test/java/org/waterproofingdata/wpdauth/integrationtest/UsersServiceIntegrationTest.java
  7. 64
      swagger.yaml

2
index.html

@ -1,5 +1,5 @@
<!DOCTYPE html>
<!-- V1.0.3.0.4-->
<!-- V1.0.3.0.5-->
<html lang="en">
<head>

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

@ -31,6 +31,50 @@ public class UsersController {
@Autowired
private UsersService userService;
@PostMapping("/existsByUsername")
@ApiOperation(
value = "${UserController.existsByUsername}",
notes = "From a username, this method returns if there is a username in db or not."
)
@ApiResponses(value = {//
@ApiResponse(code = 400, message = "Something went wrong")
}
)
public boolean existsByUsername(//
@ApiParam(
name = "username",
type = "String",
value = "username of the user",
example = "This is an unique field, and consumers should be aware of it. By convention, WP6 should send the user phone number (i.e. (99)99999-9999).",
required = true
)
@RequestParam String username
) {
return userService.existsByUsername(username);
}
@PostMapping("/existsByNickname")
@ApiOperation(
value = "${UserController.existsByNickname}",
notes = "From a nickname, this method returns if there is a nickname in db or not."
)
@ApiResponses(value = {//
@ApiResponse(code = 400, message = "Something went wrong")
}
)
public boolean existsByNickname(//
@ApiParam(
name = "nickname",
type = "String",
value = "nickname of the user",
example = "This is an unique field, and consumers should be aware of it.",
required = true
)
@RequestParam String nickname
) {
return userService.existsByNickname(nickname);
}
@PostMapping("/login")
@ApiOperation(
value = "${UserController.login}",
@ -77,7 +121,7 @@ public class UsersController {
name = "user",
value = "Signup User",
required = true
)
)
@RequestBody UsersRequestDTO user
) {
return userService.signup(CustomMapper.map(user, Users.class));

2
src/main/java/org/waterproofingdata/wpdauth/dto/UsersRequestDTO.java

@ -139,7 +139,7 @@ public class UsersRequestDTO {
name = "roles",
dataType = "String",
value = "roles of the user.",
example = "i.e. ROLE_CLIENT.",
example = "i.e. [ROLE_CLIENT].",
required = true
)
private List<Roles> roles;

2
src/main/java/org/waterproofingdata/wpdauth/security/WebSecurityConfig.java

@ -35,6 +35,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
http.authorizeRequests()//
.antMatchers("/users/login").permitAll()//
.antMatchers("/users/signup").permitAll()//
.antMatchers("/users/existsByUsername").permitAll()//
.antMatchers("/users/existsByNickname").permitAll()//
.antMatchers("/h2-console/**/**").permitAll()
// Disallow everything else..
.anyRequest().authenticated();

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

@ -67,6 +67,10 @@ public class UsersService {
return usersRepository.existsByUsername(username);
}
public boolean existsByNickname(String nickname) {
return usersRepository.existsByNickname(nickname);
}
public Users search(String username) {
Users user = usersRepository.findByUsername(username);
if (user == null) {
@ -101,7 +105,7 @@ public class UsersService {
if (user.getNickname().length() == 0) {
throw new CustomException("Nickname must be provided", HttpStatus.UNPROCESSABLE_ENTITY);
}
else if (usersRepository.existsByNickname(user.getNickname())) {
else if (existsByNickname(user.getNickname())) {
String nickname_unique = usersRepository.findSuggestedNickname(user.getNickname());
throw new CustomException(String.format("Nickname already exists. Would you like to use '%s'?", nickname_unique), HttpStatus.UNPROCESSABLE_ENTITY);
}

14
src/test/java/org/waterproofingdata/wpdauth/integrationtest/UsersServiceIntegrationTest.java

@ -16,6 +16,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.waterproofingdata.wpdauth.dto.UsersRequestDTO;
import org.waterproofingdata.wpdauth.exception.CustomException;
import org.waterproofingdata.wpdauth.model.EduCemadenOrganizations;
import org.waterproofingdata.wpdauth.model.Roles;
@ -52,6 +53,17 @@ public class UsersServiceIntegrationTest {
u.setTermsofusage(true);
u.setRoles(new ArrayList<Roles>(Arrays.asList(role)));
String uJson = new Gson().toJson(u);
UsersRequestDTO urDTO = new UsersRequestDTO();
urDTO.setUsername(uName);
urDTO.setNickname(uName);
urDTO.setPassword(UUID.randomUUID().toString());
urDTO.setState("SP");
urDTO.setCity("São Paulo");
urDTO.setTermsofusage(true);
urDTO.setRoles(new ArrayList<Roles>(Arrays.asList(role)));
String urDTOJson = new Gson().toJson(u);
return u;
}
@ -90,6 +102,8 @@ public class UsersServiceIntegrationTest {
Users u = setUpUserTest("user_", Roles.ROLE_CLIENT);
String signup = usersService.signup(u);
assertNotNull(signup, "Signup token returned from usersService.signup(user) should not be null");
assertEquals(true, usersService.existsByUsername(u.getUsername()));
assertEquals(true, usersService.existsByNickname(u.getNickname()));
}
@Test

64
swagger.yaml

@ -1,7 +1,7 @@
swagger: '2.0'
info:
description: 'This is a sample JWT authentication service. You can find out more about JWT at [https://jwt.io/](https://jwt.io/). For this sample, you can use the `admin` or `client` users (password: admin and client respectively) to test the authorization filters. Once you have successfully logged in and obtained the token, you should click on the right top button `Authorize` and introduce it with the prefix "Bearer ".'
version: 1.0.3
version: 1.0.5
title: The authenticator for the Waterproofing Data (WPD) Work Packages
contact:
email: igsd@warwick.ac.uk
@ -263,6 +263,66 @@ paths:
- global
- apiKey: []
deprecated: false
/users/existsByNickname:
post:
tags:
- users
summary: ${UserController.existsByNickname}
description: From a nickname, this method returns if there is a nickname in db or not.
operationId: existsByNicknameUsingPOST
consumes:
- application/json
produces:
- '*/*'
parameters:
- name: nickname
in: query
description: nickname of the user
required: true
type: string
allowEmptyValue: false
x-example: This is an unique field, and consumers should be aware of it.
responses:
'200':
description: OK
schema:
type: boolean
'400':
description: Something went wrong
security:
- Authorization:
- global
deprecated: false
/users/existsByUsername:
post:
tags:
- users
summary: ${UserController.existsByUsername}
description: From a username, this method returns if there is a username in db or not.
operationId: existsByUsernameUsingPOST
consumes:
- application/json
produces:
- '*/*'
parameters:
- name: username
in: query
description: username of the user
required: true
type: string
allowEmptyValue: false
x-example: This is an unique field, and consumers should be aware of it. By convention, WP6 should send the user phone number (i.e. (99)99999-9999).
responses:
'200':
description: OK
schema:
type: boolean
'400':
description: Something went wrong
security:
- Authorization:
- global
deprecated: false
/users/login:
post:
tags:
@ -555,7 +615,7 @@ definitions:
description: terms of usage of the user.
roles:
type: array
example: i.e. ROLE_CLIENT.
example: i.e. [ROLE_CLIENT].
description: roles of the user.
items:
type: string

Loading…
Cancel
Save