1818use OCA \Tables \Db \ViewMapper ;
1919use OCA \Tables \Errors \InternalError ;
2020use OCA \Tables \Errors \NotFoundError ;
21+ use OCA \Tables \Helper \CircleHelper ;
2122use OCA \Tables \Helper \ConversionHelper ;
2223use OCA \Tables \Helper \UserHelper ;
2324use OCA \Tables \Model \Permissions ;
2425use OCP \AppFramework \Db \DoesNotExistException ;
2526use OCP \AppFramework \Db \MultipleObjectsReturnedException ;
2627use OCP \DB \Exception ;
2728use Psr \Log \LoggerInterface ;
29+ use Throwable ;
2830
31+ /**
32+ * @psalm-suppress UndefinedDocblockClass
33+ */
2934class PermissionsService {
3035 private TableMapper $ tableMapper ;
3136
@@ -35,11 +40,14 @@ class PermissionsService {
3540
3641 private UserHelper $ userHelper ;
3742
43+ private CircleHelper $ circleHelper ;
44+
3845 protected LoggerInterface $ logger ;
3946
4047 protected ?string $ userId = null ;
4148
4249 protected bool $ isCli = false ;
50+
4351 private ContextMapper $ contextMapper ;
4452
4553 public function __construct (
@@ -50,6 +58,7 @@ public function __construct(
5058 ShareMapper $ shareMapper ,
5159 ContextMapper $ contextMapper ,
5260 UserHelper $ userHelper ,
61+ CircleHelper $ circleHelper ,
5362 bool $ isCLI
5463 ) {
5564 $ this ->tableMapper = $ tableMapper ;
@@ -60,6 +69,7 @@ public function __construct(
6069 $ this ->userId = $ userId ;
6170 $ this ->isCli = $ isCLI ;
6271 $ this ->contextMapper = $ contextMapper ;
72+ $ this ->circleHelper = $ circleHelper ;
6373 }
6474
6575
@@ -420,6 +430,7 @@ public function canReadShare(Share $share, ?string $userId = null): bool {
420430 * @param int $elementId
421431 * @param 'table'|'view' $elementType
422432 * @param string $userId
433+ * @return Permissions
423434 * @throws NotFoundError
424435 */
425436 public function getSharedPermissionsIfSharedWithMe (int $ elementId , string $ elementType , string $ userId ): Permissions {
@@ -436,16 +447,40 @@ public function getSharedPermissionsIfSharedWithMe(int $elementId, string $eleme
436447 $ this ->logger ->warning ('Exception occurred: ' .$ e ->getMessage ().' Permission denied. ' );
437448 return new Permissions ();
438449 }
439- $ additionalShares = [];
450+ $ groupShares = [];
440451 foreach ($ userGroups as $ userGroup ) {
441452 try {
442- $ additionalShares [] = $ this ->shareMapper ->findAllSharesForNodeFor ($ elementType , $ elementId , $ userGroup ->getGid (), 'group ' );
453+ $ groupShares [] = $ this ->shareMapper ->findAllSharesForNodeFor ($ elementType , $ elementId , $ userGroup ->getGid (), 'group ' );
443454 } catch (Exception $ e ) {
444455 $ this ->logger ->warning ('Exception occurred: ' .$ e ->getMessage ().' Permission denied. ' );
445456 return new Permissions ();
446457 }
447458 }
448- $ shares = array_merge ($ shares , ...$ additionalShares );
459+
460+ $ shares = array_merge ($ shares , ...$ groupShares );
461+
462+ if ($ this ->circleHelper ->isCirclesEnabled ()) {
463+ $ circleShares = [];
464+
465+ try {
466+ $ userCircles = $ this ->circleHelper ->getUserCircles ($ userId );
467+ } catch (Throwable $ e ) {
468+ $ this ->logger ->warning ('Exception occurred: ' . $ e ->getMessage () . ' Permission denied. ' );
469+ return new Permissions ();
470+ }
471+
472+ foreach ($ userCircles as $ userCircle ) {
473+ try {
474+ $ circleShares [] = $ this ->shareMapper ->findAllSharesForNodeFor ($ elementType , $ elementId , $ userCircle ->getSingleId (), 'circle ' );
475+ } catch (Exception $ e ) {
476+ $ this ->logger ->warning ('Exception occurred: ' . $ e ->getMessage () . ' Permission denied. ' );
477+ return new Permissions ();
478+ }
479+ }
480+
481+ $ shares = array_merge ($ shares , ...$ circleShares );
482+ }
483+
449484 if (count ($ shares ) > 0 ) {
450485 $ read = array_reduce ($ shares , function ($ carry , $ share ) {
451486 return $ carry || ($ share ->getPermissionRead ());
@@ -520,7 +555,7 @@ private function hasPermission(int $existingPermissions, string $permissionName)
520555 $ constantName = 'PERMISSION_ ' . strtoupper ($ permissionName );
521556 try {
522557 $ permissionBit = constant (Application::class . ":: $ constantName " );
523- } catch (\ Throwable $ t ) {
558+ } catch (Throwable $ t ) {
524559 $ this ->logger ->error ('Unexpected permission string {permission} ' , [
525560 'app ' => Application::APP_ID ,
526561 'permission ' => $ permissionName ,
0 commit comments