Crio a conexao como num banco de dados.
[code]private DirContext connect() throws NamingException{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.0.10:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "CN=Teste,OU=Fabrica,OU=Departamentos,DC=teste,DC=com");
env.put(Context.SECURITY_CREDENTIALS, "teste");
DirContext ctx = new InitialDirContext(env);
return ctx;
}[/code]
Listo os usuarios buscados emuma tabela
[code]public List getUsers() throws NamingException{
DirContext ctx = this.connect();
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String attrs[] = {"userAccountControl","sAMAccountName","cn", "telephoneNumber", "sn", "userPrincipalName","memberOf","name","mail"};
ctls.setReturningAttributes(attrs);
String filter = "(objectClass=User)";
NamingEnumeration answer = ctx.search(CONTEXT_URL, filter, ctls);
List<User> users = new ArrayList<User>();
while(answer.hasMoreElements()){
SearchResult sr = (SearchResult) answer.next();
Attributes attrbs = sr.getAttributes();
String name = attrbs.get("name") ==null ?"":attrbs.get("name").toString().split(":")[1];
String samId = attrbs.get("sAMAccountName") ==null ?"":attrbs.get("sAMAccountName").toString().split(":")[1];
String cn = attrbs.get("cn") ==null ?"":attrbs.get("cn").toString().split(":")[1];
String telephoneNumber = attrbs.get("telephoneNumber") ==null ?"":attrbs.get("telephoneNumber").toString().split(":")[1];
String sn = attrbs.get("sn") ==null ?"":attrbs.get("sn").toString().split(":")[1];
String userPrincipalName = attrbs.get("userPrincipalName") ==null ?"":attrbs.get("userPrincipalName").toString().split(":")[1];
String mail = attrbs.get("mail") ==null ?"":attrbs.get("mail").toString().split(":")[1];
String memberOf = attrbs.get("memberOf") ==null ?"":attrbs.get("memberOf").toString().split(":")[1];
User user = new User(name,samId,cn,telephoneNumber,sn,userPrincipalName,mail,memberOf);
users.add(user);
}
ctx.close();
return users;
}[/code]
Disabilita o usuario no ad pq qdo c clica la na tela do ad para desabilitar um valor inteiro la muda de 115 para 118 se nao me engano
[code]public void disableUser(String name) throws NamingException {
DirContext ctx = this.connect();
int UF_ACCOUNTDISABLE = 0x0002;
int UF_PASSWD_NOTREQD = 0x0020;
int UF_PASSWD_CANT_CHANGE = 0x0040;
int UF_NORMAL_ACCOUNT = 0x0200;
int UF_DONT_EXPIRE_PASSWD = 0x10000;
int UF_PASSWORD_EXPIRED = 0x800000;
ModificationItem[] mods = new ModificationItem[1];
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl",Integer.toString(UF_NORMAL_ACCOUNT + UF_ACCOUNTDISABLE)));
ctx.modifyAttributes(name, mods);
ctx.close();
}[/code]
deleta o usuario pelo nome
[code]public void deleteUser(String name) throws NamingException {
DirContext ctx = this.connect();
ctx.unbind(name);
ctx.close();
}[/code]
Aqui é o preenchimento da minha tabela
[code]protected Control createDialogArea(Composite parent) {
Composite composite = new Composite(parent,SWT.NONE);
composite.setLayout(new GridLayout(1,false));
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
table = new Table(composite,SWT.BORDER | SWT.CHECK |
SWT.MULTI | SWT.FULL_SELECTION);
table.setLayoutData(new GridData(GridData.FILL_BOTH));
clnName = new TableColumn(table,SWT.LEFT);
clnName.setText("Nome");
samId = new TableColumn(table,SWT.LEFT);
samId.setText("SAM ID");
clnCn = new TableColumn(table,SWT.LEFT);
clnCn.setText("Nome Comum");
clnTelephoneNumber = new TableColumn(table,SWT.LEFT);
clnTelephoneNumber.setText("Telefone");
clnSn = new TableColumn(table,SWT.LEFT);
clnSn.setText("Sobrenome");
clnUserPrincipalName = new TableColumn(table,SWT.LEFT);
clnUserPrincipalName.setText("Nome Principal");
clnMail = new TableColumn(table,SWT.LEFT);
clnMail.setText("E-mail");
clnMemberOf = new TableColumn(table,SWT.LEFT);
clnMemberOf.setText("Membro de:");
table.setLinesVisible(true);
table.setHeaderVisible(true);
this.fillTable();
for(TableColumn c : table.getColumns()){
c.pack();
}
return super.createDialogArea(composite);
}
private void fillTable() {
Access a = new Access();
try {
List<User> users = a.getUsers();
for(User user : users){
TableItem itm = new TableItem(table,SWT.NONE);
itm.setText(new String[]{user.getName(),user.getSamId(),user.getCn(),user.getTelephoneNumber(),user.getSn(),user.getUserPrincipalName(),user.getMail(),user.getMemberOf()});
itm.setChecked(true);
}
} catch (NamingException e) {
e.printStackTrace();
}
}[/code]
Aqui é quando eu clico no botao desabilitar que muda la o atributo
[code]private void disablePressed() {
for(TableItem item : table.getItems()){
if(item.getChecked()){
if(MessageDialog.openConfirm(this.getShell(), "", "Deseja desabilitar o usuário " + item.getText(0) + "?")){
String name = "CN=";
String att[] = (item.getText(2).trim()+","+item.getText(7).trim()).split(",");
for(String s : att){
if(!s.startsWith("CN="))
name += s + ",";
}
name = name.substring(0, name.length()-1);
Access a = new Access();
try {
a.disableUser(name);
MessageDialog.openInformation(this.getShell(), "", "Usuário desabilitado.");
item.setForeground(new Color(this.getShell().getDisplay(),new RGB(200,0,0)));
} catch (NamingException e) {
e.printStackTrace();
}
}
}
}
}[/code]
a parte de deletar nao fiz mas é so passar o nome que deleta normal nao fiz pq tava sem base de teste