"...lifpf.git" did not exist on "ed2fa462216360da1b48114020bc8c49753dac5f"
Newer
Older
package com.mif13.authServer.controllers;
import com.mif13.authServer.dao.UsersDao;
import com.mif13.authServer.model.User;
import com.mif13.authServer.model.User.UserCreationException;
import java.util.regex.PatternSyntaxException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
public class UserRestController {
private final UsersDao usersRepo;
@Autowired
public UserRestController(UsersDao usersRepo) {
this.usersRepo = usersRepo;
}
@Operation(summary = "Get user informations by id")
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = "Found the user",
content = {
@Content(
mediaType = "application/json",
schema = @Schema(implementation = User.class)
),
@Content(
mediaType = "application/xml",
schema = @Schema(implementation = User.class)
),
@Content(
mediaType = "text/html"
)
}),
@ApiResponse(
responseCode = "404",
description = "User not found",
content = {
@Content()
}
)}
)
@GetMapping(value = "/{id}", produces = {MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<User> getUserAsJsonOrXml(@PathVariable String id) {
ResponseEntity<User> response;
Optional<User> optionalUser = usersRepo.get(id);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
response = new ResponseEntity<>(user, HttpStatus.OK);
response = new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return response;
}
@GetMapping(value = "/{id}", produces = MediaType.TEXT_HTML_VALUE)
public ModelAndView getUserAsHtml(@PathVariable String id, Model model) {
// "user" nom du template HTML (sans extension)
ModelAndView modelAndView = new ModelAndView("user");
Optional<User> optionalUser = usersRepo.get(id);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
// on initialise les variables du template
model.addAttribute("user", user);
modelAndView.setStatus(HttpStatus.OK);
} else {
model.addAttribute("id", id);
modelAndView.setStatus(HttpStatus.NOT_FOUND);
}
return modelAndView;
}
private ResponseEntity<Void> createUser(User user) {
ResponseEntity<Void> response;
try {
Optional<User> optionalUser = usersRepo.get(user.getLogin());
if (optionalUser.isEmpty()) {
usersRepo.save(user);
response = new ResponseEntity<>(HttpStatus.CREATED);
} else {
response = new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
} catch (PatternSyntaxException e) {
e.printStackTrace();
response = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return response;
}
/**
* Cree un User a partir des parametres URL Encoded.
* Renvoie 400 BAD_REQUEST si le login ou le password sont invalides
*
* @param login Login
* @param password Password
* @return contenu vide
*/
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
@Operation(summary = "Create a new user")
@ApiResponses(value = {
@ApiResponse(
responseCode = "201 Created",
description = "User is correctely created",
content = {
@Content(
mediaType = "application/json",
schema = @Schema(implementation = User.class)
),
@Content(
mediaType = "application/xml",
schema = @Schema(implementation = User.class)
),
@Content(
mediaType = "text/html"
)
}),
@ApiResponse(
responseCode = "403 Forbidden",
description = "User not created",
content = {
@Content()
}
),
@ApiResponse(
responseCode = "500 Internal Server Error",
description = "",
content = {
@Content()
}
)
})
@PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<Void> createUserFromUrlEncoded(@RequestParam("login") String login,
@RequestParam("password") String password) {
ResponseEntity<Void> response;
User user = new User(login, password);
response = createUser(user);
} catch (UserCreationException e) {
e.printStackTrace();
response = new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return response;
}
/**
* Cree un User a partir d'un JSON.
*
* /!\ Attention /!\
* Renvoie 400 BAD_REQUEST si le login ou le password sont invalides
* (et produit une HttpMessageNotReadableException)
*
* @param user JSON converti en User (converter de Spring)
* @return contenu vide
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Void> createUserFromJson(@RequestBody User user) {
return createUser(user);
}
@Operation(summary = "Modify password of a user by its id")
@ApiResponses(value = {
@ApiResponse(
responseCode = "204 No Content",
description = "Password is correctly modified",
content = {
@Content()
}
),
@ApiResponse(
responseCode = "404 Not Found",
description = "",
content = {
@Content()
}
),
@ApiResponse(
responseCode = "500 Internal Server Error",
description = "",
content = {
@Content()
}
)
})
@PutMapping("/{id}")
public ResponseEntity<Void> modifyUserPassword(@PathVariable String id,
@RequestParam("new_password") String new_password) {
ResponseEntity<Void> response;
Optional<User> optionalUser = usersRepo.get(id);
User user = optionalUser.get();
user.setPassword(new_password);
response = new ResponseEntity<>(HttpStatus.NO_CONTENT);
} else {
response = new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
e.printStackTrace();
response = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return response;
}
@Operation(summary = "Delet a user on the database, by its id")
@ApiResponses(value = {
@ApiResponse(
responseCode = "204 No Content",
description = "User deleted with success",
content = {
@Content()
}
),
@ApiResponse(
responseCode = "404 Not Found",
description = "",
content = {
@Content()
}
),
@ApiResponse(
responseCode = "500 Internal Server Error",
description = "",
content = {
@Content()
}
)
})
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable String id) {
ResponseEntity<Void> response;
Optional<User> optionalUser = usersRepo.get(id);
User user = optionalUser.get();
usersRepo.delete(user);
response = new ResponseEntity<>(HttpStatus.NO_CONTENT);
} else {
response = new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
e.printStackTrace();
response = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return response;
}