Browse Source

Adding findById method and fixing ROLE return bug

main
ddangelorb 3 years ago
parent
commit
e572f73065
  1. 2
      index.html
  2. 30
      src/main/java/org/waterproofingdata/wpdauth/controller/UsersController.java
  3. 9
      src/main/java/org/waterproofingdata/wpdauth/dto/UsersResponseDTO.java
  4. 4
      src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java
  5. 6
      src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java
  6. 12
      src/test/java/org/waterproofingdata/wpdauth/integrationtest/UsersServiceIntegrationTest.java
  7. 54
      swagger.yaml

2
index.html

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

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

@ -30,6 +30,36 @@ import io.swagger.annotations.Authorization;
public class UsersController { public class UsersController {
@Autowired @Autowired
private UsersService userService; private UsersService userService;
@GetMapping(value = "/{id}")
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_INSTITUTION') or hasRole('ROLE_CLIENT')")
@ApiOperation(
value = "${UserController.findById}",
response = UsersResponseDTO.class,
authorizations = { @Authorization(value="apiKey") },
notes = "This is the user findById search method."
)
@ApiResponses(value = {//
@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 UsersResponseDTO findById (
@ApiParam(
name = "id",
type = "Integer",
value = "user id",
example = "A positive numeric id.",
required = true
)
@RequestParam Integer id
) {
UsersResponseDTO urDTO = CustomMapper.map(userService.findById(id), UsersResponseDTO.class);
urDTO.setEduCemadenOrganization(userService.findEduCemadenOrganizationById(urDTO.getId()));
urDTO.setProviderActivationKey(userService.findProviderActivationKeyById(urDTO.getId()));
return urDTO;
}
@PostMapping("/existsByUsername") @PostMapping("/existsByUsername")
@ApiOperation( @ApiOperation(

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

@ -5,6 +5,7 @@ import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.sql.Date; import java.sql.Date;
import java.util.List;
import org.waterproofingdata.wpdauth.model.EduCemadenOrganizations; import org.waterproofingdata.wpdauth.model.EduCemadenOrganizations;
import org.waterproofingdata.wpdauth.model.Roles; import org.waterproofingdata.wpdauth.model.Roles;
@ -149,13 +150,13 @@ public class UsersResponseDTO {
@ApiModelProperty( @ApiModelProperty(
position = 13, position = 13,
name = "institutiontype", name = "institutiontype",
dataType = "String",
value = "institution type of the user.",
example = "i.e. 'ROLE_ADMIN' means system administrator, 'ROLE_INSTITUTION' means institution administrator, 'ROLE_CLIENT' means regular users",
dataType = "List<Roles>",
value = "list of institution type of the user.",
example = "i.e. ['ROLE_ADMIN'] means system administrator, ['ROLE_INSTITUTION'] means institution administrator, ['ROLE_CLIENT'] means regular users",
allowableValues = "{@code ROLE_ADMIN, ROLE_INSTITUTION, ROLE_CLIENT}", allowableValues = "{@code ROLE_ADMIN, ROLE_INSTITUTION, ROLE_CLIENT}",
required = false required = false
) )
Roles role;
List<Roles> roles;
@ApiModelProperty( @ApiModelProperty(
position = 14, position = 14,

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

@ -1,5 +1,7 @@
package org.waterproofingdata.wpdauth.repository; package org.waterproofingdata.wpdauth.repository;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
@ -8,6 +10,8 @@ import org.waterproofingdata.wpdauth.model.Users;
@Transactional @Transactional
public interface UsersRepository extends JpaRepository<Users, Integer> { public interface UsersRepository extends JpaRepository<Users, Integer> {
Optional<Users> findById(Integer id);
boolean existsByUsername(String username); boolean existsByUsername(String username);
Users findByUsername(String username); Users findByUsername(String username);

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

@ -65,6 +65,10 @@ public class UsersService {
} }
} }
public Users findById(Integer id) {
return usersRepository.findById(id).orElseThrow(() -> new CustomException("The user doesn't exist", HttpStatus.NOT_FOUND));
}
public boolean existsByUsername(String username) { public boolean existsByUsername(String username) {
return usersRepository.existsByUsername(username); return usersRepository.existsByUsername(username);
} }
@ -181,7 +185,7 @@ public class UsersService {
} }
public Users whoami(HttpServletRequest req) { public Users whoami(HttpServletRequest req) {
return usersRepository.findByUsername(jwtTokenProvider.getUsername(jwtTokenProvider.resolveToken(req)));
return usersRepository.findByUsername(jwtTokenProvider.getUsername(jwtTokenProvider.resolveToken(req)));
} }
public EduCemadenOrganizations findEduCemadenOrganizationById(Integer userid) { public EduCemadenOrganizations findEduCemadenOrganizationById(Integer userid) {

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

@ -97,6 +97,18 @@ public class UsersServiceIntegrationTest {
assertEquals(Roles.ROLE_ADMIN, u.getRoles().get(0)); assertEquals(Roles.ROLE_ADMIN, u.getRoles().get(0));
} }
@Test
public void testAdmUserFindById() {
Users u = usersService.findById(1);
assertEquals("admin", u.getUsername());
assertEquals("admin", u.getNickname());
assertEquals("SP", u.getState());
assertEquals("São Paulo", u.getCity());
assertEquals(true, u.getTermsofusage());
assertEquals(1, u.getActive());
assertEquals(Roles.ROLE_ADMIN, u.getRoles().get(0));
}
@Test @Test
public void testRandomUserSignup() { public void testRandomUserSignup() {
Users u = setUpUserTest("user_", Roles.ROLE_CLIENT); Users u = setUpUserTest("user_", Roles.ROLE_CLIENT);

54
swagger.yaml

@ -1,7 +1,7 @@
swagger: '2.0' swagger: '2.0'
info: 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 ".' 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.5
version: 1.0.4
title: The authenticator for the Waterproofing Data (WPD) Work Packages title: The authenticator for the Waterproofing Data (WPD) Work Packages
contact: contact:
email: igsd@warwick.ac.uk email: igsd@warwick.ac.uk
@ -435,6 +435,40 @@ paths:
- Authorization: - Authorization:
- global - global
deprecated: false deprecated: false
/users/{id}:
get:
tags:
- users
summary: ${UserController.findById}
description: This is the user findById search method.
operationId: findByIdUsingGET
produces:
- '*/*'
parameters:
- name: id
in: query
description: user id
required: true
type: integer
format: int32
allowEmptyValue: false
x-example: A positive numeric id.
responses:
'200':
description: OK
schema:
$ref: '#/definitions/UsersResponseDTO'
'403':
description: Access denied
'404':
description: The user doesn't exist
'500':
description: Expired or invalid JWT token
security:
- Authorization:
- global
- apiKey: []
deprecated: false
/users/{username}: /users/{username}:
get: get:
tags: tags:
@ -699,14 +733,16 @@ definitions:
format: int32 format: int32
example: i.e. true. example: i.e. true.
description: whether user is active or not. description: whether user is active or not.
role:
type: string
example: i.e. 'ROLE_ADMIN' means system administrator, 'ROLE_INSTITUTION' means institution administrator, 'ROLE_CLIENT' means regular users
description: institution type of the user.
enum:
- '{@code ROLE_ADMIN'
- ROLE_INSTITUTION
- ROLE_CLIENT}
roles:
type: array
example: i.e. ['ROLE_ADMIN'] means system administrator, ['ROLE_INSTITUTION'] means institution administrator, ['ROLE_CLIENT'] means regular users
description: list of institution type of the user.
items:
type: string
enum:
- '{@code ROLE_ADMIN'
- ROLE_INSTITUTION
- ROLE_CLIENT}
eduCemadenOrganization: eduCemadenOrganization:
description: which Educational Cemaden Organization the user belongs. description: which Educational Cemaden Organization the user belongs.
$ref: '#/definitions/EduCemadenOrganizations' $ref: '#/definitions/EduCemadenOrganizations'

Loading…
Cancel
Save