Difference between revisions of "Knowledge:Script Utilities"
From OpenKM Documentation
(→LDAP connection test) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 50: | Line 50: | ||
changeNodes("folder-uuid"); | changeNodes("folder-uuid"); | ||
+ | </source> | ||
+ | |||
+ | == LDAP connection test == | ||
+ | <source lang="java"> | ||
+ | import java.util.*; | ||
+ | import javax.naming.*; | ||
+ | import javax.naming.directory.*; | ||
+ | |||
+ | try { | ||
+ | Hashtable env = new Hashtable(); | ||
+ | env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); | ||
+ | env.put(Context.PROVIDER_URL, "ldap://192.168.100.5"); | ||
+ | env.put(Context.SECURITY_AUTHENTICATION, "simple"); | ||
+ | env.put(Context.SECURITY_PRINCIPAL, "CN=openkm,CN=Users,DC=possehlelectronics,DC=nl"); | ||
+ | env.put(Context.SECURITY_CREDENTIALS, "****"); | ||
+ | |||
+ | DirContext ctx = new InitialDirContext(env); | ||
+ | |||
+ | print ("autentificado<br/>"); | ||
+ | |||
+ | List searchBases = new ArrayList(); | ||
+ | searchBases.add("cn=Users,DC=possehlelectronics,DC=nl"); | ||
+ | String searchFilter = "(&(objectClass=person)(|(memberOf=CN=ROLE_ADMIN,OU=Groups,DC=possehlelectronics,DC=nl)(memberOf=CN=ROLE_USER,OU=Groups,DC=possehlelectronics,DC=nl)))"; | ||
+ | String attribute = "cn"; | ||
+ | |||
+ | SearchControls searchCtls = new SearchControls(); | ||
+ | searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); | ||
+ | |||
+ | for (String searchBase : searchBases) { | ||
+ | NamingEnumeration results = ctx.search(searchBase, searchFilter, searchCtls); | ||
+ | |||
+ | while (results.hasMore()) { | ||
+ | print("Resultado:"); | ||
+ | SearchResult searchResult = (SearchResult) results.next(); | ||
+ | Attributes attributes = searchResult.getAttributes(); | ||
+ | |||
+ | if (attribute.equals("")) { | ||
+ | StringBuilder sb = new StringBuilder(); | ||
+ | |||
+ | for (NamingEnumeration ne = attributes.getAll(); ne.hasMore();) { | ||
+ | Attribute attr = (Attribute) ne.nextElement(); | ||
+ | sb.append(attr.toString()); | ||
+ | sb.append("\n"); | ||
+ | } | ||
+ | |||
+ | al.add(sb.toString()); | ||
+ | print(sb.toString()+"<br/>"); | ||
+ | } else { | ||
+ | Attribute attrib = attributes.get(attribute); | ||
+ | |||
+ | if (attrib != null) { | ||
+ | String item = (String) attrib.get(); | ||
+ | print("Item:"+item+"<br/>"); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | ctx.close(); | ||
+ | |||
+ | } catch(NamingException ne) { | ||
+ | print("Error authenticating user:"); | ||
+ | print(ne.getMessage()); | ||
+ | } | ||
</source> | </source> |
Latest revision as of 12:53, 10 December 2012
Contents
Get principals
import com.openkm.spring.PrincipalUtils;
import java.util.*;
HashSet roles = PrincipalUtils.getRoles();
for (Iterator it = roles.iterator(); it.hasNext();) {
String role = (String) it.next();
print("{"+role+"}<br/>");
}
print(PrincipalUtils.getRoles());
List missing datastore document
import com.openkm.module.db.stuff.*;
import com.openkm.dao.bean.*;
import com.openkm.dao.*;
for (NodeDocumentVersion ndv : NodeDocumentVersionDAO.getInstance().findAll()) {
String verUuid = ndv.getUuid();
File file = FsDataStore.resolveFile(verUuid);
if (!file.exists()) {
String docUuid = ndv.getParent();
String docPath = NodeBaseDAO.getInstance().getPathFromUuid(docUuid);
print("File: " + file + "<br/>");
print("Path: " + docPath + "<br/>");
}
}
Force text extraction from a folder
import com.openkm.dao.bean.*;
import com.openkm.dao.*;
void changeNodes(String parentUuid) {
for (NodeFolder nFld : NodeFolderDAO.getInstance().findByParent(parentUuid)) {
print("Folder: " + nFld.getUuid() + "<br/>");
changeNodes(nFld.getUuid());
}
for (NodeDocument nDoc : NodeDocumentDAO.getInstance().findByParent(parentUuid)) {
print("Document: " + nDoc.getUuid() + "<br/>");
NodeDocumentDAO.getInstance().resetPendingExtractionFlag(nDoc.getUuid());
}
}
changeNodes("folder-uuid");
LDAP connection test
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.100.5");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "CN=openkm,CN=Users,DC=possehlelectronics,DC=nl");
env.put(Context.SECURITY_CREDENTIALS, "****");
DirContext ctx = new InitialDirContext(env);
print ("autentificado<br/>");
List searchBases = new ArrayList();
searchBases.add("cn=Users,DC=possehlelectronics,DC=nl");
String searchFilter = "(&(objectClass=person)(|(memberOf=CN=ROLE_ADMIN,OU=Groups,DC=possehlelectronics,DC=nl)(memberOf=CN=ROLE_USER,OU=Groups,DC=possehlelectronics,DC=nl)))";
String attribute = "cn";
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
for (String searchBase : searchBases) {
NamingEnumeration results = ctx.search(searchBase, searchFilter, searchCtls);
while (results.hasMore()) {
print("Resultado:");
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
if (attribute.equals("")) {
StringBuilder sb = new StringBuilder();
for (NamingEnumeration ne = attributes.getAll(); ne.hasMore();) {
Attribute attr = (Attribute) ne.nextElement();
sb.append(attr.toString());
sb.append("\n");
}
al.add(sb.toString());
print(sb.toString()+"<br/>");
} else {
Attribute attrib = attributes.get(attribute);
if (attrib != null) {
String item = (String) attrib.get();
print("Item:"+item+"<br/>");
}
}
}
}
ctx.close();
} catch(NamingException ne) {
print("Error authenticating user:");
print(ne.getMessage());
}