Skip to content

Commit

Permalink
Fix php code styles
Browse files Browse the repository at this point in the history
Signed-off-by: Giuliano Mele <[email protected]>
  • Loading branch information
melegiul committed Jul 14, 2022
1 parent 30649e7 commit 4a32175
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 168 deletions.
22 changes: 11 additions & 11 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,30 @@
return;
}

\OC::$server->registerService(GroupDuplicateChecker::class, function(ContainerInterface $c) use($config) {
return new GroupDuplicateChecker(
$config,
$c->get(IGroupManager::class),
$c->get(LoggerInterface::class)
);
\OC::$server->registerService(GroupDuplicateChecker::class, function (ContainerInterface $c) use ($config) {
return new GroupDuplicateChecker(
$config,
$c->get(IGroupManager::class),
$c->get(LoggerInterface::class)
);
});

$groupBackend = new \OCA\User_SAML\GroupBackend(\OC::$server->getDatabaseConnection());
\OC::$server->get(IGroupManager::class)->addBackend($groupBackend);

$samlSettings = \OC::$server->get(\OCA\User_SAML\SAMLSettings::class);

\OC::$server->registerService(GroupManager::class, function(ContainerInterface $c) use($groupBackend, $samlSettings) {
return new GroupManager(
$c->get(IDBConnection::class),
$c->get(SAMLGroupDuplicateChecker::class),
\OC::$server->registerService(GroupManager::class, function (ContainerInterface $c) use ($groupBackend, $samlSettings) {
return new GroupManager(
$c->get(IDBConnection::class),
$c->get(SAMLGroupDuplicateChecker::class),
$c->get(IGroupManager::class),
$c->get(IUserManager::class),
$groupBackend,
$c->get(IConfig::class),
$c->get(IJobList::class),
$samlSettings,
);
);
});

$userData = new \OCA\User_SAML\UserData(
Expand Down
8 changes: 4 additions & 4 deletions lib/GroupBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class GroupBackend extends ABackend implements IAddToGroupBackend, ICountUsersBa
/** @var array */
private $groupCache = [];

const TABLE_GROUPS = 'user_saml_groups';
const TABLE_MEMBERS = 'user_saml_group_members';
public const TABLE_GROUPS = 'user_saml_groups';
public const TABLE_MEMBERS = 'user_saml_group_members';

public function __construct(IDBConnection $dbc) {
$this->dbc = $dbc;
Expand Down Expand Up @@ -75,7 +75,7 @@ public function getUserGroups($uid) {
->execute();

$groups = [];
while( $row = $cursor->fetch()) {
while ($row = $cursor->fetch()) {
$groups[] = $row['gid'];
$this->groupCache[$row['gid']] = $row['gid'];
}
Expand Down Expand Up @@ -195,7 +195,7 @@ public function createGroup(string $gid, string $samlGid = null): bool {
->setValue('displayname', $builder->createNamedParameter($displayName))
->setValue('saml_gid', $builder->createNamedParameter($samlGid))
->execute();
} catch(UniqueConstraintViolationException $e) {
} catch (UniqueConstraintViolationException $e) {
$result = 0;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/GroupDuplicateChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
use OCP\IGroupManager;
use Psr\Log\LoggerInterface;

class GroupDuplicateChecker
{
class GroupDuplicateChecker {
/**
* @var IConfig
*/
Expand Down
47 changes: 22 additions & 25 deletions lib/GroupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,16 @@

use OC\BackgroundJob\JobList;
use OC\Hooks\PublicEmitter;
use OCA\User_SAML\GroupBackend;
use OCA\User_SAML\Jobs\MigrateGroups;
use OCA\User_SAML\SAMLSettings;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;

class GroupManager
{
const LOCAL_GROUPS_CHECK_FOR_MIGRATION = 'localGroupsCheckForMigration';
class GroupManager {
public const LOCAL_GROUPS_CHECK_FOR_MIGRATION = 'localGroupsCheckForMigration';

/**
* @var IDBConnection $db
Expand Down Expand Up @@ -89,7 +86,7 @@ public function __construct(

private function getGroupsToRemove(array $samlGroups, array $assignedGroups): array {
$groupsToRemove = [];
foreach($assignedGroups as $group) {
foreach ($assignedGroups as $group) {
// if group is not supplied by SAML and group has SAML backend
if (!in_array($group->getGID(), $samlGroups) && $this->hasSamlBackend($group)) {
$groupsToRemove[] = $group->getGID();
Expand All @@ -100,7 +97,7 @@ private function getGroupsToRemove(array $samlGroups, array $assignedGroups): ar

private function getGroupsToAdd(array $samlGroups, array $assignedGroupIds): array {
$groupsToAdd = [];
foreach($samlGroups as $group) {
foreach ($samlGroups as $group) {
// if user is not assigend to the group or the provided group has a non SAML backend
if (!in_array($group, $assignedGroupIds) || !$this->hasSamlBackend($this->groupManager->get($group))) {
$groupsToAdd[] = $group;
Expand All @@ -111,12 +108,12 @@ private function getGroupsToAdd(array $samlGroups, array $assignedGroupIds): arr

public function replaceGroups(string $uid, array $samlGroups): void {
$user = $this->userManager->get($uid);
if($user === null) {
if ($user === null) {
return;
}
$this->translateGroupToIds($samlGroups);
$assignedGroups = $this->groupManager->getUserGroups($user);
$assignedGroupIds = array_map(function(IGroup $group){
$assignedGroupIds = array_map(function (IGroup $group) {
return $group->getGID();
}, $assignedGroups);
$groupsToRemove = $this->getGroupsToRemove($samlGroups, $assignedGroups);
Expand All @@ -126,9 +123,9 @@ public function replaceGroups(string $uid, array $samlGroups): void {
}

protected function translateGroupToIds(array &$samlGroups): void {
array_walk($samlGroups, function (&$gid){
array_walk($samlGroups, function (&$gid) {
$altGid = $this->ownGroupBackend->groupExistsWithDifferentGid($gid);
if($altGid !== null) {
if ($altGid !== null) {
$gid = $altGid;
}
});
Expand All @@ -142,7 +139,7 @@ public function removeGroups(IUser $user, array $groupIds): void {

public function removeGroup(IUser $user, string $gid): void {
$group = $this->groupManager->get($gid);
if($group === null) {
if ($group === null) {
return;
}
$this->ownGroupBackend->removeFromGroup($user->getUID(), $group->getGID());
Expand All @@ -161,9 +158,9 @@ public function addGroup(IUser $user, string $gid): void {
try {
$group = $this->findGroup($gid);
} catch (\RuntimeException $e) {
if($e->getCode() === 1) {
if ($e->getCode() === 1) {
$group = $this->createGroupInBackend($gid);
} else if($e->getCode() === 2) {
} elseif ($e->getCode() === 2) {
//FIXME: probably need config flag. Previous to 17, gid was used as displayname
$providerId = $this->settings->getProviderId();
$settings = $this->settings->get($providerId);
Expand All @@ -179,15 +176,15 @@ public function addGroup(IUser $user, string $gid): void {
}

protected function createGroupInBackend(string $gid, ?string $originalGid = null): ?IGroup {
if($this->groupManager instanceof PublicEmitter) {
if ($this->groupManager instanceof PublicEmitter) {
$this->groupManager->emit('\OC\Group', 'preCreate', array($gid));
}
if(!$this->ownGroupBackend->createGroup($gid, $originalGid ?? $gid)) {
if (!$this->ownGroupBackend->createGroup($gid, $originalGid ?? $gid)) {
return null;
}

$group = $this->groupManager->get($gid);
if($this->groupManager instanceof PublicEmitter) {
if ($this->groupManager instanceof PublicEmitter) {
$this->groupManager->emit('\OC\Group', 'postCreate', array($group));
}

Expand All @@ -204,25 +201,25 @@ protected function findGroup(string $gid): IGroup {
if ($migrationWhiteList !== null) {
$migrationWhiteList = \json_decode($migrationWhiteList, true);
}
if(!$strictBackendCheck && in_array($gid, $migrationWhiteList['groups'], true)) {
if (!$strictBackendCheck && in_array($gid, $migrationWhiteList['groups'], true)) {
$group = $this->groupManager->get($gid);
if($group === null) {
if ($group === null) {
//FIXME: specific Exception and/or constant error code
throw new \RuntimeException('Group not found', 1);
}
return $group;
}
$group = $this->groupManager->get($gid);
if($group === null) {
if ($group === null) {
//FIXME: specific Exception and/or constant error code
throw new \RuntimeException('Group not found', 1);
}
if($this->hasSamlBackend($group)) {
if ($this->hasSamlBackend($group)) {
return $group;
}

$altGid = $this->ownGroupBackend->groupExistsWithDifferentGid($gid);
if($altGid) {
if ($altGid) {
return $this->groupManager->get($altGid);
}

Expand All @@ -238,7 +235,7 @@ protected function hasSamlBackend(IGroup $group): bool {
// available at nextcloud 22
// $backends = $group->getBackendNames();
foreach ($backends as $backend) {
if($backend instanceof GroupBackend) {
if ($backend instanceof GroupBackend) {
return true;
}
}
Expand All @@ -247,11 +244,11 @@ protected function hasSamlBackend(IGroup $group): bool {

public function evaluateGroupMigrations(array $groups): void {
$candidateInfo = $this->config->getAppValue('user_saml', self::LOCAL_GROUPS_CHECK_FOR_MIGRATION, null);
if($candidateInfo === null) {
if ($candidateInfo === null) {
return;
}
$candidateInfo = \json_decode($candidateInfo, true);
if(!isset($candidateInfo['dropAfter']) || $candidateInfo['dropAfter'] < time()) {
if (!isset($candidateInfo['dropAfter']) || $candidateInfo['dropAfter'] < time()) {
$this->config->deleteAppValue('user_saml', self::LOCAL_GROUPS_CHECK_FOR_MIGRATION);
return;
}
Expand Down
21 changes: 10 additions & 11 deletions lib/Jobs/MigrateGroups.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
Expand Down Expand Up @@ -79,11 +80,11 @@ protected function run($argument) {

protected function updateCandidatePool($migrateGroups) {
$candidateInfo = $this->config->getAppValue('user_saml', GroupManager::LOCAL_GROUPS_CHECK_FOR_MIGRATION, null);
if($candidateInfo === null) {
if ($candidateInfo === null) {
return;
}
$candidateInfo = \json_decode($candidateInfo, true);
if(!isset($candidateInfo['dropAfter']) || !isset($candidateInfo['groups'])) {
if (!isset($candidateInfo['dropAfter']) || !isset($candidateInfo['groups'])) {
return;
}
$candidateInfo['groups'] = array_diff($candidateInfo['groups'], $migrateGroups);
Expand All @@ -108,11 +109,11 @@ protected function migrateGroup(string $gid): bool {
$affected = $qb->delete('groups')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
->execute();
if($affected === 0) {
if ($affected === 0) {
throw new \RuntimeException('Could not delete group from local backend');
}

if(!$this->ownGroupBackend->createGroup($gid)) {
if (!$this->ownGroupBackend->createGroup($gid)) {
throw new \RuntimeException('Could not create group in SAML backend');
}

Expand All @@ -129,20 +130,20 @@ protected function migrateGroup(string $gid): bool {

protected function getGroupsToMigrate(array $samlGroups, array $pool): array {
return array_filter($samlGroups, function (string $gid) use ($pool) {
if(!in_array($gid, $pool)) {
if (!in_array($gid, $pool)) {
return false;
}

$group = $this->groupManager->get($gid);
if($group === null) {
if ($group === null) {
return false;
}
$reflected = new \ReflectionClass($group);
$backendsProperty = $reflected->getProperty('backends');
$backendsProperty->setAccessible(true);
$backends = $backendsProperty->getValue($group);
foreach ($backends as $backend) {
if($backend instanceof Database) {
if ($backend instanceof Database) {
return true;
}
}
Expand All @@ -152,17 +153,15 @@ protected function getGroupsToMigrate(array $samlGroups, array $pool): array {

protected function getMigratableGroups(): array {
$candidateInfo = $this->config->getAppValue('user_saml', GroupManager::LOCAL_GROUPS_CHECK_FOR_MIGRATION, null);
if($candidateInfo === null) {
if ($candidateInfo === null) {
throw new \RuntimeException('No migration of groups to SAML backend anymore');
}
$candidateInfo = \json_decode($candidateInfo, true);
if(!isset($candidateInfo['dropAfter']) || !isset($candidateInfo['groups']) || $candidateInfo['dropAfter'] < time()) {
if (!isset($candidateInfo['dropAfter']) || !isset($candidateInfo['groups']) || $candidateInfo['dropAfter'] < time()) {
$this->config->deleteAppValue('user_saml', GroupManager::LOCAL_GROUPS_CHECK_FOR_MIGRATION);
throw new \RuntimeException('Period for migration groups is over');
}

return $candidateInfo['groups'];
}


}
5 changes: 3 additions & 2 deletions lib/Migration/RememberLocalGroupsForPotentialMigrations.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
Expand Down Expand Up @@ -86,7 +87,7 @@ public function run(IOutput $output) {
protected function findGroupIds(Database $backend): array {
$groupIds = $backend->getGroups();
$adminGroupIndex = array_search('admin', $groupIds, true);
if($adminGroupIndex !== false) {
if ($adminGroupIndex !== false) {
unset($groupIds[$adminGroupIndex]);
}
return $groupIds;
Expand All @@ -95,7 +96,7 @@ protected function findGroupIds(Database $backend): array {
protected function findBackend(): Database {
$groupBackends = $this->groupManager->getBackends();
foreach ($groupBackends as $backend) {
if($backend instanceof Database) {
if ($backend instanceof Database) {
return $backend;
break;
}
Expand Down
1 change: 0 additions & 1 deletion lib/UserBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserManager;
use OCA\User_SAML\GroupManager;
use OCP\UserInterface;
use OCP\IUserBackend;
use OCP\IConfig;
Expand Down
Loading

0 comments on commit 4a32175

Please sign in to comment.