1 package de.webman.sync.ldap.worker; 2 3 import de.webman.sync.Worker; 4 import de.webman.sync.ACLAdaptor; 5 import de.webman.sync.SyncException; 6 import de.webman.sync.ldap.LDAPAdaptor; 7 import java.util.List ; 8 import java.util.Iterator ; 9 import java.util.Enumeration ; 10 import java.util.HashSet ; 11 import java.util.Map ; 12 import java.util.HashMap ; 13 import org.apache.log4j.Category; 14 import de.webman.acl.UserFactory; 15 import de.webman.acl.Profile; 16 import de.webman.acl.ProfileFactory; 17 import com.teamkonzept.lib.TKException; 18 import com.teamkonzept.lib.TKVector; 19 20 21 28 public class CreateMissingUsers 29 implements Worker 30 { 31 32 33 36 public static Category cat = Category.getInstance(CreateMissingUsers.class); 37 38 41 private static final int STAT_NEW = 1; 42 43 46 private static final int STAT_CREATED = 2; 47 48 51 private static final int STAT_WARNING = 3; 52 53 56 private static final int STAT_IGNORED = 4; 57 58 61 private static final int STAT_FAILED = 5; 62 63 66 private static final int STAT_SLOTS = 6; 67 68 73 public void run(ACLAdaptor adaptor) 74 throws SyncException 75 { 76 cat.debug("synchronize for new users"); 77 78 HashMap groups = null; 79 try { 80 81 TKVector pfvect = ProfileFactory.getInstance().getProfiles(); 82 groups = new HashMap (pfvect.size()); 83 84 85 for (Enumeration en = pfvect.elements(); en.hasMoreElements(); ) { 86 Profile p = (Profile)en.nextElement(); 87 88 if (p.isProfile()) 89 groups.put(p.getLogin(), p); 90 } 91 } 92 catch (Exception e) { 93 throw new SyncException(e); 94 } 95 96 HashSet wmlogins = null; 97 98 99 try { 100 TKVector webman_users = UserFactory.getInstance().getUsers(); 101 wmlogins = new HashSet (webman_users.size()); 102 103 105 for (Enumeration en = webman_users.elements(); en.hasMoreElements(); ) { 106 de.webman.acl.User wmu = (de.webman.acl.User)en.nextElement(); 107 108 wmlogins.add(wmu.getLogin()); 109 } 110 } 111 catch (TKException te) { 112 throw new SyncException(te); 113 } 114 115 List ldap_users = adaptor.getAllUsers(); 116 int[] statistics = new int[STAT_SLOTS]; for (Iterator it = ldap_users.iterator(); it.hasNext(); ) { 118 de.webman.sync.User ldu = (de.webman.sync.User)it.next(); 119 120 if (wmlogins.contains(ldu.getWebmanName())) 121 cat.debug ("user found in webman: '" + ldu.getWebmanName() + "'"); 122 else { 123 statistics[STAT_NEW]++; 124 125 if ((adaptor instanceof LDAPAdaptor) && 126 ((LDAPAdaptor)adaptor).ignoreUsers().contains(ldu.getWebmanName())) { 127 statistics[STAT_IGNORED]++; 128 } 129 else if (createNewUser(ldu, groups, statistics)) 130 statistics[STAT_CREATED]++; 131 } 132 } 133 134 cat.info("synchronize for new users successfully; STATISTICS:\n" + 135 ldap_users.size() + " found in LDAP system,\n" + 136 wmlogins.size() + " found in webman,\n" + 137 statistics[STAT_NEW] + " to allocated new,\n" + 138 statistics[STAT_CREATED] + " created successfully\n" + 139 statistics[STAT_FAILED] + " user creations failed,\n" + 140 statistics[STAT_WARNING] + " group assignment warnings,\n" + 141 statistics[STAT_IGNORED] + " users ignored"); 142 } 143 144 145 152 private boolean createNewUser(de.webman.sync.User user, Map groups, 153 int[] statistics) { 154 cat.info ("NEW USER: '" + user.getWebmanName() + "'"); 155 156 try { 157 de.webman.acl.User wmu = null; 158 159 wmu = UserFactory.getInstance().createUser(user.getWebmanName(), 160 user.getDisplayName()); 161 162 165 for (Iterator it = user.getGroups().iterator(); it.hasNext(); ) { 166 String ldapgroup = (String )it.next(); 167 168 if (groups.containsKey(ldapgroup)) { 169 assignUserToGroup((Profile)groups.get(ldapgroup), wmu); 170 } 171 else { 172 cat.warn("unknown webman-group found in LDAP: '" + ldapgroup + "' " + 173 "(user-dn: '" + user.getID() + "')"); 174 statistics[STAT_WARNING] ++; 175 } 176 } 177 } 178 catch (TKException tke) { 179 cat.error("creation of new users '" + user.getWebmanName() + "' [='" + 180 user.getID() + "'] failed (" + tke + ")"); 181 statistics[STAT_FAILED]++; 182 } 183 184 return true; 185 } 186 187 188 private boolean assignUserToGroup(Profile group, de.webman.acl.Login wmuser) 189 { 190 try { 191 cat.info("assign user '" + wmuser.getLogin() + "' to group '" + 192 group.getLogin() + "' "); 193 group.addChild(wmuser); 194 ProfileFactory.getInstance().modifyProfile(group); 195 return true; 196 } 197 catch (Exception e) { 198 cat.warn("can't assign user '" + wmuser.getLogin() + "' to group '" + 199 group.getLogin() + "'"); 200 return false; 201 } 202 } 203 204 } 205 | Popular Tags |