001: package com.technoetic.xplanner.security;
002:
003: import com.technoetic.xplanner.db.hibernate.GlobalSessionFactory;
004: import com.technoetic.xplanner.db.hibernate.HibernateHelper;
005: import com.technoetic.xplanner.domain.Person;
006: import com.technoetic.xplanner.domain.Project;
007: import com.technoetic.xplanner.domain.Role;
008: import com.technoetic.xplanner.security.module.XPlannerLoginModule;
009: import com.technoetic.xplanner.security.module.LoginSupportImpl;
010:
011: import net.sf.hibernate.Hibernate;
012: import net.sf.hibernate.Session;
013: import org.apache.commons.digester.Digester;
014: import org.apache.log4j.Logger;
015:
016: import java.io.FileInputStream;
017: import java.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.List;
020:
021: public class TomcatUserImporter {
022: public static void main(String[] args) {
023: Logger log = Logger.getLogger(TomcatUserImporter.class);
024:
025: String filename = null;
026: if (args.length > 0) {
027: filename = args[0];
028: } else {
029: log.error("usage: TomcatUserImporter filename");
030: return;
031: }
032:
033: Digester digester = new Digester();
034: digester.setValidating(false);
035:
036: digester.addObjectCreate("tomcat-users", ArrayList.class);
037:
038: digester.addObjectCreate("tomcat-users/user", User.class);
039: digester.addSetProperties("tomcat-users/user");
040: digester.addSetNext("tomcat-users/user", "add");
041:
042: try {
043: FileInputStream in = new FileInputStream(filename);
044: List users = (List) digester.parse(in);
045:
046: HibernateHelper.initializeHibernate();
047: Session session = GlobalSessionFactory.get().openSession();
048: XPlannerLoginModule encryptor = new XPlannerLoginModule(
049: new LoginSupportImpl());
050: Iterator userItr = users.iterator();
051: while (userItr.hasNext()) {
052: User user = (User) userItr.next();
053: try {
054: List projects = session
055: .find("from project in class "
056: + Project.class.getName());
057: List people = session.find("from person in class "
058: + Person.class.getName()
059: + " where userid = ?", user.getUsername(),
060: Hibernate.STRING);
061: Person person = null;
062: Iterator peopleIterator = people.iterator();
063: if (peopleIterator.hasNext()) {
064: person = (Person) peopleIterator.next();
065: if (person.getPassword() == null) {
066: log.info("setting password: user="
067: + user.getUsername());
068: person.setPassword(encryptor
069: .encodePassword(user.getPassword(),
070: null));
071: }
072: String[] roles = user.getRoles().split(",");
073: for (int i = 0; i < roles.length; i++) {
074: Iterator projectItr = projects.iterator();
075: while (projectItr.hasNext()) {
076: Project project = (Project) projectItr
077: .next();
078: if (!isUserInRole(person, project
079: .getId(), roles[i])) {
080: Role role = new Role(roles[i]);
081: log.info("adding role: user="
082: + user.getUsername()
083: + ", role=" + roles[i]);
084: session.save(role);
085: }
086: }
087: }
088: } else {
089: log.warn("no xplanner user: "
090: + user.getUsername());
091: }
092: session.flush();
093: session.connection().commit();
094: } catch (Throwable e) {
095: session.connection().rollback();
096: }
097: }
098: session.close();
099: } catch (Exception e) {
100: e.printStackTrace();
101: }
102: }
103:
104: private static boolean isUserInRole(Person person, int projectId,
105: String roleName) {
106: // Iterator roleItr = person.getRoles().iterator();
107: // while (roleItr.hasNext()) {
108: // Role role = (Role)roleItr.next();
109: // // do-before-release Check if Importer is still correct
110: // if (role.fromNameKey().equals(roleName)) {
111: // return true;
112: // }
113: // }
114: return false;
115: }
116:
117: public static class User {
118: private String username;
119: private String password;
120: private String roles;
121:
122: public String getUsername() {
123: return username;
124: }
125:
126: public void setUsername(String username) {
127: this .username = username;
128: }
129:
130: public String getPassword() {
131: return password;
132: }
133:
134: public void setPassword(String password) {
135: this .password = password;
136: }
137:
138: public String getRoles() {
139: return roles;
140: }
141:
142: public void setRoles(String roles) {
143: this.roles = roles;
144: }
145: }
146: }
|