Skip to content
Snippets Groups Projects
Commit f8ebf071 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

api for retrieveing all users data

parent c23334c9
No related branches found
No related tags found
2 merge requests!115Resolve "admin panel should use API",!114Resolve "admin panel should use API"
...@@ -3,6 +3,7 @@ package lcsb.mapviewer.api.users; ...@@ -3,6 +3,7 @@ package lcsb.mapviewer.api.users;
import java.io.IOException; import java.io.IOException;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
...@@ -71,6 +72,15 @@ public class UserController extends BaseController { ...@@ -71,6 +72,15 @@ public class UserController extends BaseController {
) throws SecurityException, ObjectNotFoundException { ) throws SecurityException, ObjectNotFoundException {
return userRest.getUser(token, login, columns); return userRest.getUser(token, login, columns);
} }
@RequestMapping(value = "/users/", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getUsers(//
@CookieValue(value = Configuration.AUTH_TOKEN) String token, //
@PathVariable(value = "login") String login, //
@RequestParam(value = "columns", defaultValue = "") String columns//
) throws SecurityException, ObjectNotFoundException {
return userRest.getUsers(token, columns);
}
@RequestMapping(value = "/doLogout", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE }) @RequestMapping(value = "/doLogout", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public Map<String, String> logout(@CookieValue(value = Configuration.AUTH_TOKEN) String token, public Map<String, String> logout(@CookieValue(value = Configuration.AUTH_TOKEN) String token,
......
...@@ -10,6 +10,7 @@ import java.util.Set; ...@@ -10,6 +10,7 @@ import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.ObjectNotFoundException; import lcsb.mapviewer.api.ObjectNotFoundException;
import lcsb.mapviewer.common.exception.InvalidArgumentException; import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.user.BasicPrivilege; import lcsb.mapviewer.model.user.BasicPrivilege;
...@@ -19,44 +20,25 @@ import lcsb.mapviewer.model.user.User; ...@@ -19,44 +20,25 @@ import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.services.SecurityException; import lcsb.mapviewer.services.SecurityException;
import lcsb.mapviewer.services.interfaces.ILayoutService; import lcsb.mapviewer.services.interfaces.ILayoutService;
import lcsb.mapviewer.services.interfaces.IUserService; import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.services.view.AuthenticationToken;
@Transactional(value = "txManager") @Transactional(value = "txManager")
public class UserRestImpl { public class UserRestImpl extends BaseRestImpl {
@Autowired
private IUserService userService;
@Autowired @Autowired
private ILayoutService layoutService; private ILayoutService layoutService;
/**
* @return the userService
* @see #userService
*/
public IUserService getUserService() {
return userService;
}
/**
* @param userService
* the userService to set
* @see #userService
*/
public void setUserService(IUserService userService) {
this.userService = userService;
}
public Map<String, Object> getUser(String token, String login, String columns) throws SecurityException, ObjectNotFoundException { public Map<String, Object> getUser(String token, String login, String columns) throws SecurityException, ObjectNotFoundException {
User ownUserData = userService.getUserByToken(token); User ownUserData = getUserService().getUserByToken(token);
Set<String> columnSet = createUserColumnSet(columns); Set<String> columnSet = createUserColumnSet(columns);
boolean isAdmin = userService.userHasPrivilege(ownUserData, PrivilegeType.USER_MANAGEMENT); boolean isAdmin = getUserService().userHasPrivilege(ownUserData, PrivilegeType.USER_MANAGEMENT);
if (ownUserData.getLogin().equals(login)) { if (ownUserData.getLogin().equals(login)) {
return prepareUse(ownUserData, columnSet, true); return prepareUse(ownUserData, columnSet, true);
} else if (isAdmin) { } else if (isAdmin) {
User user = userService.getUserByLogin(login); User user = getUserService().getUserByLogin(login);
if (user == null) { if (user == null) {
throw new ObjectNotFoundException("User doesn't exist"); throw new ObjectNotFoundException("User doesn't exist");
} }
...@@ -172,4 +154,18 @@ public class UserRestImpl { ...@@ -172,4 +154,18 @@ public class UserRestImpl {
this.layoutService = layoutService; this.layoutService = layoutService;
} }
public List<Map<String, Object>> getUsers(String token, String columns) throws SecurityException {
AuthenticationToken authenticationToken = getUserService().getToken(token);
User ownUserData = getUserService().getUserByToken(token);
boolean isAdmin = getUserService().userHasPrivilege(ownUserData, PrivilegeType.USER_MANAGEMENT);
Set<String> columnSet = createUserColumnSet(columns);
List<Map<String, Object>> result = new ArrayList<>();
for (User user : getUserService().getUsers(authenticationToken)) {
result.add(prepareUse(user, columnSet, isAdmin));
}
return result;
}
} }
...@@ -14,7 +14,7 @@ import lcsb.mapviewer.api.users.UserRestImpl; ...@@ -14,7 +14,7 @@ import lcsb.mapviewer.api.users.UserRestImpl;
import lcsb.mapviewer.common.Configuration; import lcsb.mapviewer.common.Configuration;
public class UserRestImplTest extends RestTestFunctions { public class UserRestImplTest extends RestTestFunctions {
Logger logger = Logger.getLogger(UserRestImplTest.class); Logger logger = Logger.getLogger(UserRestImplTest.class);
@Autowired @Autowired
UserRestImpl userRestImpl; UserRestImpl userRestImpl;
...@@ -32,7 +32,7 @@ public class UserRestImplTest extends RestTestFunctions { ...@@ -32,7 +32,7 @@ public class UserRestImplTest extends RestTestFunctions {
} }
@Test @Test
public void test() throws Exception { public void testGetUser() throws Exception {
try { try {
Object response = userRestImpl.getUser(token.getId(), Configuration.ANONYMOUS_LOGIN, ""); Object response = userRestImpl.getUser(token.getId(), Configuration.ANONYMOUS_LOGIN, "");
assertNotNull(response); assertNotNull(response);
...@@ -42,4 +42,15 @@ public class UserRestImplTest extends RestTestFunctions { ...@@ -42,4 +42,15 @@ public class UserRestImplTest extends RestTestFunctions {
} }
} }
@Test
public void testGetUsers() throws Exception {
try {
Object response = userRestImpl.getUsers(adminToken.getId(), "");
assertNotNull(response);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
} }
...@@ -207,7 +207,7 @@ public class UserService implements IUserService { ...@@ -207,7 +207,7 @@ public class UserService implements IUserService {
public List<UserView> getAllUserRows() { public List<UserView> getAllUserRows() {
List<Project> projects = projectDao.getAll(); List<Project> projects = projectDao.getAll();
List<UserView> result = new ArrayList<UserView>(); List<UserView> result = new ArrayList<>();
List<User> fullList = userDao.getAll(); List<User> fullList = userDao.getAll();
for (User user : fullList) { for (User user : fullList) {
result.add(userViewFactory.create(user, projects)); result.add(userViewFactory.create(user, projects));
...@@ -581,4 +581,13 @@ public class UserService implements IUserService { ...@@ -581,4 +581,13 @@ public class UserService implements IUserService {
throw new SecurityException("You cannot access data of other users"); throw new SecurityException("You cannot access data of other users");
} }
} }
@Override
public List<User> getUsers(AuthenticationToken token) throws SecurityException {
if (userHasPrivilege(token, PrivilegeType.USER_MANAGEMENT)) {
return userDao.getAll();
} else {
throw new SecurityException("You have no access to users data");
}
}
} }
...@@ -256,4 +256,6 @@ public interface IUserService { ...@@ -256,4 +256,6 @@ public interface IUserService {
boolean userHasPrivilege(AuthenticationToken token, PrivilegeType addMap); boolean userHasPrivilege(AuthenticationToken token, PrivilegeType addMap);
User getUserById(String creatorId, AuthenticationToken authenticationToken) throws SecurityException; User getUserById(String creatorId, AuthenticationToken authenticationToken) throws SecurityException;
List<User> getUsers(AuthenticationToken token) throws SecurityException;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment