28
28
use Exception ;
29
29
use OC \Group \Backend ;
30
30
use OCA \LdapWriteSupport \AppInfo \Application ;
31
+ use OCA \LdapWriteSupport \Service \Configuration ;
31
32
use OCA \User_LDAP \Group_Proxy ;
32
33
use OCA \User_LDAP \ILDAPGroupPlugin ;
33
34
use OCP \IGroupManager ;
35
+ use OCP \IUser ;
36
+ use OCP \IUserSession ;
34
37
use OCP \LDAP \ILDAPProvider ;
35
38
use Psr \Log \LoggerInterface ;
36
39
37
40
class LDAPGroupManager implements ILDAPGroupPlugin {
41
+ /** @var Configuration */
42
+ protected $ configuration ;
38
43
39
44
/** @var ILDAPProvider */
40
45
private $ ldapProvider ;
41
46
47
+ /** @var IUserSession */
48
+ private $ userSession ;
49
+
42
50
/** @var IGroupManager */
43
51
private $ groupManager ;
44
52
@@ -47,11 +55,13 @@ class LDAPGroupManager implements ILDAPGroupPlugin {
47
55
/** @var LoggerInterface */
48
56
private $ logger ;
49
57
50
- public function __construct (IGroupManager $ groupManager , LDAPConnect $ ldapConnect , LoggerInterface $ logger , ILDAPProvider $ LDAPProvider ) {
58
+ public function __construct (IGroupManager $ groupManager , IUserSession $ userSession , LDAPConnect $ ldapConnect , LoggerInterface $ logger , ILDAPProvider $ LDAPProvider ) {
51
59
$ this ->groupManager = $ groupManager ;
60
+ $ this ->userSession = $ userSession ;
52
61
$ this ->ldapConnect = $ ldapConnect ;
62
+ $ this ->ldapProvider = $ ldapProvider ;
63
+ $ this ->configuration = $ configuration ;
53
64
$ this ->logger = $ logger ;
54
- $ this ->ldapProvider = $ LDAPProvider ;
55
65
56
66
if ($ this ->ldapConnect ->groupsEnabled ()) {
57
67
$ this ->makeLdapBackendFirst ();
@@ -82,15 +92,27 @@ public function respondToActions() {
82
92
* @return string|null
83
93
*/
84
94
public function createGroup ($ gid ) {
95
+ $ adminUser = $ this ->userSession ->getUser ();
96
+ $ requireActorFromLDAP = $ this ->configuration ->isLdapActorRequired ();
97
+ if ($ requireActorFromLDAP && !$ adminUser instanceof IUser) {
98
+ throw new Exception ('Acting user is not from LDAP ' );
99
+ }
100
+ try {
101
+ $ connection = $ this ->ldapProvider ->getLDAPConnection ($ adminUser ->getUID ());
102
+ // TODO: what about multiple bases?
103
+ $ base = $ this ->ldapProvider ->getLDAPBaseGroups ($ adminUser ->getUID ());
104
+ } catch (Exception $ e ) {
105
+ if ($ requireActorFromLDAP ) {
106
+ if ($ this ->configuration ->isPreventFallback ()) {
107
+ throw new \Exception ('Acting admin is not from LDAP ' , 0 , $ e );
108
+ }
109
+ return false ;
110
+ }
111
+ $ connection = $ this ->ldapConnect ->getLDAPConnection ();
112
+ $ base = $ this ->ldapConnect ->getLDAPBaseGroups ()[0 ];
113
+ }
85
114
86
- /**
87
- * FIXME could not create group using LDAPProvider, because its methods rely
88
- * on passing an already inserted [ug]id, which we do not have at this point.
89
- */
90
-
91
- $ newGroupEntry = $ this ->buildNewEntry ($ gid );
92
- $ connection = $ this ->ldapConnect ->getLDAPConnection ();
93
- $ newGroupDN = "cn= $ gid, " . $ this ->ldapConnect ->getLDAPBaseGroups ()[0 ];
115
+ list ($ newGroupDN , $ newGroupEntry ) = $ this ->buildNewEntry ($ gid , $ base );
94
116
$ newGroupDN = $ this ->ldapProvider ->sanitizeDN ([$ newGroupDN ])[0 ];
95
117
96
118
if ($ ret = ldap_add ($ connection , $ newGroupDN , $ newGroupEntry )) {
@@ -151,7 +173,6 @@ public function addToGroup($uid, $gid) {
151
173
break ;
152
174
case 'gidNumber ' :
153
175
throw new Exception ('Cannot add to group when gidNumber is used as relation ' );
154
- break ;
155
176
}
156
177
157
178
if (!$ ret = ldap_mod_add ($ connection , $ groupDN , $ entry )) {
@@ -220,12 +241,30 @@ public function isLDAPGroup($gid): bool {
220
241
}
221
242
}
222
243
223
- private function buildNewEntry ($ gid ): array {
224
- return [
225
- 'objectClass ' => ['groupOfNames ' , 'top ' ],
226
- 'cn ' => $ gid ,
227
- 'member ' => ['' ]
228
- ];
244
+ private function buildNewEntry ($ gid , $ base ): array {
245
+ $ ldif = $ this ->configuration ->getGroupTemplate ();
246
+
247
+ $ ldif = str_replace ('{GID} ' , $ gid , $ ldif );
248
+ $ ldif = str_replace ('{BASE} ' , $ base , $ ldif );
249
+
250
+ $ entry = [];
251
+ $ lines = explode (PHP_EOL , $ ldif );
252
+ foreach ($ lines as $ line ) {
253
+ $ split = explode (': ' , $ line , 2 );
254
+ $ key = trim ($ split [0 ]);
255
+ $ value = trim ($ split [1 ]);
256
+ if (!isset ($ entry [$ key ])) {
257
+ $ entry [$ key ] = $ value ;
258
+ } else if (is_array ($ entry [$ key ])) {
259
+ $ entry [$ key ][] = $ value ;
260
+ } else {
261
+ $ entry [$ key ] = [$ entry [$ key ], $ value ];
262
+ }
263
+ }
264
+ $ dn = $ entry ['dn ' ];
265
+ unset($ entry ['dn ' ]);
266
+
267
+ return [$ dn , $ entry ];
229
268
}
230
269
231
270
public function makeLdapBackendFirst (): void {
0 commit comments