1010
1111use OC \Files \Cache \CacheEntry ;
1212use OC \Files \Storage \FailedStorage ;
13- use OC \User \LazyUser ;
1413use OCA \Files_External \Config \ConfigAdapter ;
1514use OCA \Files_External \Event \StorageCreatedEvent ;
1615use OCA \Files_External \Event \StorageDeletedEvent ;
1716use OCA \Files_External \Event \StorageUpdatedEvent ;
17+ use OCA \Files_External \Lib \ApplicableHelper ;
1818use OCA \Files_External \Lib \StorageConfig ;
1919use OCP \Cache \CappedMemoryCache ;
2020use OCP \EventDispatcher \Event ;
2525use OCP \Group \Events \UserAddedEvent ;
2626use OCP \Group \Events \UserRemovedEvent ;
2727use OCP \IGroup ;
28- use OCP \IGroupManager ;
2928use OCP \IUser ;
30- use OCP \IUserManager ;
3129use OCP \User \Events \PostLoginEvent ;
3230use OCP \User \Events \UserCreatedEvent ;
3331
@@ -42,9 +40,8 @@ class MountCacheService implements IEventListener {
4240 public function __construct (
4341 private readonly IUserMountCache $ userMountCache ,
4442 private readonly ConfigAdapter $ configAdapter ,
45- private readonly IUserManager $ userManager ,
46- private readonly IGroupManager $ groupManager ,
4743 private readonly GlobalStoragesService $ storagesService ,
44+ private readonly ApplicableHelper $ applicableHelper ,
4845 ) {
4946 $ this ->storageRootCache = new CappedMemoryCache ();
5047 }
@@ -76,63 +73,24 @@ public function handle(Event $event): void {
7673 }
7774 }
7875
79-
80- /**
81- * Get all users that have access to a storage, either directly or through a group
82- *
83- * @param StorageConfig $storage
84- * @return \Iterator<string, IUser>
85- */
86- private function getUsersForStorage (StorageConfig $ storage ): \Iterator {
87- $ yielded = [];
88- if (count ($ storage ->getApplicableUsers ()) + count ($ storage ->getApplicableGroups ()) === 0 ) {
89- yield from $ this ->userManager ->getSeenUsers ();
90- }
91- foreach ($ storage ->getApplicableUsers () as $ userId ) {
92- $ yielded [$ userId ] = true ;
93- yield $ userId => new LazyUser ($ userId , $ this ->userManager );
94- }
95- foreach ($ storage ->getApplicableGroups () as $ groupId ) {
96- $ group = $ this ->groupManager ->get ($ groupId );
97- if ($ group !== null ) {
98- foreach ($ group ->searchUsers ('' ) as $ user ) {
99- if (!isset ($ yielded [$ user ->getUID ()])) {
100- $ yielded [$ user ->getUID ()] = true ;
101- yield $ user ->getUID () => $ user ;
102- }
103- }
104- }
105- }
106- }
107-
10876 public function handleDeletedStorage (StorageConfig $ storage ): void {
109- foreach ($ this ->getUsersForStorage ($ storage ) as $ user ) {
77+ foreach ($ this ->applicableHelper -> getUsersForStorage ($ storage ) as $ user ) {
11078 $ this ->userMountCache ->removeMount ($ storage ->getMountPointForUser ($ user ));
11179 }
11280 }
11381
11482 public function handleAddedStorage (StorageConfig $ storage ): void {
115- foreach ($ this ->getUsersForStorage ($ storage ) as $ user ) {
83+ foreach ($ this ->applicableHelper -> getUsersForStorage ($ storage ) as $ user ) {
11684 $ this ->registerForUser ($ user , $ storage );
11785 }
11886 }
11987
12088 public function handleUpdatedStorage (StorageConfig $ oldStorage , StorageConfig $ newStorage ): void {
121- /** @var array<string, IUser> $oldApplicable */
122- $ oldApplicable = iterator_to_array ($ this ->getUsersForStorage ($ oldStorage ));
123- /** @var array<string, IUser> $newApplicable */
124- $ newApplicable = iterator_to_array ($ this ->getUsersForStorage ($ newStorage ));
125-
126- foreach ($ oldApplicable as $ oldUser ) {
127- if (!isset ($ newApplicable [$ oldUser ->getUID ()])) {
128- $ this ->userMountCache ->removeMount ($ oldStorage ->getMountPointForUser ($ oldUser ));
129- }
89+ foreach ($ this ->applicableHelper ->diffApplicable ($ oldStorage , $ newStorage ) as $ user ) {
90+ $ this ->userMountCache ->removeMount ($ oldStorage ->getMountPointForUser ($ user ));
13091 }
131-
132- foreach ($ newApplicable as $ newUser ) {
133- if (!isset ($ oldApplicable [$ newUser ->getUID ()])) {
134- $ this ->registerForUser ($ newUser , $ newStorage );
135- }
92+ foreach ($ this ->applicableHelper ->diffApplicable ($ newStorage , $ oldStorage ) as $ user ) {
93+ $ this ->registerForUser ($ user , $ newStorage );
13694 }
13795 }
13896
@@ -194,26 +152,10 @@ private function registerForUser(IUser $user, StorageConfig $storage): void {
194152 );
195153 }
196154
197- private function isApplicableForUser (StorageConfig $ storage , IUser $ user ): bool {
198- if (count ($ storage ->getApplicableUsers ()) + count ($ storage ->getApplicableGroups ()) === 0 ) {
199- return true ;
200- }
201- if (in_array ($ user ->getUID (), $ storage ->getApplicableUsers ())) {
202- return true ;
203- }
204- $ groupIds = $ this ->groupManager ->getUserGroupIds ($ user );
205- foreach ($ groupIds as $ groupId ) {
206- if (in_array ($ groupId , $ storage ->getApplicableGroups ())) {
207- return true ;
208- }
209- }
210- return false ;
211- }
212-
213155 private function handleUserRemoved (IGroup $ group , IUser $ user ): void {
214156 $ storages = $ this ->storagesService ->getAllStoragesForGroup ($ group );
215157 foreach ($ storages as $ storage ) {
216- if (!$ this ->isApplicableForUser ($ storage , $ user )) {
158+ if (!$ this ->applicableHelper -> isApplicableForUser ($ storage , $ user )) {
217159 $ this ->userMountCache ->removeMount ($ storage ->getMountPointForUser ($ user ));
218160 }
219161 }
@@ -229,10 +171,17 @@ private function handleUserAdded(IGroup $group, IUser $user): void {
229171 private function handleGroupDeleted (IGroup $ group ): void {
230172 $ storages = $ this ->storagesService ->getAllStoragesForGroup ($ group );
231173 foreach ($ storages as $ storage ) {
232- foreach ($ group ->searchUsers ('' ) as $ user ) {
233- if (!$ this ->isApplicableForUser ($ storage , $ user )) {
234- $ this ->userMountCache ->removeMount ($ storage ->getMountPointForUser ($ user ));
235- }
174+ $ this ->removeGroupFromStorage ($ storage , $ group );
175+ }
176+ }
177+
178+ /**
179+ * Remove mounts from users in a group, if they don't have access to the storage trough other means
180+ */
181+ private function removeGroupFromStorage (StorageConfig $ storage , IGroup $ group ): void {
182+ foreach ($ group ->searchUsers ('' ) as $ user ) {
183+ if (!$ this ->applicableHelper ->isApplicableForUser ($ storage , $ user )) {
184+ $ this ->userMountCache ->removeMount ($ storage ->getMountPointForUser ($ user ));
236185 }
237186 }
238187 }
0 commit comments