Commit 25af49a3 authored by Matija Obreza's avatar Matija Obreza
Browse files

User admin controller updated

- Get user by UUID only
- Set roles
- Set password
parent dc1464cc
......@@ -39,14 +39,14 @@ public interface UserRepository extends JpaRepository<User, Long>, QueryDslPredi
@Query("select u from User u where lower(u.email) = lower(?1)")
User findByEmail(String email);
/**
* Find by uuid and version.
*
* @param uuid the uuid
* @param version the version
* @return the user
*/
User getByUuidAndVersion(String uuid, int version);
/**
* Find by uuid and version.
*
* @param uuid the uuid
* @param version the version
* @return the user
*/
User getByUuidAndVersion(String uuid, int version);
/**
* Autocomplete user by email or fullName
......@@ -58,4 +58,13 @@ public interface UserRepository extends JpaRepository<User, Long>, QueryDslPredi
@Query("select u from User u where lower(u.email) like concat(lower(?1), '%') or lower(u.fullName) like concat(lower(?1), '%')")
List<User> autocomplete(String term, Pageable page);
/**
* Find by uuid.
*
* @param string the uuid
* @return the user
*/
// FIXME User#uuid should be UUID
User findByUuid(String string);
}
......@@ -31,6 +31,15 @@ import org.springframework.security.access.prepost.PreAuthorize;
*/
public interface UserService extends BasicUserService<UserRole, User> {
/**
* Get user by uuid
*
* @param uuid user's uuid
* @return the user
*/
// FIXME add to app-blocks
User getUser(UUID uuid);
/**
* Autocomplete user.
*
......@@ -40,23 +49,23 @@ public interface UserService extends BasicUserService<UserRole, User> {
*/
List<User> autocompleteUser(String term, int limit);
/**
* Gets the user.
*
* @param uuid the uuid
* @param version the version
* @return the user
*/
User getUser(UUID uuid, int version);
/**
* List users matching the filter.
*
* @param filter filter data
* @param page page
* @return list of User
*/
@PreAuthorize("hasRole('ADMINISTRATOR')")
Page<User> listUsers(UserFilter filter, Pageable page);
/**
* Gets the user.
*
* @param uuid the uuid
* @param version the version
* @return the user
*/
User getUser(UUID uuid, int version);
/**
* List users matching the filter.
*
* @param filter filter data
* @param page page
* @return list of User
*/
@PreAuthorize("hasRole('ADMINISTRATOR')")
Page<User> listUsers(UserFilter filter, Pageable page);
}
......@@ -90,6 +90,11 @@ public class UserServiceImpl extends BasicUserServiceImpl<UserRole, User> implem
return deepLoad(userRepository.findByEmail(email));
}
@Override
public User getUser(UUID uuid) {
return deepLoad(userRepository.findByUuid(uuid.toString()));
}
@Override
@Transactional
public User createUser(String email, String fullName, String password, AccountType accountType) throws NotUniqueUserException, PasswordPolicyException, UserException {
......
......@@ -16,10 +16,12 @@
package org.genesys.catalog.server.controller.api.v0.admin;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import org.genesys.blocks.security.UserException;
import org.genesys.catalog.model.user.User;
import org.genesys.catalog.model.user.UserRole;
import org.genesys.catalog.service.UserService;
import org.genesys.catalog.service.filters.UserFilter;
import org.genesys.catalog.util.RandomPasswordUtil;
......@@ -31,6 +33,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestBody;
......@@ -42,44 +45,118 @@ import org.springframework.web.bind.annotation.RestController;
* The Class UserAdminController.
*
* @author Maxym Borodenko
* @author Matija Obreza
*/
@RestController
@RequestMapping(UserAdminController.API_BASE)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class UserAdminController {
protected static final String API_BASE = "/api/v0/admin";
/** The Constant API_BASE. */
protected static final String API_BASE = "/api/v0/admin/user";
private static final Logger LOG = LoggerFactory.getLogger(UserAdminController.class);
private static final Logger LOG = LoggerFactory.getLogger(UserAdminController.class);
@Autowired
private UserService userService;
@Autowired
private UserService userService;
@PostMapping(value = "/create")
public User addUser(@RequestBody final User user) throws UserException {
final String password = RandomPasswordUtil.generatePassword(new Random(), 15);
return userService.createUser(user.getEmail(), user.getFullName(), password, user.getAccountType());
}
/**
* Adds the user.
*
* @param user the user
* @return the user
* @throws UserException the user exception
*/
@PostMapping(value = "/create")
public User addUser(@RequestBody final User user) throws UserException {
final String password = RandomPasswordUtil.generatePassword(new Random(), 15);
return userService.createUser(user.getEmail(), user.getFullName(), password, user.getAccountType());
}
@PostMapping(value = "/update")
public User updaterUser(@RequestBody final User user) throws UserException {
return userService.updateUser(user, user.getEmail(), user.getFullName());
}
/**
* Gets the user
*
* @param uuid the uuid
* @return the user
* @throws UserException the user exception
*/
@GetMapping(value = "/{UUID}")
public User get(@PathVariable("UUID") final UUID uuid) throws UserException {
return userService.getUser(uuid);
}
@DeleteMapping(value = "/delete/{UUID},{version}")
public User deleteUser(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version) {
final User user = userService.getUser(uuid, version);
userService.deleteUser(user);
user.setId(null);
return user;
}
/**
* Updater user.
*
* @param user the user
* @return the user
* @throws UserException the user exception
*/
@PostMapping(value = "/update")
public User updaterUser(@RequestBody final User user) throws UserException {
return userService.updateUser(userService.getUser(UUID.fromString(user.getUuid()), user.getVersion().intValue()), user.getEmail(), user.getFullName());
}
@PostMapping(value = "/list")
public Page<User> list(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
@RequestParam(name = "l", required = false, defaultValue = "50") final int pageSize,
@RequestParam(name = "d", required = false, defaultValue = "ASC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "id") final String[] sort,
@RequestBody final UserFilter filter) {
return userService.listUsers(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort));
}
/**
* Sets the roles.
*
* @param uuid the uuid
* @param version the version
* @param roles the roles
* @return the user
* @throws UserException the user exception
*/
@PostMapping(value = "/roles/{UUID},{version}")
public User setRoles(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version, @RequestBody Set<UserRole> roles) throws UserException {
return userService.setRoles(userService.getUser(uuid, version), roles);
}
/**
* Sets the password.
*
* @param uuid the uuid
* @param version the version
* @param password the password
* @return the user
* @throws UserException the user exception
*/
@PostMapping(value = "/password/{UUID},{version}")
public User setPassword(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version, @RequestBody String password) throws UserException {
LOG.warn("Attempting to set user uuid={} password", uuid);
return userService.changePassword(userService.getUser(uuid, version), password);
}
/**
* Delete user.
*
* @param uuid the uuid
* @param version the version
* @return the user
*/
@DeleteMapping(value = "/delete/{UUID},{version}")
public User deleteUser(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version) {
final User user = userService.getUser(uuid, version);
userService.deleteUser(user);
user.setId(null);
return user;
}
/**
* List.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filter the filter
* @return the page
*/
@PostMapping(value = "/list")
public Page<User> list(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
@RequestParam(name = "l", required = false, defaultValue = "50") final int pageSize,
@RequestParam(name = "d", required = false, defaultValue = "ASC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "id") final String[] sort, @RequestBody final UserFilter filter) {
return userService.listUsers(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment