use of org.hisp.dhis.user.UserGroup in project dhis2-core by dhis2.
the class UnregisteredSMSListener method receive.
@Transactional
@Override
public void receive(IncomingSms sms) {
String message = sms.getText();
SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.UNREGISTERED_PARSER);
UserGroup userGroup = smsCommand.getUserGroup();
String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
if (userGroup != null) {
Collection<User> users = userService.getUsersByPhoneNumber(senderPhoneNumber);
if (users != null && users.size() >= 1) {
String messageError = "This number is already registered for user: ";
for (Iterator<User> iterator = users.iterator(); iterator.hasNext(); ) {
User user = iterator.next();
messageError += user.getName();
if (iterator.hasNext()) {
messageError += ", ";
}
}
throw new SMSParserException(messageError);
} else {
Set<User> receivers = new HashSet<>(userGroup.getMembers());
UserCredentials anonymousUser = userService.getUserCredentialsByUsername("anonymous");
if (anonymousUser == null) {
User user = new User();
UserCredentials usercredential = new UserCredentials();
usercredential.setUsername(USER_NAME);
usercredential.setPassword(USER_NAME);
usercredential.setUserInfo(user);
user.setSurname(USER_NAME);
user.setFirstName(USER_NAME);
user.setUserCredentials(usercredential);
userService.addUserCredentials(usercredential);
userService.addUser(user);
anonymousUser = userService.getUserCredentialsByUsername("anonymous");
}
// forward to user group by SMS, E-mail, DHIS conversation
messageService.sendMessage(smsCommand.getName(), message, null, receivers, anonymousUser.getUserInfo(), MessageType.SYSTEM, false);
// confirm SMS was received and forwarded completely
Set<User> feedbackList = new HashSet<>();
User sender = new User();
sender.setPhoneNumber(senderPhoneNumber);
feedbackList.add(sender);
smsSender.sendMessage(smsCommand.getName(), smsCommand.getReceivedMessage(), null, null, feedbackList, true);
sms.setStatus(SmsMessageStatus.PROCESSED);
sms.setParsed(true);
incomingSmsService.update(sms);
}
}
}
use of org.hisp.dhis.user.UserGroup in project dhis2-core by dhis2.
the class SharingController method setSharing.
@RequestMapping(method = { RequestMethod.POST, RequestMethod.PUT }, consumes = MediaType.APPLICATION_JSON_VALUE)
public void setSharing(@RequestParam String type, @RequestParam String id, HttpServletResponse response, HttpServletRequest request) throws IOException, WebMessageException {
Class<? extends IdentifiableObject> sharingClass = aclService.classForType(type);
if (sharingClass == null || !aclService.isShareable(sharingClass)) {
throw new WebMessageException(WebMessageUtils.conflict("Type " + type + " is not supported."));
}
BaseIdentifiableObject object = (BaseIdentifiableObject) manager.get(sharingClass, id);
if (object == null) {
throw new WebMessageException(WebMessageUtils.notFound("Object of type " + type + " with ID " + id + " was not found."));
}
User user = currentUserService.getCurrentUser();
if (!aclService.canManage(user, object)) {
throw new AccessDeniedException("You do not have manage access to this object.");
}
Sharing sharing = renderService.fromJson(request.getInputStream(), Sharing.class);
if (!AccessStringHelper.isValid(sharing.getObject().getPublicAccess())) {
throw new WebMessageException(WebMessageUtils.conflict("Invalid public access string: " + sharing.getObject().getPublicAccess()));
}
if (aclService.canMakeExternal(user, object.getClass())) {
object.setExternalAccess(sharing.getObject().hasExternalAccess());
}
if (aclService.canMakePublic(user, object.getClass())) {
object.setPublicAccess(sharing.getObject().getPublicAccess());
}
if (object.getUser() == null) {
object.setUser(user);
}
Iterator<UserGroupAccess> userGroupAccessIterator = object.getUserGroupAccesses().iterator();
while (userGroupAccessIterator.hasNext()) {
UserGroupAccess userGroupAccess = userGroupAccessIterator.next();
userGroupAccessIterator.remove();
userGroupAccessService.deleteUserGroupAccess(userGroupAccess);
}
for (SharingUserGroupAccess sharingUserGroupAccess : sharing.getObject().getUserGroupAccesses()) {
UserGroupAccess userGroupAccess = new UserGroupAccess();
if (!AccessStringHelper.isValid(sharingUserGroupAccess.getAccess())) {
throw new WebMessageException(WebMessageUtils.conflict("Invalid user group access string: " + sharingUserGroupAccess.getAccess()));
}
userGroupAccess.setAccess(sharingUserGroupAccess.getAccess());
UserGroup userGroup = manager.get(UserGroup.class, sharingUserGroupAccess.getId());
if (userGroup != null) {
userGroupAccess.setUserGroup(userGroup);
userGroupAccessService.addUserGroupAccess(userGroupAccess);
object.getUserGroupAccesses().add(userGroupAccess);
}
}
Iterator<UserAccess> userAccessIterator = object.getUserAccesses().iterator();
while (userAccessIterator.hasNext()) {
UserAccess userAccess = userAccessIterator.next();
userAccessIterator.remove();
userAccessService.deleteUserAccess(userAccess);
}
for (SharingUserAccess sharingUserAccess : sharing.getObject().getUserAccesses()) {
UserAccess userAccess = new UserAccess();
if (!AccessStringHelper.isValid(sharingUserAccess.getAccess())) {
throw new WebMessageException(WebMessageUtils.conflict("Invalid user access string: " + sharingUserAccess.getAccess()));
}
userAccess.setAccess(sharingUserAccess.getAccess());
User sharingUser = manager.get(User.class, sharingUserAccess.getId());
if (sharingUser != null) {
userAccess.setUser(sharingUser);
userAccessService.addUserAccess(userAccess);
object.getUserAccesses().add(userAccess);
}
}
manager.updateNoAcl(object);
log.info(sharingToString(object));
webMessageService.send(WebMessageUtils.ok("Access control set"), response, request);
}
use of org.hisp.dhis.user.UserGroup in project dhis2-core by dhis2.
the class CreateSMSCommandForm method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
SMSCommand command = new SMSCommand();
command.setName(name);
command.setParserType(parserType);
if (parserType.equals(ParserType.KEY_VALUE_PARSER) || parserType.equals(ParserType.J2ME_PARSER)) {
DataSet dataset = dataSetService.getDataSet(selectedDataSetID);
command.setDataset(dataset);
} else if (parserType.equals(ParserType.ALERT_PARSER) || parserType.equals(ParserType.UNREGISTERED_PARSER)) {
UserGroup userGroup = new UserGroup();
userGroup = userGroupService.getUserGroup(userGroupID);
command.setUserGroup(userGroup);
} else if (parserType.equals(ParserType.TRACKED_ENTITY_REGISTRATION_PARSER)) {
Program program = programService.getProgram(selectedProgramId);
command.setProgram(program);
} else if (parserType.equals(ParserType.EVENT_REGISTRATION_PARSER)) {
Program program = programService.getProgram(selectedProgramIdWithoutRegistration);
command.setProgram(program);
command.setProgramStage(program.getProgramStages().iterator().next());
}
smsCommandService.save(command);
return SUCCESS;
}
use of org.hisp.dhis.user.UserGroup in project dhis2-core by dhis2.
the class ProcessingSendSMSAction method execute.
// -------------------------------------------------------------------------
// Action Implementation
// -------------------------------------------------------------------------
@Override
@SuppressWarnings("unchecked")
public String execute() throws Exception {
SmsGatewayConfig defaultgateway = gatewayAdminService.getDefaultGateway();
if (defaultgateway == null) {
message = i18n.getString("please_select_a_gateway_type_to_send_sms");
return ERROR;
}
if (text == null || text.trim().length() == 0) {
message = i18n.getString("no_message");
return ERROR;
}
User currentUser = currentUserService.getCurrentUser();
List<User> recipientsList = new ArrayList<>();
if ("phone".equals(sendTarget)) {
ObjectMapper mapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
recipients = mapper.readValue(recipients.iterator().next(), Set.class);
for (String each : recipients) {
User user = new User();
user.setPhoneNumber(each);
recipientsList.add(user);
}
// message = messageSender.sendMessage( smsSubject, smsMessage,
// currentUser, true, recipients, gatewayId );
} else if ("userGroup".equals(sendTarget)) {
UserGroup group = userGroupService.getUserGroup(userGroup);
if (group == null) {
message = i18n.getString("selected_user_group_is_unavailable");
return ERROR;
}
if (group.getMembers() == null || group.getMembers().isEmpty()) {
message = i18n.getString("selected_user_group_has_no_member");
return ERROR;
}
recipientsList = new ArrayList<>(group.getMembers());
} else if ("user".equals(sendTarget)) {
Collection<OrganisationUnit> units = selectionTreeManager.getReloadedSelectedOrganisationUnits();
if (units != null && !units.isEmpty()) {
for (OrganisationUnit unit : units) {
recipientsList.addAll(unit.getUsers());
}
if (recipientsList.isEmpty()) {
message = i18n.getString("there_is_no_user_assigned_to_selected_units");
return ERROR;
}
// message = messageSender.sendMessage( smsSubject, smsMessage,
// currentUser, false, users, gatewayId );
}
} else if ("unit".equals(sendTarget)) {
for (OrganisationUnit unit : selectionTreeManager.getSelectedOrganisationUnits()) {
if (unit.getPhoneNumber() != null && !unit.getPhoneNumber().isEmpty()) {
User user = new User();
user.setPhoneNumber(unit.getPhoneNumber());
recipientsList.add(user);
}
}
if (recipientsList.isEmpty()) {
message = i18n.getString("selected_units_have_no_phone_number");
return ERROR;
}
}
TaskId taskId = new TaskId(TaskCategory.SENDING_SMS, currentUser);
notifier.clear(taskId);
sendSmsTask.setTaskId(taskId);
sendSmsTask.setCurrentUser(currentUser);
sendSmsTask.setRecipientsList(recipientsList);
sendSmsTask.setSmsSubject(smsSubject);
sendSmsTask.setText(text);
scheduler.executeTask(sendSmsTask);
if (message != null && !message.equals("success")) {
message = i18n.getString(message);
return ERROR;
}
if (message == null) {
message = "An inter error occurs, please contact your administration";
return ERROR;
}
return SUCCESS;
}
use of org.hisp.dhis.user.UserGroup in project dhis2-core by dhis2.
the class DhisMessageAlertListener method postProcess.
@Override
protected void postProcess(IncomingSms sms, SMSCommand smsCommand, Map<String, String> parsedMessage) {
String message = sms.getText();
UserGroup userGroup = smsCommand.getUserGroup();
if (userGroup != null) {
Collection<User> users = Collections.singleton(sms.getCreatedBy());
if (users != null && users.size() > 1) {
String messageMoreThanOneUser = smsCommand.getMoreThanOneOrgUnitMessage();
if (messageMoreThanOneUser.trim().isEmpty()) {
messageMoreThanOneUser = SMSCommand.MORE_THAN_ONE_ORGUNIT_MESSAGE;
}
for (Iterator<User> i = users.iterator(); i.hasNext(); ) {
User user = i.next();
messageMoreThanOneUser += " " + user.getName();
if (i.hasNext()) {
messageMoreThanOneUser += ",";
}
}
throw new SMSParserException(messageMoreThanOneUser);
} else if (users != null && users.size() == 1) {
User sender = users.iterator().next();
Set<User> receivers = new HashSet<>(userGroup.getMembers());
messageService.sendMessage(new MessageConversationParams.Builder(receivers, sender, smsCommand.getName(), message, MessageType.SYSTEM, null).build());
Set<User> feedbackList = new HashSet<>();
feedbackList.add(sender);
String confirmMessage = smsCommand.getReceivedMessage();
if (confirmMessage == null) {
confirmMessage = SMSCommand.ALERT_FEEDBACK;
}
if (smsSender.isConfigured()) {
smsSender.sendMessage(smsCommand.getName(), confirmMessage, null, null, feedbackList, false);
} else {
log.info("No sms configuration found.");
}
update(sms, SmsMessageStatus.PROCESSED, true);
} else if (users == null || users.size() == 0) {
throw new SMSParserException("No user associated with this phone number. Please contact your supervisor.");
}
}
}
Aggregations