(AHPSCRIPTS-2 Added)
|
(AHPSCRIPTS-3)
|
Line 111: | |||
new PropertyBasedAgentVarEqualsCriteria("TechType", "TechType") | new PropertyBasedAgentVarEqualsCriteria("TechType", "TechType") | ||
);</pre> | );</pre> | ||
+ | = Add unknown SCM users as AHP users = | ||
+ | '''AHPSCRIPTS-3''' | ||
+ | Script Notes: | ||
+ | * Automatically add users that we see in change logs to Anthill so we can send notification to them. | ||
+ | * Needs to have the logging reworked. | ||
+ | <pre>import com.urbancode.anthill3.runtime.scripting.properties.*; | ||
+ | import com.urbancode.anthill3.runtime.scripting.session.*; | ||
+ | import java.util.*; | ||
+ | import java.io.*; | ||
+ | import com.urbancode.anthill3.domain.authentication.*; | ||
+ | import com.urbancode.anthill3.domain.project.*; | ||
+ | import com.urbancode.anthill3.domain.persistent.PersistenceException; | ||
+ | import com.urbancode.anthill3.domain.security.*; | ||
+ | import com.urbancode.anthill3.domain.singleton.security.*; | ||
+ | import com.urbancode.anthill3.domain.userprofile.UserProfile; | ||
+ | import com.urbancode.commons.util.CollectionUtil; | ||
+ | import com.urbancode.vcsdriver3.*; | ||
+ | HashSet userSet = new HashSet(); | ||
+ | ChangeLog[] changelogs = ChangeLogHelper.getChangeLogArray(workflow); | ||
+ | Long repoId = workflow.getBuildLife().getProfile().getSourceConfig().getId(); | ||
+ | userFactory = UserFactory.getInstance(); | ||
+ | developerRole = RoleFactory.getInstance().restoreForName("Sonata Developers"); | ||
+ | userRole = RoleFactory.getInstance().restoreForName("User"); | ||
+ | // Look-up the LDAP realm here | ||
+ | AuthenticationRealm ldapRealm = AuthenticationRealmFactory.getInstance().restore("LDAP"); | ||
+ | File f = new File("/tmp/script.log"); | ||
+ | StringBuffer sb = new StringBuffer(); | ||
+ | for (int i = 0; i < changelogs.length; i++){ | ||
+ | ChangeSet[] changeset = changelogs[i].getChangeSetArray(); | ||
+ | for (int j = 0; j < changeset.length; j++){ | ||
+ | try { | ||
+ | String username = changeset[j].getUser(); | ||
+ | String domain = null; | ||
+ | String[] splitValue = username.split("\\\\"); | ||
+ | if(splitValue.length == 2) { | ||
+ | domain = splitValue[0]; | ||
+ | username = splitValue[1].trim().toLowerCase(); | ||
+ | } | ||
+ | sb.append("Changelog username : "+username+"\n"); | ||
+ | User[] tempUsers = userFactory.restoreAllForChangelogNameAndRepository(username, repoId); | ||
+ | if (tempUsers.length > 0) { | ||
+ | CollectionUtil.addAll(userSet, tempUsers); | ||
+ | } else { | ||
+ | sb.append(username+" User does not exist. Adding User\n"); | ||
+ | User user1 = new User(true, ldapRealm); | ||
+ | user1.setName(username); | ||
+ | user1.setPassword("Bogus"); | ||
+ | Long[] roleArray = new Long[] {userRole.getId(), developerRole.getId()}; | ||
+ | user1.addRole(developerRole); | ||
+ | user1.addRole(userRole); | ||
+ | user1.store(); | ||
+ | UserProfile p = new UserProfile(); | ||
+ | p.setUser(user1); | ||
+ | p.setEmailAddress(username+"@mycompany.com"); | ||
+ | p.setFirstName("New"); | ||
+ | p.setLastName("User"); | ||
+ | p.setNew(); | ||
+ | p.store(); | ||
+ | // Adding the user into the notification userset again | ||
+ | User[] tempUsers = userFactory.restoreAllForChangelogNameAndRepository(username, repoId); | ||
+ | CollectionUtil.addAll(userSet, tempUsers); | ||
+ | } | ||
+ | } | ||
+ | catch (PersistenceException e) { | ||
+ | throw new PersistenceRuntimeException(e); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | User[] result = new User[userSet.size()]; | ||
+ | userSet.toArray(result); | ||
+ | for(int i=0; i<result.length;i++) { | ||
+ | sb.append("user name "+result[i].toString()); | ||
+ | sb.append("realm "+result[i].getAuthenticationRealm().getName()+"\n"); | ||
+ | } | ||
+ | try { | ||
+ | BufferedWriter out = new BufferedWriter(new FileWriter("/tmp/script.log")); | ||
+ | out.write(sb.toString()); | ||
+ | out.close(); | ||
+ | } catch (IOException e) { | ||
+ | }</pre> |