use of org.hisp.dhis.user.CurrentUser in project dhis2-core by dhis2.
the class MessageConversationController method removeUserFromMessageConversations.
// --------------------------------------------------------------------------
// Remove a user from one or more MessageConversations (batch operation)
// --------------------------------------------------------------------------
@DeleteMapping(produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseBody
public RootNode removeUserFromMessageConversations(@RequestParam("mc") List<String> mcUids, @RequestParam(value = "user", required = false) String userUid, HttpServletResponse response, @CurrentUser User currentUser) throws DeleteAccessDeniedException {
RootNode responseNode = new RootNode("response");
User user = userUid == null ? currentUser : userService.getUser(userUid);
if (user == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
responseNode.addChild(new SimpleNode("message", "User does not exist: " + userUid));
return responseNode;
}
if (!canModifyUserConversation(currentUser, user)) {
throw new DeleteAccessDeniedException("Not authorized to modify user: " + user.getUid());
}
Collection<org.hisp.dhis.message.MessageConversation> messageConversations = messageService.getMessageConversations(user, mcUids);
if (messageConversations.isEmpty()) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
responseNode.addChild(new SimpleNode("message", "No MessageConversations found for the given UIDs."));
return responseNode;
}
CollectionNode removed = responseNode.addChild(new CollectionNode("removed"));
for (org.hisp.dhis.message.MessageConversation mc : messageConversations) {
if (mc.remove(user)) {
messageService.updateMessageConversation(mc);
removed.addChild(new SimpleNode("uid", mc.getUid()));
}
}
response.setStatus(HttpServletResponse.SC_OK);
return responseNode;
}
use of org.hisp.dhis.user.CurrentUser in project dhis2-core by dhis2.
the class MessageConversationController method removeUserFromMessageConversation.
// --------------------------------------------------------------------------
// Remove a user from a MessageConversation
// In practice a DELETE on MessageConversation <-> User relationship
// --------------------------------------------------------------------------
@DeleteMapping(value = "/{mc-uid}/{user-uid}", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseBody
public RootNode removeUserFromMessageConversation(@PathVariable(value = "mc-uid") String mcUid, @PathVariable(value = "user-uid") String userUid, @CurrentUser User currentUser, HttpServletResponse response) throws DeleteAccessDeniedException {
RootNode responseNode = new RootNode("reply");
User user = userService.getUser(userUid);
if (user == null) {
responseNode.addChild(new SimpleNode("message", "No user with uid: " + userUid));
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return responseNode;
}
if (!canModifyUserConversation(currentUser, user)) {
throw new DeleteAccessDeniedException("Not authorized to modify user: " + user.getUid());
}
org.hisp.dhis.message.MessageConversation messageConversation = messageService.getMessageConversation(mcUid);
if (messageConversation == null) {
responseNode.addChild(new SimpleNode("message", "No messageConversation with uid: " + mcUid));
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return responseNode;
}
CollectionNode removed = responseNode.addChild(new CollectionNode("removed"));
if (messageConversation.remove(user)) {
messageService.updateMessageConversation(messageConversation);
removed.addChild(new SimpleNode("uid", messageConversation.getUid()));
}
response.setStatus(HttpServletResponse.SC_OK);
return responseNode;
}
use of org.hisp.dhis.user.CurrentUser in project dhis2-core by dhis2.
the class MessageConversationController method unmarkMessageConversationFollowup.
// --------------------------------------------------------------------------
// Clear follow up
// --------------------------------------------------------------------------
@PostMapping(value = "unfollowup", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseBody
public RootNode unmarkMessageConversationFollowup(@RequestParam(value = "user", required = false) String userUid, @RequestBody List<String> uids, HttpServletResponse response, @CurrentUser User currentUser) {
RootNode responseNode = new RootNode("response");
User user = userUid != null ? userService.getUser(userUid) : currentUser;
if (user == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
responseNode.addChild(new SimpleNode("message", "No user with uid: " + userUid));
return responseNode;
}
if (!canModifyUserConversation(currentUser, user)) {
throw new UpdateAccessDeniedException("Not authorized to modify this object.");
}
Collection<org.hisp.dhis.message.MessageConversation> messageConversations = messageService.getMessageConversations(user, uids);
if (messageConversations.isEmpty()) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
responseNode.addChild(new SimpleNode("message", "No MessageConversations found for the given UIDs"));
return responseNode;
}
CollectionNode marked = responseNode.addChild(new CollectionNode("unmarkedFollowup"));
marked.setWrapping(false);
for (org.hisp.dhis.message.MessageConversation conversation : messageConversations) {
if (conversation.isFollowUp()) {
conversation.toggleFollowUp(user);
messageService.updateMessageConversation(conversation);
}
marked.addChild(new SimpleNode("uid", conversation.getUid()));
}
response.setStatus(HttpServletResponse.SC_OK);
return responseNode;
}
use of org.hisp.dhis.user.CurrentUser in project dhis2-core by dhis2.
the class OrganisationUnitController method getEntityList.
@Override
@SuppressWarnings("unchecked")
protected List<OrganisationUnit> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
List<OrganisationUnit> objects = Lists.newArrayList();
User currentUser = currentUserService.getCurrentUser();
boolean anySpecialPropertySet = ObjectUtils.anyIsTrue(options.isTrue("userOnly"), options.isTrue("userDataViewOnly"), options.isTrue("userDataViewFallback"), options.isTrue("levelSorted"));
boolean anyQueryPropertySet = ObjectUtils.firstNonNull(options.get("query"), options.getInt("level"), options.getInt("maxLevel")) != null || options.isTrue("withinUserHierarchy") || options.isTrue("withinUserSearchHierarchy");
String memberObject = options.get("memberObject");
String memberCollection = options.get("memberCollection");
if (options.isTrue("userOnly")) {
objects = new ArrayList<>(currentUser.getOrganisationUnits());
} else if (options.isTrue("userDataViewOnly")) {
objects = new ArrayList<>(currentUser.getDataViewOrganisationUnits());
} else if (options.isTrue("userDataViewFallback")) {
if (currentUser.hasDataViewOrganisationUnit()) {
objects = new ArrayList<>(currentUser.getDataViewOrganisationUnits());
} else {
objects = organisationUnitService.getOrganisationUnitsAtLevel(1);
}
} else if (options.isTrue("levelSorted")) {
objects = new ArrayList<>(manager.getAll(getEntityClass()));
objects.sort(OrganisationUnitByLevelComparator.INSTANCE);
} else if (anyQueryPropertySet) {
OrganisationUnitQueryParams params = new OrganisationUnitQueryParams();
params.setQuery(options.get("query"));
params.setLevel(options.getInt("level"));
params.setMaxLevels(options.getInt("maxLevel"));
params.setParents(options.isTrue("withinUserHierarchy") ? currentUser.getOrganisationUnits() : options.isTrue("withinUserSearchHierarchy") ? currentUser.getTeiSearchOrganisationUnitsWithFallback() : Sets.newHashSet());
objects = organisationUnitService.getOrganisationUnitsByQuery(params);
}
// ---------------------------------------------------------------------
// Standard Query handling
// ---------------------------------------------------------------------
Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, getPaginationData(options), options.getRootJunction());
query.setUser(currentUser);
query.setDefaultOrder();
query.setDefaults(Defaults.valueOf(options.get("defaults", DEFAULTS)));
if (anySpecialPropertySet || anyQueryPropertySet) {
query.setObjects(objects);
}
List<OrganisationUnit> list = (List<OrganisationUnit>) queryService.query(query);
// ---------------------------------------------------------------------
// Collection member count in hierarchy handling
// ---------------------------------------------------------------------
IdentifiableObject member;
if (memberObject != null && memberCollection != null && (member = manager.get(memberObject)) != null) {
for (OrganisationUnit unit : list) {
Long count = organisationUnitService.getOrganisationUnitHierarchyMemberCount(unit, member, memberCollection);
unit.setMemberCount((count != null ? count.intValue() : 0));
}
}
return list;
}
use of org.hisp.dhis.user.CurrentUser in project dhis2-core by dhis2.
the class SmsInboundController method getUserByPhoneNumber.
// -------------------------------------------------------------------------
// SUPPORTIVE METHOD
// -------------------------------------------------------------------------
private User getUserByPhoneNumber(String phoneNumber, String text, User currentUser) throws WebMessageException {
SMSCommand unregisteredParser = smsCommandService.getSMSCommand(SmsUtils.getCommandString(text), ParserType.UNREGISTERED_PARSER);
List<User> users = userService.getUsersByPhoneNumber(phoneNumber);
if (SmsUtils.isBase64(text)) {
return handleCompressedCommands(currentUser, phoneNumber);
}
if (users == null || users.isEmpty()) {
if (unregisteredParser != null) {
return null;
}
// No user belong to this phone number
throw new WebMessageException(conflict("User's phone number is not registered in the system"));
}
return users.iterator().next();
}
Aggregations