/src/test/resources/examples/SetUserTimeZone.groovy
Groovy | 94 lines | 78 code | 14 blank | 2 comment | 10 complexity | 550c2788d00027b34cf6b03027e83873 MD5 | raw file
- package examples
-
- import com.atlassian.jira.ComponentManager
- import com.atlassian.jira.bc.JiraServiceContextImpl
- import com.atlassian.jira.security.JiraAuthenticationContext
- import com.atlassian.jira.timezone.TimeZoneService
- import com.opensymphony.user.User
- import com.opensymphony.user.UserManager
- import org.apache.directory.groovyldap.LDAP
- import org.apache.log4j.Category
- import org.apache.log4j.Level
-
- ComponentManager componentManager = ComponentManager.getInstance()
- Category log = log
-
- log.setLevel(Level.DEBUG)
-
- List<String> availableTzIds = TimeZone.getAvailableIDs().toList()
-
- log.debug availableTzIds
-
- TimeZoneService tzService = ComponentManager.getComponentInstanceOfType(TimeZoneService.class)
-
- JiraAuthenticationContext authenticationContext = componentManager.getJiraAuthenticationContext()
-
- LDAP ldap = LDAP.newInstance(
- "ldap://gldap.csintra.net:40389/ou=people,dc=intranet,dc=net",
- "uid=UNITY,ou=users,ou=access,dc=intranet,dc=net",
- "coRDk5dBmVv86dw"
- ) as LDAP
-
- log.debug "ldap ${ldap.url}"
-
- // each user - get timezone from gldap - verify it exists, then set it.
- // List users = UserManager.getInstance().getUser("admin") as List
- List users = UserManager.getInstance().getUsers() as List
- Integer kount = 0
- int maxUsers = 100000
- int minUsers = 0
-
- int missingUsers = 0
- int updatedUsers = 0
- int badUsernameUsers = 0
-
- users.each {User user ->
- kount++
-
- if (kount < maxUsers && kount >= minUsers) {
- if (kount % 100 == 0) {
- log.warn("$kount users processed")
- }
-
- if (user.name.contains("\\")) {
- log.error ("Weird username: ${user.name}")
- badUsernameUsers++
- return
- }
-
- List entries = ldap.search(filter: "(csgUPNs=${user.name}@*)")
- if (entries.size() == 0) {
- log.debug ("No LDAP entry for ${user.name}")
- missingUsers++
-
- }
- else if (entries.size() == 1) {
- Map entry = entries.first() as Map
- String csgTimeZone = entry["csgtimezone"]
- if (csgTimeZone == "asia/kolkata") {
- csgTimeZone = "Asia/Kolkata" // local fix for broken data in GLDAP
- }
-
- JiraServiceContextImpl jiraServiceContext = new JiraServiceContextImpl(user)
- if (availableTzIds*.toLowerCase().contains(csgTimeZone.toLowerCase())) {
- log.debug ("Set timezone to $csgTimeZone for ${user.name}")
- try {
- tzService.setUserDefaultTimeZone(csgTimeZone, jiraServiceContext)
- updatedUsers++
- }
- catch (IllegalArgumentException e) {
- log.warn ("Exception: Invalid timezone: $csgTimeZone for $user")
- }
- }
- else {
- log.warn ("Invalid timezone: $csgTimeZone for $user")
- }
- }
- else if (entries.size() > 1) {
- log.error ("More than one entry for ${user.name}")
- }
- }
- }
-
- log.debug "Totals: updatedUsers: $updatedUsers, missingUsers: $missingUsers, badUsernameUsers: $badUsernameUsers"
- ""