Commit 9f369dbd authored by Mamadou Diallo's avatar Mamadou Diallo
Browse files

update

parent 1c6e5b2a
......@@ -43,7 +43,7 @@ title %0
@REM set %HOME% to equivalent of $HOME
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
@REM Execute a user defined script before this one
@REM Execute a userEntity defined script before this one
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
......
......@@ -23,6 +23,11 @@
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
......@@ -31,7 +36,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
......@@ -49,6 +53,10 @@
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
......
package com.example.server.config;
import com.example.server.model.IUser;
import com.example.server.model.User;
import com.example.server.model.UserEntity;
import com.example.server.utils.Const;
import com.example.server.utils.PasswordEncoderHelper;
import com.example.server.utils.Encoders;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
......@@ -16,19 +16,19 @@ import java.util.stream.Stream;
@Configuration
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final List<IUser> users = Stream.of(
new User("Diallo", "Mamadou", "greer", "Greer", Const.ADMIN),
new User("Dupont", "Jean Pierre", "jdupont", "Jdupont", Const.USER),
new User("Poulain", "Emanuel", "epoulain", "Epoulain", Const.USER)
private final List<User> users = Stream.of(
new UserEntity("Diallo", "Mamadou", "greer", "Greer", Const.ADMIN),
new UserEntity("Dupont", "Jean Pierre", "jdupont", "Jdupont", Const.USER),
new UserEntity("Poulain", "Emanuel", "epoulain", "Epoulain", Const.USER)
).collect(Collectors.toList());
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
User user = new User("Diallo", "Mamadou", "greer", "Greer", Const.ADMIN);
User user = new UserEntity("Diallo", "Mamadou", "greer", "Greer", Const.ADMIN);
auth.inMemoryAuthentication().withUser(user.getUserName())
.password(PasswordEncoderHelper.passwordEncoder().encode(user.getPassword()))
.password(Encoders.userPasswordEncoder().encode(user.getPassword()))
.roles(user.getRole());
}
......@@ -46,5 +46,4 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
.anyRequest().authenticated();
}
}
......@@ -3,7 +3,7 @@ package com.example.server.controller;
import com.example.server.exceptions.CommentNotFoundException;
import com.example.server.model.Comment;
import com.example.server.model.Post;
import com.example.server.model.User;
import com.example.server.model.UserEntity;
import com.example.server.repository.CommentRepository;
import com.example.server.repository.PostRepository;
import com.example.server.repository.UserRepository;
......@@ -34,7 +34,7 @@ public class CommentController {
public ResponseEntity<Comment> add(@RequestBody JsonNode jsonNode, @PathVariable String postId,
@PathVariable String userid) throws JsonProcessingException {
User author = userRepository.getUserById(userid);
UserEntity author = userRepository.getUserById(userid);
Post post = postRepository.getPostById(postId);
Comment comment = mapper.treeToValue(jsonNode, Comment.class);
......@@ -50,15 +50,17 @@ public class CommentController {
}
@PutMapping("/{postId}/{commentId}")
public ResponseEntity<Comment> update(@RequestBody JsonNode jsonNode, @PathVariable String postId, @PathVariable String commentId) {
public ResponseEntity<Comment> update(@RequestBody JsonNode jsonNode, @PathVariable String postId, @PathVariable String commentId) throws JsonProcessingException {
if(!repository.existsById(commentId)){
throw new CommentNotFoundException(commentId);
}
Comment comment = repository.getCommentById(commentId);
Post post = postRepository.getPostById(postId);
post.deleteComment(comment);
//post.deleteComment(comment);
Comment newComment = mapper.treeToValue(jsonNode, Comment.class);
comment.setMessage(newComment.getMessage());
postRepository.save(post);
postRepository.deleteById(postId);
......
......@@ -2,7 +2,7 @@ package com.example.server.controller;
import com.example.server.exceptions.PostNotFoundException;
import com.example.server.model.Post;
import com.example.server.model.User;
import com.example.server.model.UserEntity;
import com.example.server.repository.PostRepository;
import com.example.server.repository.UserRepository;
import com.example.server.utils.DateGenerator;
......@@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@RestController
@RequiredArgsConstructor
......@@ -31,7 +30,7 @@ public class PostController {
public ResponseEntity<Post> add(@RequestBody JsonNode jsonNode, @PathVariable String userid) {
User author = userRepository.getUserById(userid);
UserEntity author = userRepository.getUserById(userid);
Post post = mapper.convertValue(jsonNode, Post.class);
post.setAuthor(author);
post.setDate(DateGenerator.generateDate());
......@@ -49,8 +48,8 @@ public class PostController {
Post post = repository.getPostById(id);
Post newPost = mapper.treeToValue(jsonNode, Post.class);
User user = userRepository.getUserById(post.getAuthor().getId());
newPost.setAuthor(user);
UserEntity userEntity = userRepository.getUserById(post.getAuthor().getId());
newPost.setAuthor(userEntity);
newPost.setDate(post.getDate());
System.out.println(newPost);
......
package com.example.server.controller;
import com.example.server.exceptions.UserNotFoundException;
import com.example.server.model.User;
import com.example.server.model.UserEntity;
import com.example.server.repository.UserRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
......@@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
......@@ -22,26 +23,32 @@ public class UserController {
private final UserRepository userRepository;
private final ObjectMapper mapper = new ObjectMapper();
/**
*
* @param jsonNode
* @return
* @throws JsonProcessingException
*/
@PostMapping("/add")
public ResponseEntity<User> add(@RequestBody JsonNode jsonNode) throws JsonProcessingException {
User user = mapper.treeToValue(jsonNode, User.class);
return new ResponseEntity<>(userRepository.save(user), HttpStatus.CREATED);
public ResponseEntity<UserEntity> add(@RequestBody JsonNode jsonNode) throws JsonProcessingException {
UserEntity userEntity = mapper.treeToValue(jsonNode, UserEntity.class);
return new ResponseEntity<>(userRepository.save(userEntity), HttpStatus.CREATED);
}
@PutMapping("/{id}")
public ResponseEntity<User> update(@PathVariable("id") String id, @RequestBody JsonNode jsonNode) throws JsonProcessingException {
public ResponseEntity<UserEntity> update(@PathVariable("id") String id, @RequestBody JsonNode jsonNode) throws JsonProcessingException {
if(!userRepository.existsById(id)){
throw new UserNotFoundException(id);
}
userRepository.deleteById(id);
User user = mapper.treeToValue(jsonNode, User.class);
return new ResponseEntity<>(userRepository.save(user), HttpStatus.OK);
UserEntity userEntity = mapper.treeToValue(jsonNode, UserEntity.class);
return new ResponseEntity<>(userRepository.save(userEntity), HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<User> getById(@PathVariable("id") String id) {
public ResponseEntity<UserEntity> getById(@PathVariable("id") String id) {
System.out.println("id : "+id);
Optional<User> user = userRepository.findById(id);
Optional<UserEntity> user = userRepository.findById(id);
return user.map(value -> new ResponseEntity<>(value, HttpStatus.OK)).orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
......@@ -55,7 +62,8 @@ public class UserController {
}
@GetMapping("/all")
public ResponseEntity<List<User>> getAll() {
@PreAuthorize("hasAnyRole('ADMIN')")
public ResponseEntity<List<UserEntity>> getAll() {
return new ResponseEntity<>(userRepository.findAll(), HttpStatus.OK);
}
......
......@@ -22,7 +22,7 @@ public class Comment{
@Field("message")
private String message;
@Field("author")
private User author;
private UserEntity author;
@Field("date")
private String date;
@Field("reply")
......@@ -42,7 +42,7 @@ public class Comment{
return message;
}
public User getAuthor() {
public UserEntity getAuthor() {
return author;
}
......@@ -62,7 +62,7 @@ public class Comment{
this.message = message;
}
public void setAuthor(User author) {
public void setAuthor(UserEntity author) {
this.author = author;
}
......
package com.example.server.model;
import java.util.Set;
import java.util.UUID;
public interface IUser {
String getId();
String getFirstName();
String getLastName();
String getUserName();
String getPassword();
String getRole();
void setFirstName(String firstName);
void setLastName(String lastName);
void setUserName(String userName);
void setPassword(String password);
void setRole(String role);
}
......@@ -5,10 +5,6 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Data
......@@ -26,7 +22,7 @@ public class Post{
@Field("content")
private String content;
@Field("author")
private User author;
private UserEntity author;
@Field("date")
private String date;
@Field("comments")
......@@ -52,7 +48,7 @@ public class Post{
return content;
}
public User getAuthor() {
public UserEntity getAuthor() {
return author;
}
......@@ -68,7 +64,7 @@ public class Post{
this.content = content;
}
public void setAuthor(User author) {
public void setAuthor(UserEntity author) {
this.author = author;
}
......
package com.example.server.model;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.util.Objects;
@Data
@Setter
@Getter
@NoArgsConstructor
@Document(collection = "user")
public class User implements IUser{
@Id
private String id;
@Field("firstName")
private String firstName;
@Field("lastName")
private String lastName;
@Field("userName")
private String userName;
@Field("password")
private String password;
@Field("role")
private String role;
public User(String firstName, String lastName, String userName, String password, String role) {
this.firstName = firstName;
this.lastName = lastName;
this.userName = userName;
this.password = password;
this.role = role;
}
@Override
public String getId() {
return id;
}
@Override
public String getFirstName() {
return firstName;
}
@Override
public String getLastName() {
return lastName;
}
@Override
public String getUserName() {
return userName;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getRole() {
return role;
}
@Override
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Override
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public void setPassword(String password) {
this.password = password;
}
@Override
public void setRole(String role) {
this.role = role;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", role=" + role +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return getId().equals(user.getId()) &&
getFirstName().equals(user.getFirstName()) &&
getLastName().equals(user.getLastName()) &&
getUserName().equals(user.getUserName()) &&
getPassword().equals(user.getPassword()) &&
getRole().equals(user.getRole());
}
@Override
public int hashCode() {
return Objects.hash(getId(), getFirstName(), getLastName(), getUserName(), getRole());
}
import java.util.Set;
import java.util.UUID;
public interface User {
String getId();
String getFirstName();
String getLastName();
String getUserName();
String getPassword();
String getRole();
void setFirstName(String firstName);
void setLastName(String lastName);
void setUserName(String userName);
void setPassword(String password);
void setRole(String role);
}
package com.example.server.model;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.util.Objects;
@Data
@Setter
@Getter
@NoArgsConstructor
@Document(collection = "user")
public class UserEntity implements User {
@Id
private String id;
@Field("firstName")
private String firstName;
@Field("lastName")
private String lastName;
@Field("userName")
private String userName;
@Field("password")
private String password;
@Field("role")
private String role;
public UserEntity(String firstName, String lastName, String userName, String password, String role) {
this.firstName = firstName;
this.lastName = lastName;
this.userName = userName;
this.password = password;
this.role = role;
}
@Override
public String getId() {
return id;
}
@Override
public String getFirstName() {
return firstName;
}
@Override
public String getLastName() {
return lastName;
}
@Override
public String getUserName() {
return userName;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getRole() {
return role;
}
@Override
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Override
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public void setPassword(String password) {
this.password = password;
}
@Override
public void setRole(String role) {
this.role = role;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", role=" + role +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof UserEntity)) return false;
UserEntity userEntity = (UserEntity) o;
return getId().equals(userEntity.getId()) &&
getFirstName().equals(userEntity.getFirstName()) &&
getLastName().equals(userEntity.getLastName()) &&
getUserName().equals(userEntity.getUserName()) &&
getPassword().equals(userEntity.getPassword()) &&
getRole().equals(userEntity.getRole());
}
@Override
public int hashCode() {
return Objects.hash(getId(), getFirstName(), getLastName(), getUserName(), getRole());
}
}
package com.example.server.repository;
import com.example.server.model.User;
import com.example.server.model.UserEntity;
import org.springframework.data.mongodb.repository.MongoRepository;
<