forked from openmrs/openmrs-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TRUNK-6235: auto deactivate users after XX days of inactivity (openmr…
…s#4650) * TRUNK-6235 - auto deactivate users * --- (openmrs#4643) updated-dependencies: - dependency-name: org.codehaus.mojo:build-helper-maven-plugin dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Update to Tomcat 9 * --- (openmrs#4647) updated-dependencies: - dependency-name: net.bytebuddy:byte-buddy dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * --- (openmrs#4648) updated-dependencies: - dependency-name: net.bytebuddy:byte-buddy-agent dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Updating tests * Changes from Pull request * Reverting refactor * Adding property in core global property * Using matchers in place of empty string * Adding licence * Adding tests for auto-retire * Changing an access modifier * Improving documentation * Updating tests * escaping superusers while auto retiring users * Code refactor * Improving tests * Improving tests * Code refactor * Using TimeUnit for time conversion * Changing method modifiers to private * Fixing a failing build * making a static variable private * Using exact retire reason in tests * Fixing tests * correcting the expected values * Removing import of a constant --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Isaiah <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Ian <[email protected]> Co-authored-by: Isaiah Muli <[email protected]>
- Loading branch information
1 parent
cca287a
commit 9241104
Showing
9 changed files
with
364 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
api/src/main/java/org/openmrs/scheduler/tasks/AutoRetireUsersTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.scheduler.tasks; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.openmrs.User; | ||
import org.openmrs.api.UserService; | ||
import org.openmrs.api.context.Context; | ||
import org.openmrs.util.OpenmrsConstants; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collections; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* A scheduled task that automatically retires users after the set number of days of inactivity. | ||
* The inactivity duration is set as a global property. | ||
* <a href="https://openmrs.atlassian.net/wiki/spaces/docs/pages/101318663/Creating+Auto-Deactivating+User+Task">Documentation</a> | ||
* {@link OpenmrsConstants#GP_NUMBER_OF_DAYS_TO_AUTO_RETIRE_USERS} | ||
* | ||
* @since 2.7.0 | ||
*/ | ||
public class AutoRetireUsersTask extends AbstractTask { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AutoRetireUsersTask.class); | ||
private static final String AUTO_RETIRE_REASON = "User retired due to inactivity"; | ||
|
||
/** | ||
* @see org.openmrs.scheduler.tasks.AbstractTask#execute() | ||
*/ | ||
@Override | ||
public void execute() { | ||
if (!isExecuting) { | ||
log.debug("Auto-retiring users task Started"); | ||
|
||
startExecuting(); | ||
|
||
try { | ||
UserService userService = Context.getUserService(); | ||
Set<User> usersToRetire = getUsersToRetire(userService); | ||
|
||
usersToRetire.forEach(user -> userService.retireUser(user, AUTO_RETIRE_REASON)); | ||
} catch (Exception e) { | ||
log.error("Error occurred while auto-retiring users: ", e); | ||
} finally { | ||
log.debug("Auto-retiring users task ended"); | ||
stopExecuting(); | ||
} | ||
} | ||
} | ||
|
||
private Set<User> getUsersToRetire(UserService userService) { | ||
final List<User> allUsers = userService.getAllUsers(); | ||
String numberOfDaysToRetire = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_NUMBER_OF_DAYS_TO_AUTO_RETIRE_USERS); | ||
|
||
if (StringUtils.isBlank(numberOfDaysToRetire)) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
long numberOfMillisecondsToRetire = TimeUnit.DAYS.toMillis(Long.parseLong(numberOfDaysToRetire)); | ||
|
||
return allUsers.stream() | ||
.filter(user -> !user.isSuperUser() | ||
&& !user.isRetired() | ||
&& userInactivityExceedsDaysToRetire(user, numberOfMillisecondsToRetire) | ||
) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private boolean userInactivityExceedsDaysToRetire(User user, long numberOfMillisecondsToRetire) { | ||
String lastLoginTimeString = Context.getUserService().getLastLoginTime(user); | ||
|
||
if (StringUtils.isNotBlank(lastLoginTimeString)) { | ||
long lastLoginTime = Long.parseLong(lastLoginTimeString); | ||
|
||
return System.currentTimeMillis() - lastLoginTime >= numberOfMillisecondsToRetire; | ||
} else { | ||
Date dateCreated = user.getDateCreated(); | ||
|
||
if (dateCreated != null) { | ||
return System.currentTimeMillis() - dateCreated.getTime() >= numberOfMillisecondsToRetire; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.