use of org.hisp.dhis.user.User in project dhis2-core by dhis2.
the class MobileClientController method getOrgUnitsForUser2_8.
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public OrgUnits getOrgUnitsForUser2_8(HttpServletRequest request) throws NotAllowedException {
User user = currentUserService.getCurrentUser();
if (user == null) {
throw NotAllowedException.NO_USER;
}
Collection<OrganisationUnit> units = user.getOrganisationUnits();
List<MobileOrgUnitLinks> unitList = new ArrayList<>();
for (OrganisationUnit unit : units) {
unitList.add(getOrgUnit(unit, request));
}
OrgUnits orgUnits = new OrgUnits(unitList);
orgUnits.setClientVersion(DataStreamSerializable.TWO_POINT_EIGHT);
return orgUnits;
}
use of org.hisp.dhis.user.User in project dhis2-core by dhis2.
the class UserRoleController method removeUserFromRole.
@RequestMapping(value = "/{id}/users/{userId}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void removeUserFromRole(@PathVariable(value = "id") String pvId, @PathVariable("userId") String pvUserId, HttpServletResponse response) throws WebMessageException {
UserAuthorityGroup userAuthorityGroup = userService.getUserAuthorityGroup(pvId);
if (userAuthorityGroup == null) {
throw new WebMessageException(WebMessageUtils.notFound("UserRole does not exist: " + pvId));
}
User user = userService.getUser(pvUserId);
if (user == null || user.getUserCredentials() == null) {
throw new WebMessageException(WebMessageUtils.notFound("User does not exist: " + pvId));
}
if (!aclService.canUpdate(currentUserService.getCurrentUser(), userAuthorityGroup)) {
throw new DeleteAccessDeniedException("You don't have the proper permissions to delete this object.");
}
if (user.getUserCredentials().getUserAuthorityGroups().contains(userAuthorityGroup)) {
user.getUserCredentials().getUserAuthorityGroups().remove(userAuthorityGroup);
userService.updateUserCredentials(user.getUserCredentials());
}
}
use of org.hisp.dhis.user.User in project dhis2-core by dhis2.
the class DefaultPushAnalysisService method runPushAnalysis.
@Override
public void runPushAnalysis(int id, TaskId taskId) {
//----------------------------------------------------------------------
// Set up
//----------------------------------------------------------------------
PushAnalysis pushAnalysis = pushAnalysisStore.get(id);
Set<User> receivingUsers = new HashSet<>();
notifier.clear(taskId);
//----------------------------------------------------------------------
// Pre-check
//----------------------------------------------------------------------
log(taskId, NotificationLevel.INFO, "Starting pre-check on PushAnalysis", false, null);
if (!setPushAnalysisIsRunningFlag(pushAnalysis, true)) {
log(taskId, NotificationLevel.ERROR, "PushAnalysis with id '" + id + "' is already running. Terminating new PushAnalysis job.", true, null);
return;
}
if (pushAnalysis == null) {
log(taskId, NotificationLevel.ERROR, "PushAnalysis with id '" + id + "' was not found. Terminating PushAnalysis", true, null);
return;
}
if (pushAnalysis.getRecipientUserGroups().size() == 0) {
log(taskId, NotificationLevel.ERROR, "PushAnalysis with id '" + id + "' has no userGroups assigned. Terminating PushAnalysis.", true, null);
return;
}
if (pushAnalysis.getDashboard() == null) {
log(taskId, NotificationLevel.ERROR, "PushAnalysis with id '" + id + "' has no dashboard assigned. Terminating PushAnalysis.", true, null);
return;
}
if (systemSettingManager.getInstanceBaseUrl() == null) {
log(taskId, NotificationLevel.ERROR, "Missing system setting '" + SettingKey.INSTANCE_BASE_URL.getName() + "'. Terminating PushAnalysis.", true, null);
return;
}
log(taskId, NotificationLevel.INFO, "pre-check completed successfully", false, null);
//----------------------------------------------------------------------
// Compose list of users that can receive PushAnalysis
//----------------------------------------------------------------------
log(taskId, NotificationLevel.INFO, "Composing list of receiving users", false, null);
for (UserGroup userGroup : pushAnalysis.getRecipientUserGroups()) {
for (User user : userGroup.getMembers()) {
if (!user.hasEmail()) {
log(taskId, NotificationLevel.WARN, "Skipping user: User '" + user.getUsername() + "' is missing a valid email.", false, null);
continue;
}
receivingUsers.add(user);
}
}
log(taskId, NotificationLevel.INFO, "List composed. " + receivingUsers.size() + " eligible users found.", false, null);
//----------------------------------------------------------------------
// Generating reports
//----------------------------------------------------------------------
log(taskId, NotificationLevel.INFO, "Generating and sending reports", false, null);
for (User user : receivingUsers) {
try {
String title = pushAnalysis.getTitle();
String html = generateHtmlReport(pushAnalysis, user, taskId);
// TODO: Better handling of messageStatus; Might require refactoring of EmailMessageSender
@SuppressWarnings("unused") OutboundMessageResponse status = messageSender.sendMessage(title, html, "", null, Sets.newHashSet(user), true);
} catch (Exception e) {
log(taskId, NotificationLevel.ERROR, "Could not create or send report for PushAnalysis '" + pushAnalysis.getName() + "' and User '" + user.getUsername() + "': " + e.getMessage(), false, e);
}
}
// Update lastRun date
pushAnalysis.setLastRun(new Date());
pushAnalysisStore.update(pushAnalysis);
setPushAnalysisIsRunningFlag(pushAnalysis, false);
}
use of org.hisp.dhis.user.User in project dhis2-core by dhis2.
the class PreheatServiceTest method testPreheatAllMetadataUID.
@Test
public void testPreheatAllMetadataUID() {
DataElementGroup dataElementGroup = new DataElementGroup("DataElementGroupA");
dataElementGroup.setAutoFields();
DataElement de1 = createDataElement('A');
DataElement de2 = createDataElement('B');
DataElement de3 = createDataElement('C');
manager.save(de1);
manager.save(de2);
manager.save(de3);
User user = createUser('A');
manager.save(user);
dataElementGroup.addDataElement(de1);
dataElementGroup.addDataElement(de2);
dataElementGroup.addDataElement(de3);
dataElementGroup.setUser(user);
manager.save(dataElementGroup);
PreheatParams params = new PreheatParams();
params.setPreheatMode(PreheatMode.ALL);
preheatService.validate(params);
Preheat preheat = preheatService.preheat(params);
assertFalse(preheat.isEmpty());
assertFalse(preheat.isEmpty(PreheatIdentifier.UID));
assertFalse(preheat.isEmpty(PreheatIdentifier.UID, DataElement.class));
assertFalse(preheat.isEmpty(PreheatIdentifier.UID, DataElementGroup.class));
assertFalse(preheat.isEmpty(PreheatIdentifier.UID, User.class));
assertTrue(preheat.containsKey(PreheatIdentifier.UID, DataElement.class, de1.getUid()));
assertTrue(preheat.containsKey(PreheatIdentifier.UID, DataElement.class, de2.getUid()));
assertTrue(preheat.containsKey(PreheatIdentifier.UID, DataElement.class, de3.getUid()));
assertTrue(preheat.containsKey(PreheatIdentifier.UID, DataElementGroup.class, dataElementGroup.getUid()));
assertTrue(preheat.containsKey(PreheatIdentifier.UID, User.class, user.getUid()));
}
use of org.hisp.dhis.user.User in project dhis2-core by dhis2.
the class DhisConvenienceTest method createUserAndInjectSecurityContext.
/**
* Creates a user and injects into the security context with username
* "username". Requires <code>identifiableObjectManager</code> and
* <code>userService</code> to be injected into the test.
*
* @param organisationUnits the organisation units of the user.
* @param dataViewOrganisationUnits user's data view organisation units.
* @param allAuth whether to grant the ALL authority.
* @param auths authorities to grant to user.
* @return the user.
*/
protected User createUserAndInjectSecurityContext(Set<OrganisationUnit> organisationUnits, Set<OrganisationUnit> dataViewOrganisationUnits, boolean allAuth, String... auths) {
Assert.notNull(userService, "UserService must be injected in test");
Set<String> authorities = new HashSet<>();
if (allAuth) {
authorities.add(UserAuthorityGroup.AUTHORITY_ALL);
}
if (auths != null) {
authorities.addAll(Lists.newArrayList(auths));
}
UserAuthorityGroup userAuthorityGroup = new UserAuthorityGroup();
userAuthorityGroup.setName("Superuser");
userAuthorityGroup.getAuthorities().addAll(authorities);
userService.addUserAuthorityGroup(userAuthorityGroup);
User user = createUser('A');
if (organisationUnits != null) {
user.setOrganisationUnits(organisationUnits);
}
if (dataViewOrganisationUnits != null) {
user.setDataViewOrganisationUnits(dataViewOrganisationUnits);
}
user.getUserCredentials().getUserAuthorityGroups().add(userAuthorityGroup);
userService.addUser(user);
user.getUserCredentials().setUserInfo(user);
userService.addUserCredentials(user.getUserCredentials());
Set<GrantedAuthority> grantedAuths = authorities.stream().map(a -> new SimpleGrantedAuthority(a)).collect(Collectors.toSet());
UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.getUserCredentials().getUsername(), user.getUserCredentials().getPassword(), grantedAuths);
Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, "", grantedAuths);
SecurityContextHolder.getContext().setAuthentication(authentication);
return user;
}
Aggregations