1
1
package com .cevheri .blog .web .rest ;
2
2
3
+ import com .cevheri .blog .domain .Post ;
3
4
import com .cevheri .blog .repository .PostRepository ;
5
+ import com .cevheri .blog .security .SecurityUtils ;
4
6
import com .cevheri .blog .service .PostService ;
5
7
import com .cevheri .blog .service .dto .PostDTO ;
6
8
import com .cevheri .blog .web .rest .errors .BadRequestAlertException ;
@@ -56,12 +58,17 @@ public PostResource(PostService postService, PostRepository postRepository) {
56
58
* @throws URISyntaxException if the Location URI syntax is incorrect.
57
59
*/
58
60
@ PostMapping ("/posts" )
59
- public ResponseEntity <PostDTO > createPost (@ Valid @ RequestBody PostDTO postDTO ) throws URISyntaxException {
61
+ public ResponseEntity <? > createPost (@ Valid @ RequestBody PostDTO postDTO ) throws URISyntaxException {
60
62
log .debug ("REST request to save Post : {}" , postDTO );
61
63
if (postDTO .getId () != null ) {
62
64
throw new BadRequestAlertException ("A new post cannot already have an ID" , ENTITY_NAME , "idexists" );
63
65
}
64
66
PostDTO result = postService .save (postDTO );
67
+
68
+ if (postDTO .getBlog () != null &&
69
+ !postDTO .getBlog ().getUser ().getLogin ().equals (SecurityUtils .getCurrentUserLogin ().orElse ("" ))) {
70
+ return new ResponseEntity <>("Unauthorized" , HttpStatus .UNAUTHORIZED );
71
+ }
65
72
return ResponseEntity
66
73
.created (new URI ("/api/posts/" + result .getId ()))
67
74
.headers (HeaderUtil .createEntityCreationAlert (applicationName , true , ENTITY_NAME , result .getId ().toString ()))
@@ -79,7 +86,7 @@ public ResponseEntity<PostDTO> createPost(@Valid @RequestBody PostDTO postDTO) t
79
86
* @throws URISyntaxException if the Location URI syntax is incorrect.
80
87
*/
81
88
@ PutMapping ("/posts/{id}" )
82
- public ResponseEntity <PostDTO > updatePost (
89
+ public ResponseEntity <? > updatePost (
83
90
@ PathVariable (value = "id" , required = false ) final Long id ,
84
91
@ Valid @ RequestBody PostDTO postDTO
85
92
) throws URISyntaxException {
@@ -95,6 +102,13 @@ public ResponseEntity<PostDTO> updatePost(
95
102
throw new BadRequestAlertException ("Entity not found" , ENTITY_NAME , "idnotfound" );
96
103
}
97
104
105
+
106
+ if (postDTO .getBlog () != null &&
107
+ !postDTO .getBlog ().getUser ().getLogin ().equals (SecurityUtils .getCurrentUserLogin ().orElse ("" ))) {
108
+ return new ResponseEntity <>("Unauthorized" , HttpStatus .UNAUTHORIZED );
109
+ }
110
+
111
+
98
112
PostDTO result = postService .update (postDTO );
99
113
return ResponseEntity
100
114
.ok ()
@@ -168,9 +182,15 @@ public ResponseEntity<List<PostDTO>> getAllPosts(
168
182
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the postDTO, or with status {@code 404 (Not Found)}.
169
183
*/
170
184
@ GetMapping ("/posts/{id}" )
171
- public ResponseEntity <PostDTO > getPost (@ PathVariable Long id ) {
185
+ public ResponseEntity <? > getPost (@ PathVariable Long id ) {
172
186
log .debug ("REST request to get Post : {}" , id );
173
187
Optional <PostDTO > postDTO = postService .findOne (id );
188
+
189
+ // if (postDTO.isPresent() && postDTO.get().getBlog() != null &&
190
+ // !postDTO.get().getBlog().getUser().getLogin().equals(SecurityUtils.getCurrentUserLogin().orElse(""))) {
191
+ // return new ResponseEntity<>("Unauthorized", HttpStatus.UNAUTHORIZED);
192
+ // }
193
+
174
194
return ResponseUtil .wrapOrNotFound (postDTO );
175
195
}
176
196
@@ -181,8 +201,16 @@ public ResponseEntity<PostDTO> getPost(@PathVariable Long id) {
181
201
* @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
182
202
*/
183
203
@ DeleteMapping ("/posts/{id}" )
184
- public ResponseEntity <Void > deletePost (@ PathVariable Long id ) {
204
+ public ResponseEntity <? > deletePost (@ PathVariable Long id ) {
185
205
log .debug ("REST request to delete Post : {}" , id );
206
+
207
+ //BusinessRule!!! Everyone can only delete their own Post.
208
+ Optional <Post > result = postRepository .findOneWithEagerRelationships (id );
209
+ if (result .isPresent () && result .get ().getBlog () != null &&
210
+ !result .get ().getBlog ().getUser ().getLogin ().equals (SecurityUtils .getCurrentUserLogin ().orElse ("" ))) {
211
+ return new ResponseEntity <>("Unauthorized" , HttpStatus .UNAUTHORIZED );
212
+ }
213
+
186
214
postService .delete (id );
187
215
return ResponseEntity
188
216
.noContent ()
0 commit comments