Skip to content

Instantly share code, notes, and snippets.

@slemarchand
Last active December 17, 2015 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slemarchand/5565262 to your computer and use it in GitHub Desktop.
Save slemarchand/5565262 to your computer and use it in GitHub Desktop.
import java.util.Calendar
import com.liferay.portal.service.*
import com.liferay.portal.model.*
import com.liferay.portal.kernel.dao.orm.*
import static com.liferay.portal.kernel.workflow.WorkflowConstants.*
//
// Deactivate users never logged and created since more than 2 years
//
previewMode = true // Update this flag to false to really make changes
Calendar twoYearsAgo = Calendar.getInstance()
twoYearsAgo.setTime(new Date())
twoYearsAgo.add(Calendar.YEAR, -2)
DynamicQuery query = DynamicQueryFactoryUtil.forClass(User.class)
.add(PropertyFactoryUtil.forName("lastLoginDate").isNull())
.add(PropertyFactoryUtil.forName("createDate").lt(twoYearsAgo.getTime()))
users = UserLocalServiceUtil.dynamicQuery(query)
users.each { u ->
if(!u.isDefaultUser() && u.getStatus() != STATUS_INACTIVE) {
out.println(u.getEmailAddress())
if(!previewMode) {
UserLocalServiceUtil.updateStatus(u.getUserId(), STATUS_INACTIVE)
}
}
}
if(previewMode) {
out.println('Preview mode is on: switch off the flag and execute '
+ 'again this script to make changes to the database')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment