2828use Exception ;
2929use OC \Group \Backend ;
3030use OCA \LdapWriteSupport \AppInfo \Application ;
31+ use OCA \LdapWriteSupport \Service \Configuration ;
3132use OCA \User_LDAP \Group_Proxy ;
3233use OCA \User_LDAP \ILDAPGroupPlugin ;
3334use OCP \IGroupManager ;
35+ use OCP \IUser ;
36+ use OCP \IUserSession ;
3437use OCP \LDAP \ILDAPProvider ;
3538use Psr \Log \LoggerInterface ;
3639
3740class LDAPGroupManager implements ILDAPGroupPlugin {
41+ /** @var Configuration */
42+ protected $ configuration ;
3843
3944 /** @var ILDAPProvider */
4045 private $ ldapProvider ;
4146
47+ /** @var IUserSession */
48+ private $ userSession ;
49+
4250 /** @var IGroupManager */
4351 private $ groupManager ;
4452
@@ -47,11 +55,13 @@ class LDAPGroupManager implements ILDAPGroupPlugin {
4755 /** @var LoggerInterface */
4856 private $ logger ;
4957
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 ) {
5159 $ this ->groupManager = $ groupManager ;
60+ $ this ->userSession = $ userSession ;
5261 $ this ->ldapConnect = $ ldapConnect ;
62+ $ this ->ldapProvider = $ ldapProvider ;
63+ $ this ->configuration = $ configuration ;
5364 $ this ->logger = $ logger ;
54- $ this ->ldapProvider = $ LDAPProvider ;
5565
5666 if ($ this ->ldapConnect ->groupsEnabled ()) {
5767 $ this ->makeLdapBackendFirst ();
@@ -82,15 +92,27 @@ public function respondToActions() {
8292 * @return string|null
8393 */
8494 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+ }
85114
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 );
94116 $ newGroupDN = $ this ->ldapProvider ->sanitizeDN ([$ newGroupDN ])[0 ];
95117
96118 if ($ ret = ldap_add ($ connection , $ newGroupDN , $ newGroupEntry )) {
@@ -151,7 +173,6 @@ public function addToGroup($uid, $gid) {
151173 break ;
152174 case 'gidNumber ' :
153175 throw new Exception ('Cannot add to group when gidNumber is used as relation ' );
154- break ;
155176 }
156177
157178 if (!$ ret = ldap_mod_add ($ connection , $ groupDN , $ entry )) {
@@ -220,12 +241,30 @@ public function isLDAPGroup($gid): bool {
220241 }
221242 }
222243
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 ];
229268 }
230269
231270 public function makeLdapBackendFirst (): void {
0 commit comments