1919
2020package com.test
2121
22+ import com.unboundid.ldap.listener.InMemoryDirectoryServer
23+ import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig
24+ import com.unboundid.ldap.listener.InMemoryListenerConfig
25+ import com.unboundid.ldap.sdk.Attribute
26+ import com.unboundid.ldap.sdk.Entry
27+
2228import grails.boot.GrailsApp
2329import grails.boot.config.GrailsAutoConfiguration
2430
2531import groovy.transform.CompileStatic
2632
2733@CompileStatic
2834class Application extends GrailsAutoConfiguration {
35+
36+ static InMemoryDirectoryServer directoryServer
37+ static private Entry scientistsUnit
38+
2939 static void main (String [] args ) {
40+ def config = new InMemoryDirectoryServerConfig (' dc=example,dc=com' )
41+ config. addAdditionalBindCredentials(' cn=admin,dc=example,dc=com' , ' secret' )
42+ config. setListenerConfigs(
43+ InMemoryListenerConfig . createLDAPConfig(
44+ ' default' ,
45+ null ,
46+ 0 ,
47+ null ,
48+ false ,
49+ false
50+ )
51+ )
52+
53+ directoryServer = new InMemoryDirectoryServer (config)
54+ def base = new Entry (
55+ ' dc=example,dc=com' ,
56+ new Attribute (' objectClass' , ' top' , ' domain' ),
57+ new Attribute (' dc' , ' example' )
58+ )
59+ directoryServer. add(base)
60+
61+ def people = new Entry (
62+ ' ou=people,dc=example,dc=com' ,
63+ new Attribute (' objectClass' , ' top' , ' organizationalUnit' ),
64+ new Attribute (' ou' , ' people' )
65+ )
66+ directoryServer. add(people)
67+
68+ def mathematiciansUnit = new Entry (
69+ ' ou=mathematicians,dc=example,dc=com' ,
70+ new Attribute (' objectClass' , ' top' , ' organizationalUnit' ),
71+ new Attribute (' ou' , ' mathematicians' )
72+ )
73+ directoryServer. add(mathematiciansUnit)
74+
75+ scientistsUnit = new Entry (
76+ ' ou=scientists,dc=example,dc=com' ,
77+ new Attribute (' objectClass' , ' top' , ' organizationalUnit' ),
78+ new Attribute (' ou' , ' scientists' )
79+ )
80+ directoryServer. add(scientistsUnit)
81+
82+ def jane = new Entry (
83+ ' uid=jane,ou=people,dc=example,dc=com' ,
84+ new Attribute (' objectClass' , ' inetOrgPerson' ),
85+ new Attribute (' uid' , ' jane' ),
86+ new Attribute (' cn' , ' Jane Doe' ),
87+ new Attribute (' sn' , ' Doe' ),
88+ new Attribute (
' mail' ,
' [email protected] ' ),
89+ new Attribute (' telephoneNumber' , ' +1 555 111 2222' ),
90+ new Attribute (' userPassword' , ' password' )
91+ )
92+ directoryServer. add(jane)
93+
94+ [' riemann' , ' gauss' , ' euler' , ' euclid' ]. each { uid ->
95+ directoryServer. add(new Entry (
96+ " uid=$uid ,ou=mathematicians,dc=example,dc=com" as String ,
97+ new Attribute (' objectClass' , ' inetOrgPerson' ),
98+ new Attribute (' uid' , uid),
99+ new Attribute (' cn' , uid. capitalize()),
100+ new Attribute (' sn' , uid. capitalize()),
101+ new Attribute (' userPassword' , ' password' )
102+ ))
103+ }
104+
105+ def mathGroup = new Entry (
106+ ' cn=mathematicians,ou=mathematicians,dc=example,dc=com' ,
107+ new Attribute (' objectClass' , ' top' , ' groupOfUniqueNames' ),
108+ new Attribute (' cn' , ' mathematicians' ),
109+ new Attribute (' uniqueMember' ,
110+ ' uid=riemann,ou=mathematicians,dc=example,dc=com' ,
111+ ' uid=gauss,ou=mathematicians,dc=example,dc=com' ,
112+ ' uid=euler,ou=mathematicians,dc=example,dc=com' ,
113+ ' uid=euclid,ou=mathematicians,dc=example,dc=com'
114+ )
115+ )
116+ directoryServer. add(mathGroup)
117+
118+ [' einstein' , ' newton' , ' galieleo' , ' tesla' ]. each { uid ->
119+ directoryServer. add(new Entry (
120+ " uid=$uid ,ou=scientists,dc=example,dc=com" as String ,
121+ new Attribute (' objectClass' , ' inetOrgPerson' ),
122+ new Attribute (' uid' , uid),
123+ new Attribute (' cn' , uid. capitalize()),
124+ new Attribute (' sn' , uid. capitalize()),
125+ new Attribute (' userPassword' , ' password' )
126+ ))
127+ }
128+ def scientistGroup = new Entry (
129+ ' cn=scientists,ou=scientists,dc=example,dc=com' ,
130+ new Attribute (' objectClass' , ' top' , ' groupOfUniqueNames' ),
131+ new Attribute (' cn' , ' scientists' ),
132+ new Attribute (' uniqueMember' ,
133+ ' uid=einstein,ou=scientists,dc=example,dc=com' ,
134+ ' uid=newton,ou=scientists,dc=example,dc=com' ,
135+ ' uid=galieleo,ou=scientists,dc=example,dc=com' ,
136+ ' uid=tesla,ou=scientists,dc=example,dc=com'
137+ )
138+ )
139+ directoryServer. add(scientistGroup)
140+ directoryServer. startListening()
141+ System . setProperty(' grails.test.ldap.url' , " ldap://localhost:$directoryServer . listenPort " )
142+
30143 GrailsApp . run(Application , args)
31144 }
145+
146+ @Override
147+ void onShutdown (Map<String , Object > event ) {
148+ if (directoryServer) {
149+ directoryServer. close()
150+ directoryServer = null
151+ }
152+ }
32153}
0 commit comments