Skip to content

Commit 413be26

Browse files
committed
Remove deleted groups from app restrictions fixes #15823
Signed-off-by: Greta Doci <[email protected]>
1 parent dc9e73a commit 413be26

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

lib/base.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ public static function init() {
717717
self::registerEncryptionHooks();
718718
self::registerAccountHooks();
719719
self::registerResourceCollectionHooks();
720+
self::registerAppRestrictionsHooks();
720721

721722
// Make sure that the application class is not loaded before the database is setup
722723
if ($systemConfig->getValue("installed", false)) {
@@ -848,6 +849,30 @@ private static function registerAccountHooks() {
848849
\OCP\Util::connectHook('OC_User', 'changeUser', $hookHandler, 'changeUserHook');
849850
}
850851

852+
private static function registerAppRestrictionsHooks() {
853+
$groupManager = self::$server->query(\OCP\IGroupManager::class);
854+
$groupManager->listen ('\OC\Group', 'postDelete', function (\OCP\IGroup $group) {
855+
$appManager = self::$server->getAppManager();
856+
$apps = $appManager->getEnabledAppsForGroups($group);
857+
foreach ($apps as $appId) {
858+
$restrictions = $appManager->getAppRestriction($appId);
859+
if ($restrictions === null) {
860+
continue;
861+
}
862+
$key = array_search($group->getGID(), $restrictions);
863+
unset($restrictions[$key]);
864+
$restrictions = array_values($restrictions);
865+
if (empty($restrictions)) {
866+
$appManager->disableApp($appId);
867+
}
868+
else{
869+
$appManager->enableAppForGroups($appId, $restrictions);
870+
}
871+
872+
}
873+
});
874+
}
875+
851876
private static function registerResourceCollectionHooks() {
852877
\OC\Collaboration\Resources\Listener::register(\OC::$server->getEventDispatcher());
853878
}

lib/private/App/AppManager.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use OCP\App\IAppManager;
3838
use OCP\App\ManagerEvent;
3939
use OCP\ICacheFactory;
40+
use OCP\IGroup;
4041
use OCP\IGroupManager;
4142
use OCP\IUser;
4243
use OCP\IUserSession;
@@ -148,6 +149,27 @@ public function getEnabledAppsForUser(IUser $user) {
148149
return array_keys($appsForUser);
149150
}
150151

152+
/**
153+
* @param \OCP\IGroup $group
154+
* @return array
155+
*/
156+
public function getEnabledAppsForGroups(IGroup $group): array {
157+
$apps = $this->getInstalledAppsValues();
158+
$appsForGroups = array_filter($apps, function ($enabled) use ($group) {
159+
return $this->checkAppForGroups($enabled, $group);
160+
});
161+
return array_keys($appsForGroups);
162+
}
163+
public function getAppRestriction(string $appId): array {
164+
$values = $this->getInstalledAppsValues();
165+
166+
if ($values[$appname] === 'yes' || $values[$appname] === 'no') {
167+
return [];
168+
}
169+
return json_decode($values[$appname]);
170+
}
171+
172+
151173
/**
152174
* Check if an app is enabled for user
153175
*
@@ -203,6 +225,33 @@ private function checkAppForUser($enabled, $user) {
203225
}
204226
}
205227

228+
/**
229+
* @param string $enabled
230+
* @param IGroup $group
231+
* @return bool
232+
*/
233+
private function checkAppForGroups(string $enabled, IGroup $group): bool {
234+
if ($enabled === 'yes') {
235+
return true;
236+
} elseif ($group === null) {
237+
return false;
238+
} else {
239+
if (empty($enabled)) {
240+
return false;
241+
}
242+
243+
$groupIds = json_decode($enabled);
244+
245+
if (!is_array($groupIds)) {
246+
$jsonError = json_last_error();
247+
\OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: ' . print_r($enabled, true) . ' - json error code: ' . $jsonError, ['app' => 'lib']);
248+
return false;
249+
}
250+
251+
return in_array($group->getGID(), $groupIds);
252+
}
253+
}
254+
206255
/**
207256
* Check if an app is enabled in the instance
208257
*
@@ -268,16 +317,22 @@ public function enableAppForGroups($appId, $groups) {
268317

269318
$groupIds = array_map(function ($group) {
270319
/** @var \OCP\IGroup $group */
271-
return $group->getGID();
320+
return ($group instanceof IGroup)
321+
? $group->getGID()
322+
: $group;
272323
}, $groups);
324+
273325
$this->installedAppsCache[$appId] = json_encode($groupIds);
274326
$this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
275327
$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
276328
ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
277329
));
278330
$this->clearAppsCache();
331+
332+
279333
}
280334

335+
281336
/**
282337
* Disable an app for every user
283338
*

lib/public/App/IAppManager.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,18 @@ public function isShipped($appId);
158158
* @since 9.0.0
159159
*/
160160
public function getAlwaysEnabledApps();
161+
162+
/**
163+
* @param \OCP\IGroup $group
164+
* @return String[]
165+
* @since 17.0.0
166+
*/
167+
public function getEnabledAppsForGroups(IGroup $group): string;
168+
169+
/**
170+
* @param String $appId
171+
* @return string[]
172+
* @since 17.0.0
173+
*/
174+
public function getAppRestriction(string $appId): array;
161175
}

0 commit comments

Comments
 (0)