-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/user-inactivity'
- Loading branch information
Showing
18 changed files
with
395 additions
and
11 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
94 changes: 91 additions & 3 deletions
94
myconext-server/src/main/java/myconext/cron/InactivityMail.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 |
---|---|---|
@@ -1,8 +1,96 @@ | ||
package myconext.cron; | ||
|
||
import myconext.mail.MailBox; | ||
import myconext.model.User; | ||
import myconext.model.UserInactivity; | ||
import myconext.repository.UserRepository; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.text.DateFormat; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
@Component | ||
public class InactivityMail { | ||
|
||
//TODO, keep track of the mails send bu using an enum indicating the level of inactivity | ||
//In the variables for the mail separate the NL variable and EN variable and use | ||
//different variables in the mail templates | ||
private static final Log LOG = LogFactory.getLog(InactivityMail.class); | ||
|
||
private final MailBox mailBox; | ||
private final UserRepository userRepository; | ||
private final boolean mailInactivityMails; | ||
private final boolean cronJobResponsible; | ||
private final DateFormat dateFormatUS; | ||
private final DateFormat dateFormatNL; | ||
|
||
@Autowired | ||
public InactivityMail(MailBox mailBox, | ||
UserRepository userRepository, | ||
@Value("${cron.node-cron-job-responsible}") boolean cronJobResponsible, | ||
@Value("${feature.mail_inactivity_mails}") boolean mailInactivityMails) { | ||
this.mailBox = mailBox; | ||
this.userRepository = userRepository; | ||
this.cronJobResponsible = cronJobResponsible; | ||
this.mailInactivityMails = mailInactivityMails; | ||
this.dateFormatUS = DateFormat.getDateInstance(DateFormat.LONG, Locale.of("us")); | ||
this.dateFormatNL = DateFormat.getDateInstance(DateFormat.LONG, Locale.of("nl")); | ||
} | ||
|
||
@Scheduled(cron = "${cron.inactivity-users-expression}") | ||
@SuppressWarnings("unchecked") | ||
public void mailInactiveUsers() { | ||
if (!mailInactivityMails || !cronJobResponsible) { | ||
return; | ||
} | ||
try { | ||
Stream.of(UserInactivity.values()).forEach(this::doMailInactiveUsers); | ||
this.doDeleteInactiveUsers(); | ||
} catch (Exception e) { | ||
//swallow exception as the scheduling stops then | ||
LOG.error("Error in mailInactiveUsers", e); | ||
} | ||
} | ||
|
||
private void doMailInactiveUsers(UserInactivity userInactivity) { | ||
long nowInMillis = System.currentTimeMillis(); | ||
long oneDayInMillis = 24 * 60 * 60 * 1000L; | ||
long fiveYearsInMillis = 5 * 365 * oneDayInMillis; | ||
|
||
long lastLoginBefore = nowInMillis - (oneDayInMillis * userInactivity.getInactivityDays()); | ||
List<User> users = userRepository.findByLastLoginBeforeAndUserInactivity(lastLoginBefore, userInactivity.getPreviousUserInactivity()); | ||
|
||
Map<String, String> localeVariables = new HashMap<>(); | ||
//This is the future date when the user will be deleted based on the inactivityDays of the userInactivity | ||
Date date = new Date(nowInMillis + (fiveYearsInMillis - (oneDayInMillis * userInactivity.getInactivityDays()))); | ||
localeVariables.put("inactivity_period_en", userInactivity.getInactivityPeriodEn()); | ||
localeVariables.put("inactivity_period_nl", userInactivity.getInactivityPeriodNl()); | ||
localeVariables.put("deletion_period_en", userInactivity.getDeletionPeriodEn()); | ||
localeVariables.put("deletion_period_nl", userInactivity.getDeletionPeriodNl()); | ||
localeVariables.put("account_delete_date_en", dateFormatUS.format(date)); | ||
localeVariables.put("account_delete_date_nl", dateFormatNL.format(date)); | ||
users.forEach(user -> { | ||
mailBox.sendUserInactivityMail(user, localeVariables, true); | ||
user.setUserInactivity(userInactivity); | ||
userRepository.save(user); | ||
}); | ||
LOG.info(String.format("Mailed %s users who has been inactive for %s period in for %s ms", | ||
users.size(), userInactivity, System.currentTimeMillis() - nowInMillis)); | ||
} | ||
|
||
private void doDeleteInactiveUsers() { | ||
long nowInMillis = System.currentTimeMillis(); | ||
long oneDayInMillis = 24 * 60 * 60 * 1000L; | ||
|
||
long lastLoginBefore = nowInMillis - (oneDayInMillis * 5L * 365); | ||
List<User> users = userRepository.findByLastLoginBeforeAndUserInactivity(lastLoginBefore, UserInactivity.WEEK_1_BEFORE_5_YEARS); | ||
userRepository.deleteAll(users); | ||
LOG.info(String.format("Deleted %s users (%s) who has been inactive for 5 years in for %s ms", | ||
users.size(), users.stream().map(User::getEmail).collect(Collectors.joining(", ")), | ||
System.currentTimeMillis() - nowInMillis)); | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
myconext-server/src/main/java/myconext/model/UserInactivity.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,58 @@ | ||
package myconext.model; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum UserInactivity { | ||
|
||
// 1 year (= 4 year before the 5-year mark) | ||
YEAR_1_INTERVAL(365L, | ||
null, | ||
"1 year", | ||
"1 jaar", | ||
"4 years", | ||
"4 jaren"), | ||
// 3 year (= 3 year before the 5-year mark) | ||
YEAR_3_INTERVAL(2L * 365, | ||
YEAR_1_INTERVAL, | ||
"2 years", | ||
"2 jaren", | ||
"3 years", | ||
"3 jaren"), | ||
// 1 month before the 5-year mark | ||
MONTH_1_BEFORE_5_YEARS((4L * 365) + (11L * 30), | ||
YEAR_3_INTERVAL, | ||
"almost 5 years", | ||
"bijna 5 jaar", | ||
"1 month", | ||
"1 maand"), | ||
// 1 week before the-5 year mark | ||
WEEK_1_BEFORE_5_YEARS((4L * 365) + (51L * 7), | ||
MONTH_1_BEFORE_5_YEARS, | ||
"almost 5 years", | ||
"bijna 5 jaar", | ||
"1 week", | ||
"1 week"); | ||
|
||
private final long inactivityDays; | ||
private final UserInactivity previousUserInactivity; | ||
private final String inactivityPeriodEn; | ||
private final String inactivityPeriodNl; | ||
private final String deletionPeriodEn; | ||
private final String deletionPeriodNl; | ||
|
||
UserInactivity(long inactivityDays, | ||
UserInactivity previousUserInactivity, | ||
String inactivityPeriodEn, | ||
String inactivityPeriodNl, | ||
String deletionPeriodEn, | ||
String deletionPeriodNl) { | ||
this.inactivityDays = inactivityDays; | ||
this.previousUserInactivity = previousUserInactivity; | ||
this.inactivityPeriodEn = inactivityPeriodEn; | ||
this.inactivityPeriodNl = inactivityPeriodNl; | ||
this.deletionPeriodEn = deletionPeriodEn; | ||
this.deletionPeriodNl = deletionPeriodNl; | ||
} | ||
|
||
} |
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
26 changes: 26 additions & 0 deletions
26
myconext-server/src/main/resources/mail_templates/inactivity_warning_short_term_en.html
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,26 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>{{title}}</title> | ||
</head> | ||
<body> | ||
<div class="main" style="font-family: Helvetica, Arial, sans-serif;line-height: 18px;font-size: 14px;color: #353535;"> | ||
<div class="head" style="background-color: #f5f5f5;padding: 30px 0 20px 60px;"> | ||
<p class="title" style="font-weight: 600;font-size: 38px;line-height: 38px; margin: 0;margin-bottom: 10px">You | ||
have not used your eidUD account for {{inactivity_period_en}}</p> | ||
</div> | ||
<div class="middle" style="padding: 20px 0 40px 60px;max-width: 600px;"> | ||
<p>Hi <strong>{{name}}</strong>,</p> | ||
<p>You have not used your eduID account for {{inactivity_period_en}}. If you remain inactive than your eduID | ||
account will be deleted within {{deletion_period_en}}, on {{account_delete_date_en}}. | ||
Go to your <a href="{{mySurfConextURL}}">eduID account</a> and login to keep your account active.</p> | ||
<br/> | ||
<p> | ||
Kind regards, | ||
eduID. | ||
</p> | ||
</div> | ||
{{> footer_nl.html}} | ||
</div> | ||
</body> | ||
</html> |
7 changes: 7 additions & 0 deletions
7
myconext-server/src/main/resources/mail_templates/inactivity_warning_short_term_en.txt
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,7 @@ | ||
Hi {{name}}, | ||
|
||
You have not used your eduID account for {{inactivity_period_en}}. If you remain inactive than your eduID | ||
account will be deleted within {{deletion_period_en}}, on {{account_delete_date_en}}. | ||
Go to your {{mySurfConextURL}} and login to keep your account active. | ||
|
||
{{> footer_en.txt}} |
27 changes: 27 additions & 0 deletions
27
myconext-server/src/main/resources/mail_templates/inactivity_warning_short_term_nl.html
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,27 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>{{title}}</title> | ||
</head> | ||
<body> | ||
<div class="main" style="font-family: Helvetica, Arial, sans-serif;line-height: 18px;font-size: 14px;color: #353535;"> | ||
<div class="head" style="background-color: #f5f5f5;padding: 30px 0 20px 60px;"> | ||
<p class="title" style="font-weight: 600;font-size: 38px;line-height: 38px; margin: 0;margin-bottom: 10px">You | ||
have created an eduID with your institutional account</p> | ||
</div> | ||
<div class="middle" style="padding: 20px 0 40px 60px;max-width: 600px;"> | ||
<p>Hi <strong>{{name}}</strong>,</p> | ||
|
||
<p>Je hebt je eduID account niet gebruikt in {{inactivity_period_nl}}. Als je je eduID account niet gebruikt dan | ||
zullen we deze verwijderen in {{deletion_period_nl}}, op {{account_delete_date_nl}}. | ||
Ga naar <a href="{{mySurfConextURL}}">je eduID account </a> en login om je account actief te houden.</p> | ||
<br/> | ||
<p> | ||
Vriendelijke groeten, | ||
eduID. | ||
</p> | ||
</div> | ||
{{> footer_nl.html}} | ||
</div> | ||
</body> | ||
</html> |
7 changes: 7 additions & 0 deletions
7
myconext-server/src/main/resources/mail_templates/inactivity_warning_short_term_nl.txt
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,7 @@ | ||
Hi {{name}}, | ||
|
||
Je hebt je eduID account niet gebruikt in {{inactivity_period_nl}}. Als je je eduID account niet gebruikt dan zullen we | ||
deze verwijderen in {{deletion_period_nl}}, op {{account_delete_date_nl}}. | ||
Ga naar {{mySurfConextURL}} en login om je account actief te houden. | ||
|
||
{{> footer_en.txt}} |
26 changes: 26 additions & 0 deletions
26
myconext-server/src/main/resources/mail_templates/inactivity_warning_years_ahead_en.html
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,26 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>{{title}}</title> | ||
</head> | ||
<body> | ||
<div class="main" style="font-family: Helvetica, Arial, sans-serif;line-height: 18px;font-size: 14px;color: #353535;"> | ||
<div class="head" style="background-color: #f5f5f5;padding: 30px 0 20px 60px;"> | ||
<p class="title" style="font-weight: 600;font-size: 38px;line-height: 38px; margin: 0;margin-bottom: 10px">You | ||
have not used your eidUD account for {{inactivity_period_en}}</p> | ||
</div> | ||
<div class="middle" style="padding: 20px 0 40px 60px;max-width: 600px;"> | ||
<p>Hi <strong>{{name}}</strong>,</p> | ||
<p>You have not used your eduID account for {{inactivity_period_en}}. If you remain inactive than your eduID | ||
account will be deleted within {{deletion_period_en}}, on {{account_delete_date_en}}. | ||
Go to your <a href="{{mySurfConextURL}}">eduID account</a> and login to keep your account active.</p> | ||
<br/> | ||
<p> | ||
Kind regards, | ||
eduID. | ||
</p> | ||
</div> | ||
{{> footer_nl.html}} | ||
</div> | ||
</body> | ||
</html> |
7 changes: 7 additions & 0 deletions
7
myconext-server/src/main/resources/mail_templates/inactivity_warning_years_ahead_en.txt
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,7 @@ | ||
Hi {{name}}, | ||
|
||
You have not used your eduID account for {{inactivity_period_en}}. If you remain inactive than your eduID | ||
account will be deleted within {{deletion_period_en}}, on {{account_delete_date_en}}. | ||
Go to your {{mySurfConextURL}} and login to keep your account active. | ||
|
||
{{> footer_en.txt}} |
27 changes: 27 additions & 0 deletions
27
myconext-server/src/main/resources/mail_templates/inactivity_warning_years_ahead_nl.html
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,27 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>{{title}}</title> | ||
</head> | ||
<body> | ||
<div class="main" style="font-family: Helvetica, Arial, sans-serif;line-height: 18px;font-size: 14px;color: #353535;"> | ||
<div class="head" style="background-color: #f5f5f5;padding: 30px 0 20px 60px;"> | ||
<p class="title" style="font-weight: 600;font-size: 38px;line-height: 38px; margin: 0;margin-bottom: 10px">You | ||
have created an eduID with your institutional account</p> | ||
</div> | ||
<div class="middle" style="padding: 20px 0 40px 60px;max-width: 600px;"> | ||
<p>Hi <strong>{{name}}</strong>,</p> | ||
|
||
<p>Je hebt je eduID account niet gebruikt in {{inactivity_period_nl}}. Als je je eduID account niet gebruikt dan | ||
zullen we deze verwijderen in {{deletion_period_nl}}, op {{account_delete_date_nl}}. | ||
Ga naar <a href="{{mySurfConextURL}}">je eduID account </a> en login om je account actief te houden.</p> | ||
<br/> | ||
<p> | ||
Vriendelijke groeten, | ||
eduID. | ||
</p> | ||
</div> | ||
{{> footer_nl.html}} | ||
</div> | ||
</body> | ||
</html> |
7 changes: 7 additions & 0 deletions
7
myconext-server/src/main/resources/mail_templates/inactivity_warning_years_ahead_nl.txt
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,7 @@ | ||
Hi {{name}}, | ||
|
||
Je hebt je eduID account niet gebruikt in {{inactivity_period_nl}}. Als je je eduID account niet gebruikt dan zullen we | ||
deze verwijderen in {{deletion_period_nl}}, op {{account_delete_date_nl}}. | ||
Ga naar {{mySurfConextURL}} en login om je account actief te houden. | ||
|
||
{{> footer_en.txt}} |
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.