Skip to content

Commit 7aadac0

Browse files
committed
Access given to admin to update user and its profile
1 parent b2ba18f commit 7aadac0

3 files changed

Lines changed: 31 additions & 22 deletions

File tree

src/main/java/com/sopromadze/blogapi/controller/TodoController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ public ResponseEntity<?> deleteTodo(@PathVariable(value = "id") Long id, @Curren
5959
}
6060

6161
@PutMapping("/{id}/complete")
62+
@PreAuthorize("hasRole('USER')")
6263
public ResponseEntity<?> completeTodo(@PathVariable(value = "id") Long id, @CurrentUser UserPrincipal currentUser){
6364
return todoService.completeTodo(id, currentUser);
6465
}
6566

6667
@PutMapping("/{id}/unComplete")
68+
@PreAuthorize("hasRole('USER')")
6769
public ResponseEntity<?> unCompleteTodo(@PathVariable(value = "id") Long id, @CurrentUser UserPrincipal currentUser){
6870
return todoService.unCompleteTodo(id, currentUser);
6971
}

src/main/java/com/sopromadze/blogapi/controller/UserController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public ResponseEntity<?> addUser(@Valid @RequestBody User user){
7676
}
7777

7878
@PutMapping("/{username}")
79-
@PreAuthorize("hasRole('USER')")
79+
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
8080
public ResponseEntity<?> updateUser(@Valid @RequestBody User newUser, @PathVariable(value = "username") String username, @CurrentUser UserPrincipal currentUser){
8181
return userService.updateUser(newUser, username, currentUser);
8282
}
@@ -100,7 +100,7 @@ public ResponseEntity<?> takeAdmin(@PathVariable(name = "username") String usern
100100
}
101101

102102
@PutMapping("/setOrUpdateInfo")
103-
@PreAuthorize("hasRole('USER')")
103+
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
104104
public ResponseEntity<?> setAddress(@CurrentUser UserPrincipal currentUser, @Valid @RequestBody InfoRequest infoRequest){
105105
return userService.setOrUpdateInfo(currentUser, infoRequest);
106106
}

src/main/java/com/sopromadze/blogapi/service/UserService.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.springframework.beans.factory.annotation.Autowired;
1717
import org.springframework.http.HttpStatus;
1818
import org.springframework.http.ResponseEntity;
19+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
1920
import org.springframework.security.crypto.password.PasswordEncoder;
2021
import org.springframework.stereotype.Service;
2122

@@ -79,19 +80,22 @@ public ResponseEntity<?> addUser(User user){
7980

8081
public ResponseEntity<?> updateUser(User newUser, String username, UserPrincipal currentUser){
8182
User user = userRepository.findByUsername(username).orElseThrow(() -> new ResourceNotFoundException("User", "username", username));
82-
if(!user.getId().equals(currentUser.getId())){
83-
return new ResponseEntity<>(new ApiResponse(false, "You don't have permission to update profile of: " + username), HttpStatus.UNAUTHORIZED);
83+
if(user.getId().equals(currentUser.getId()) || currentUser.getAuthorities().contains(new SimpleGrantedAuthority(RoleName.ROLE_ADMIN.toString()))){
84+
user.setFirstName(newUser.getFirstName());
85+
user.setLastName(newUser.getLastName());
86+
user.setPassword(passwordEncoder.encode(newUser.getPassword()));
87+
user.setAddress(newUser.getAddress());
88+
user.setPhone(newUser.getPhone());
89+
user.setWebsite(newUser.getWebsite());
90+
user.setCompany(newUser.getCompany());
91+
92+
User updatedUser = userRepository.save(user);
93+
return new ResponseEntity<>(updatedUser, HttpStatus.OK);
94+
8495
}
85-
user.setFirstName(newUser.getFirstName());
86-
user.setLastName(newUser.getLastName());
87-
user.setPassword(passwordEncoder.encode(newUser.getPassword()));
88-
user.setAddress(newUser.getAddress());
89-
user.setPhone(newUser.getPhone());
90-
user.setWebsite(newUser.getWebsite());
91-
user.setCompany(newUser.getCompany());
92-
93-
User updatedUser = userRepository.save(user);
94-
return new ResponseEntity<>(updatedUser, HttpStatus.OK);
96+
97+
return new ResponseEntity<>(new ApiResponse(false, "You don't have permission to update profile of: " + username), HttpStatus.UNAUTHORIZED);
98+
9599
}
96100

97101
public ResponseEntity<?> deleteUser(String username, UserPrincipal currentUser){
@@ -128,16 +132,19 @@ public ResponseEntity<?> setOrUpdateInfo(UserPrincipal currentUser, InfoRequest
128132
Geo geo = new Geo(infoRequest.getLat(), infoRequest.getLng());
129133
Address address = new Address(infoRequest.getStreet(), infoRequest.getSuite(), infoRequest.getCity(), infoRequest.getZipcode(), geo);
130134
Company company = new Company(infoRequest.getCompanyName(), infoRequest.getCatchPhrase(), infoRequest.getBs());
131-
user.setAddress(address);
132-
user.setCompany(company);
133-
user.setWebsite(infoRequest.getWebsite());
134-
user.setPhone(infoRequest.getPhone());
135-
User updatedUser = userRepository.save(user);
135+
if (user.getId().equals(currentUser.getId()) || currentUser.getAuthorities().contains(new SimpleGrantedAuthority(RoleName.ROLE_ADMIN.toString()))){
136+
user.setAddress(address);
137+
user.setCompany(company);
138+
user.setWebsite(infoRequest.getWebsite());
139+
user.setPhone(infoRequest.getPhone());
140+
User updatedUser = userRepository.save(user);
136141

137-
Long postCount = postRepository.countByCreatedBy(updatedUser.getId());
142+
Long postCount = postRepository.countByCreatedBy(updatedUser.getId());
138143

139144

140-
UserProfile userProfile = new UserProfile(updatedUser.getId(), updatedUser.getUsername(), updatedUser.getFirstName(), updatedUser.getLastName(), updatedUser.getCreatedAt(), updatedUser.getEmail(), updatedUser.getAddress(), updatedUser.getPhone(), updatedUser.getWebsite(), updatedUser.getCompany(), postCount);
141-
return new ResponseEntity<>(userProfile, HttpStatus.OK);
145+
UserProfile userProfile = new UserProfile(updatedUser.getId(), updatedUser.getUsername(), updatedUser.getFirstName(), updatedUser.getLastName(), updatedUser.getCreatedAt(), updatedUser.getEmail(), updatedUser.getAddress(), updatedUser.getPhone(), updatedUser.getWebsite(), updatedUser.getCompany(), postCount);
146+
return new ResponseEntity<>(userProfile, HttpStatus.OK);
147+
}
148+
return new ResponseEntity<>(new ApiResponse(false, "You don't have permission to update users profile"), HttpStatus.OK);
142149
}
143150
}

0 commit comments

Comments
 (0)