@@ -74,9 +74,21 @@ class Principal implements BackendInterface {
7474
7575 /** @var bool */
7676 private $ hasCircles ;
77+
7778 /** @var ProxyMapper */
7879 private $ proxyMapper ;
7980
81+ /**
82+ * Principal constructor.
83+ *
84+ * @param IUserManager $userManager
85+ * @param IGroupManager $groupManager
86+ * @param IShareManager $shareManager
87+ * @param IUserSession $userSession
88+ * @param IAppManager $appManager
89+ * @param ProxyMapper $proxyMapper
90+ * @param string $principalPrefix
91+ */
8092 public function __construct (IUserManager $ userManager ,
8193 IGroupManager $ groupManager ,
8294 IShareManager $ shareManager ,
@@ -169,13 +181,28 @@ public function getPrincipalByPath($path) {
169181 * @throws Exception
170182 */
171183 public function getGroupMemberSet ($ principal ) {
172- // TODO: for now the group principal has only one member, the user itself
173- $ principal = $ this ->getPrincipalByPath ($ principal );
174- if (!$ principal ) {
175- throw new Exception ('Principal not found ' );
184+ $ members = [];
185+
186+ if ($ this ->isProxyPrincipal ($ principal )) {
187+ $ realPrincipal = $ this ->getPrincipalUriFromProxyPrincipal ($ principal );
188+ $ principalArray = $ this ->getPrincipalByPath ($ realPrincipal );
189+ if (!$ principalArray ) {
190+ throw new Exception ('Principal not found ' );
191+ }
192+
193+ $ proxies = $ this ->proxyMapper ->getProxiesOf ($ principalArray ['uri ' ]);
194+ foreach ($ proxies as $ proxy ) {
195+ if ($ this ->isReadProxyPrincipal ($ principal ) && $ proxy ->getPermissions () === ProxyMapper::PERMISSION_READ ) {
196+ $ members [] = $ proxy ->getProxyId ();
197+ }
198+
199+ if ($ this ->isWriteProxyPrincipal ($ principal ) && $ proxy ->getPermissions () === (ProxyMapper::PERMISSION_READ | ProxyMapper::PERMISSION_WRITE )) {
200+ $ members [] = $ proxy ->getProxyId ();
201+ }
202+ }
176203 }
177204
178- return [ $ principal [ ' uri ' ]] ;
205+ return $ members ;
179206 }
180207
181208 /**
@@ -189,34 +216,36 @@ public function getGroupMemberSet($principal) {
189216 public function getGroupMembership ($ principal , $ needGroups = false ) {
190217 list ($ prefix , $ name ) = \Sabre \Uri \split ($ principal );
191218
192- if ($ prefix === $ this ->principalPrefix ) {
193- $ user = $ this ->userManager ->get ($ name );
194- if (!$ user ) {
195- throw new Exception ('Principal not found ' );
196- }
219+ if ($ prefix !== $ this ->principalPrefix ) {
220+ return [];
221+ }
197222
198- if ($ this ->hasGroups || $ needGroups ) {
199- $ groups = $ this ->groupManager ->getUserGroups ($ user );
200- $ groups = array_map (function ($ group ) {
201- /** @var IGroup $group */
202- return 'principals/groups/ ' . urlencode ($ group ->getGID ());
203- }, $ groups );
204-
205- $ proxies = $ this ->proxyMapper ->getProxiesFor ($ user ->getUID ());
206- foreach ($ proxies as $ proxy ) {
207- if ($ proxy ->getPermissions () & ProxyMapper::PERMISSION_READ ) {
208- $ groups [] = 'principals/users/ ' . $ proxy ->getOwnerId () . '/calendar-proxy-read ' ;
209- }
223+ $ user = $ this ->userManager ->get ($ name );
224+ if (!$ user ) {
225+ throw new Exception ('Principal not found ' );
226+ }
210227
211- if ($ proxy ->getPermissions () & ProxyMapper::PERMISSION_WRITE ) {
212- $ groups [] = 'principals/users/ ' . $ proxy ->getOwnerId () . '/calendar-proxy-write ' ;
213- }
214- }
228+ $ groups = [];
215229
216- return $ groups ;
230+ if ($ this ->hasGroups || $ needGroups ) {
231+ $ userGroups = $ this ->groupManager ->getUserGroups ($ user );
232+ foreach ($ userGroups as $ userGroup ) {
233+ $ groups [] = 'principals/groups/ ' . urlencode ($ userGroup ->getGID ());
217234 }
218235 }
219- return [];
236+
237+ $ proxies = $ this ->proxyMapper ->getProxiesFor ($ principal );
238+ foreach ($ proxies as $ proxy ) {
239+ if ($ proxy ->getPermissions () === ProxyMapper::PERMISSION_READ ) {
240+ $ groups [] = $ proxy ->getOwnerId () . '/calendar-proxy-read ' ;
241+ }
242+
243+ if ($ proxy ->getPermissions () === (ProxyMapper::PERMISSION_READ | ProxyMapper::PERMISSION_WRITE )) {
244+ $ groups [] = $ proxy ->getOwnerId () . '/calendar-proxy-write ' ;
245+ }
246+ }
247+
248+ return $ groups ;
220249 }
221250
222251 /**
@@ -229,7 +258,7 @@ public function getGroupMembership($principal, $needGroups = false) {
229258 * @throws Exception
230259 */
231260 public function setGroupMemberSet ($ principal , array $ members ) {
232- list ($ prefix , $ target ) = \Sabre \Uri \split ($ principal );
261+ list ($ principalUri , $ target ) = \Sabre \Uri \split ($ principal );
233262
234263 if ($ target !== 'calendar-proxy-write ' && $ target !== 'calendar-proxy-read ' ) {
235264 throw new Exception ('Setting members of the group is not supported yet ' );
@@ -240,8 +269,8 @@ public function setGroupMemberSet($principal, array $members) {
240269 $ permission |= ProxyMapper::PERMISSION_WRITE ;
241270 }
242271
243- list ($ prefix , $ owner ) = \Sabre \Uri \split ($ prefix );
244- $ proxies = $ this ->proxyMapper ->getProxiesOf ($ owner );
272+ list ($ prefix , $ owner ) = \Sabre \Uri \split ($ principalUri );
273+ $ proxies = $ this ->proxyMapper ->getProxiesOf ($ principalUri );
245274
246275 foreach ($ members as $ member ) {
247276 list ($ prefix , $ name ) = \Sabre \Uri \split ($ member );
@@ -257,7 +286,7 @@ public function setGroupMemberSet($principal, array $members) {
257286
258287 $ found = false ;
259288 foreach ($ proxies as $ proxy ) {
260- if ($ proxy ->getProxyId () === $ user -> getUID () ) {
289+ if ($ proxy ->getProxyId () === $ member ) {
261290 $ found = true ;
262291 $ proxy ->setPermissions ($ proxy ->getPermissions () | $ permission );
263292 $ this ->proxyMapper ->update ($ proxy );
@@ -271,16 +300,20 @@ public function setGroupMemberSet($principal, array $members) {
271300
272301 if ($ found === false ) {
273302 $ proxy = new Proxy ();
274- $ proxy ->setOwnerId ($ owner );
275- $ proxy ->setProxyId ($ user -> getUID () );
303+ $ proxy ->setOwnerId ($ principalUri );
304+ $ proxy ->setProxyId ($ member );
276305 $ proxy ->setPermissions ($ permission );
277306 $ this ->proxyMapper ->insert ($ proxy );
278307 }
279308 }
280309
281310 // Delete all remaining proxies
282311 foreach ($ proxies as $ proxy ) {
283- $ this ->proxyMapper ->delete ($ proxy );
312+ // Write and Read Proxies have individual requests,
313+ // so only delete proxies of this permission
314+ if ($ proxy ->getPermissions () === $ permission ) {
315+ $ this ->proxyMapper ->delete ($ proxy );
316+ }
284317 }
285318 }
286319
@@ -553,4 +586,50 @@ public function getCircleMembership($principal):array {
553586 return [];
554587 }
555588
589+ /**
590+ * @param string $principalUri
591+ * @return bool
592+ */
593+ private function isProxyPrincipal (string $ principalUri ):bool {
594+ list ($ realPrincipalUri , $ proxy ) = \Sabre \Uri \split ($ principalUri );
595+ list ($ prefix , $ userId ) = \Sabre \Uri \split ($ realPrincipalUri );
596+
597+ if (!isset ($ prefix ) || !isset ($ userId )) {
598+ return false ;
599+ }
600+ if ($ prefix !== $ this ->principalPrefix ) {
601+ return false ;
602+ }
603+
604+ return $ proxy === 'calendar-proxy-read '
605+ || $ proxy === 'calendar-proxy-write ' ;
606+
607+ }
608+
609+ /**
610+ * @param string $principalUri
611+ * @return bool
612+ */
613+ private function isReadProxyPrincipal (string $ principalUri ):bool {
614+ list (, $ proxy ) = \Sabre \Uri \split ($ principalUri );
615+ return $ proxy === 'calendar-proxy-read ' ;
616+ }
617+
618+ /**
619+ * @param string $principalUri
620+ * @return bool
621+ */
622+ private function isWriteProxyPrincipal (string $ principalUri ):bool {
623+ list (, $ proxy ) = \Sabre \Uri \split ($ principalUri );
624+ return $ proxy === 'calendar-proxy-write ' ;
625+ }
626+
627+ /**
628+ * @param string $principalUri
629+ * @return string
630+ */
631+ private function getPrincipalUriFromProxyPrincipal (string $ principalUri ):string {
632+ list ($ realPrincipalUri , ) = \Sabre \Uri \split ($ principalUri );
633+ return $ realPrincipalUri ;
634+ }
556635}
0 commit comments