Wiki source code of test
Last modified by Thomas Coelho (local) on 2022/04/20 16:44
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{groovy}} | ||
2 | import org.xwiki.velocity.tools.EscapeTool; | ||
3 | import org.xwiki.contrib.ldap.XWikiLDAPConfig; | ||
4 | import org.xwiki.contrib.ldap.XWikiLDAPConnection; | ||
5 | import org.xwiki.contrib.ldap.XWikiLDAPSearchAttribute; | ||
6 | import com.novell.ldap.LDAPConnection; | ||
7 | |||
8 | def escapetool = new EscapeTool(); | ||
9 | |||
10 | def getConfig(param) { | ||
11 | return xwiki.getXWiki().getXWikiPreference(param, "xwiki.authentication." + param.replaceAll("ldap_", "ldap."), | ||
12 | "", xcontext.getContext()); | ||
13 | } | ||
14 | |||
15 | def getParamFromConfig(name) { | ||
16 | return getParamFromConfig(name, "ldap_" + name); | ||
17 | } | ||
18 | |||
19 | def getParamFromConfig(name, ldapname) { | ||
20 | if (request.getParameter(name)!=null) { | ||
21 | return request.getParameter(name) | ||
22 | } else { | ||
23 | return getConfig(ldapname); | ||
24 | } | ||
25 | } | ||
26 | |||
27 | def getParam(name, defaultvalue) { | ||
28 | if (request.getParameter(name)!=null) { | ||
29 | return request.getParameter(name) | ||
30 | } else { | ||
31 | return defaultvalue; | ||
32 | } | ||
33 | } | ||
34 | |||
35 | def getTrustedLDAPConfig() { | ||
36 | def map = new HashMap(); | ||
37 | for (paramname in ["server", "port", "base_DN", "bind_DN", "bind_pass"]) { | ||
38 | def param = xwiki.getXWiki().Param("xwiki.authentication.trustedldap.remoteUserMapping.ldap_" + paramname); | ||
39 | if (param==null) | ||
40 | continue; | ||
41 | try { | ||
42 | for (config in param.split("\\|")) { | ||
43 | def pos = config.indexOf("="); | ||
44 | domain = config.substring(0, pos); | ||
45 | domainparam = config.substring(pos + 1); | ||
46 | def domainmap = map.get(domain); | ||
47 | if (domainmap==null) { | ||
48 | domainmap = new HashMap(); | ||
49 | map.put(domain, domainmap); | ||
50 | } | ||
51 | domainmap.put(paramname, domainparam); | ||
52 | } | ||
53 | } catch (e) { | ||
54 | println "Failed reading param ${param}: " + e.getMessage(); | ||
55 | } | ||
56 | } | ||
57 | return map; | ||
58 | } | ||
59 | |||
60 | def map = getTrustedLDAPConfig(); | ||
61 | if (map.size()>0) { | ||
62 | print "Use config: " | ||
63 | for (key in map.keySet()) { | ||
64 | print """[[${key}>>||queryString="domain=${key}"]] """ | ||
65 | } | ||
66 | println "" | ||
67 | println "" | ||
68 | } | ||
69 | |||
70 | if (request.domain) { | ||
71 | server = map.get(request.domain).get("server"); | ||
72 | port = map.get(request.domain).get("port"); | ||
73 | binddn = map.get(request.domain).get("bind_DN"); | ||
74 | bindpassword = map.get(request.domain).get("bind_pass"); | ||
75 | basedn = map.get(request.domain).get("base_DN"); | ||
76 | } else { | ||
77 | server = getParamFromConfig("server") | ||
78 | port = getParamFromConfig("port") | ||
79 | binddn = getParamFromConfig("binddn", "ldap_bind_DN") | ||
80 | bindpassword = getParamFromConfig("bindpassword", "ldap_bind_pass") | ||
81 | basedn = getParamFromConfig("basedn", "ldap_base_DN") | ||
82 | } | ||
83 | |||
84 | |||
85 | if (port==null || port=="") port = "389" | ||
86 | query = getParam("query", "sn=ldubost") | ||
87 | params = getParam("params", "dn,samAccountName") | ||
88 | |||
89 | println """ | ||
90 | {{html clean="false"}} | ||
91 | <form action="" method="GET"> | ||
92 | <table> | ||
93 | <tr><td>Server</td><td><input type="text" name="server" value="${escapetool.xml(server)}" size="20" /></td></tr> | ||
94 | <tr><td>Port</td><td><input type="text" name="port" value="${escapetool.xml(port)}" size="20" /></td></tr> | ||
95 | <tr><td>Bind DN</td><td><input type="text" name="binddn" value="${escapetool.xml(binddn)}" size="80" /></td></tr> | ||
96 | <tr><td>Bind Password</td><td><input type="password" name="bindpassword" value="${escapetool.xml(bindpassword)}" size="20" /></td></tr> | ||
97 | <tr><td>Base DN</td><td><input type="text" name="basedn" value="${escapetool.xml(basedn)}" size="80" /></td></tr> | ||
98 | <tr><td>LDAP Query</td><td><input type="text" name="query" value="${escapetool.xml(query)}" size="80"/></td></tr> | ||
99 | <tr><td>Params</td><td><input type="text" name="params" value="${escapetool.xml(params)}" size="80"/></td></tr> | ||
100 | </table> | ||
101 | |||
102 | <input type="submit" value="Go" class="button" /> | ||
103 | </form> | ||
104 | {{/html}} | ||
105 | """ | ||
106 | |||
107 | def connection = new LDAPConnection(); | ||
108 | |||
109 | try { | ||
110 | // connect | ||
111 | connection.connect(server, Integer.parseInt(port)) | ||
112 | connection.bind(LDAPConnection.LDAP_V3, binddn, bindpassword.getBytes("UTF8")); | ||
113 | def paramslist = null; | ||
114 | if (params!="") { | ||
115 | paramslist = params.split(",") | ||
116 | } | ||
117 | def results = connection.search(basedn, (int) 2, query, paramslist, false); | ||
118 | while (results.hasMore()) { | ||
119 | try { | ||
120 | def entry = results.next() | ||
121 | def dn = entry.getDN() | ||
122 | println "* ${dn}" | ||
123 | def attrs = entry.getAttributeSet() | ||
124 | for (attr in attrs) { | ||
125 | try { | ||
126 | println "** ${attr.getName()} ${attr.getStringValue()}" | ||
127 | |||
128 | if (attr.getName()=="member") { | ||
129 | for (item in attr.getStringValueArray()) { | ||
130 | println "*** ${item}" | ||
131 | } | ||
132 | } | ||
133 | |||
134 | } catch(e2) { | ||
135 | println "** Exception getting attribute" | ||
136 | } | ||
137 | } | ||
138 | } catch(e3) { | ||
139 | println "** Exception calling next"+e3 | ||
140 | } | ||
141 | } | ||
142 | } catch (e) { | ||
143 | e.printStackTrace(); | ||
144 | println("Exception") | ||
145 | println(e.getMessage()) | ||
146 | println(org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(e)); | ||
147 | } | ||
148 | |||
149 | {{/groovy}} |