Skip to content

Commit

Permalink
[SHIBUI-1031]
Browse files Browse the repository at this point in the history
First pass at POST and PUT endpoints.
  • Loading branch information
Bill Smith committed Dec 12, 2018
1 parent 0c26b6f commit 926c7b9
Showing 1 changed file with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
package edu.internet2.tier.shibboleth.admin.ui.security.controller;

import edu.internet2.tier.shibboleth.admin.ui.controller.ErrorResponse;
import edu.internet2.tier.shibboleth.admin.ui.security.model.User;
import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository;
<<<<<<< Updated upstream
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
=======
import org.springframework.beans.factory.annotation.Required;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
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;
>>>>>>> Stashed changes
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.client.HttpClientErrorException;

import java.util.List;
import java.util.Optional;

import static org.springframework.http.HttpStatus.NOT_FOUND;

Expand All @@ -22,7 +37,7 @@
* @author Dmitriy Kopylenko
*/
@RestController
@RequestMapping("/api/admin/users")
@RequestMapping("/api/admin")
public class UsersController {

private UserRepository userRepository;
Expand All @@ -32,19 +47,19 @@ public UsersController(UserRepository userRepository) {
}

@Transactional(readOnly = true)
@GetMapping
@GetMapping("/users")
public List<User> getAll() {
return userRepository.findAll();
}

@Transactional(readOnly = true)
@GetMapping("/{username}")
@GetMapping("/user/{username}")
public ResponseEntity<?> getOne(@PathVariable String username) {
return ResponseEntity.ok(findUserOrThrowHttp404(username));
}

@Transactional
@DeleteMapping("/{username}")
@DeleteMapping("/user/{username}")
public ResponseEntity<?> deleteOne(@PathVariable String username) {
User user = findUserOrThrowHttp404(username);
userRepository.delete(user);
Expand All @@ -57,4 +72,37 @@ private User findUserOrThrowHttp404(String username) {
.orElseThrow(() -> new HttpClientErrorException(NOT_FOUND, String.format("User with username [%s] not found", username)));
}

@Transactional
@PostMapping("/user")
ResponseEntity<?> saveUser(@RequestParam User user) {
Optional<User> persistedUser = userRepository.findByUsername(user.getUsername());
if (persistedUser.isPresent()) {
return ResponseEntity
.status(HttpStatus.CONFLICT)
.body(new ErrorResponse(String.valueOf(HttpStatus.CONFLICT.value()),
String.format("A user with username [ %s ] already exists within the system.", user.getUsername())));
}
//TODO: encrypt password?
User savedUser = userRepository.save(user);
return ResponseEntity.ok(savedUser);
}

@Transactional
@PutMapping("/user/{username}")
ResponseEntity<?> updateUser(@PathVariable(value = "username") String username, @RequestParam User user) {
Optional<User> userSearchResult = userRepository.findByUsername(username);
if (!userSearchResult.isPresent()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(String.valueOf(HttpStatus.BAD_REQUEST.value()),
String.format("No user with username [ %s ] exists within the system.", username)));
}
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
User persistedUser = userSearchResult.get();
persistedUser.setPassword(passwordEncoder.encode(user.getPassword());
persistedUser.setFirstName(user.getFirstName());
persistedUser.setLastName(user.getLastName());
persistedUser.setRoles(user.getRoles());
User savedUser = userRepository.save(persistedUser);
return ResponseEntity.ok(savedUser);
}
}

0 comments on commit 926c7b9

Please sign in to comment.