Skip to content

Commit

Permalink
Merge pull request #1229 from nextcloud/chore/migrate-ilogger-to-psr-3
Browse files Browse the repository at this point in the history
refactor: Migrate from deprecated `ILogger` interface to PSR-3 logger
  • Loading branch information
susnux authored Aug 21, 2024
2 parents 0b81293 + e00210d commit d0cde95
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 128 deletions.
37 changes: 10 additions & 27 deletions lib/AppWhitelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,20 @@

use OCP\App\IAppManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Template;
use Psr\Log\LoggerInterface;

/**
* Only allow whitelisted apps to be accessed by guests
*
* @package OCA\Guests
*/
class AppWhitelist {
/** @var Config */
private $config;
/** @var GuestManager */
private $guestManager;
/** @var IL10N */
private $l10n;
/** @var IAppManager */
private $appManager;
/** @var string */
private $baseUrl;
/** @var int */
private $baseUrlLength;
/** @var ILogger */
private $logger;
private string $baseUrl;
private int $baseUrlLength;

public const WHITELIST_ALWAYS = ',core,theming,settings,avatar,files,heartbeat,dav,guests,impersonate,accessibility,terms_of_service,dashboard,weather_status,user_status,apporder';

Expand All @@ -62,23 +50,18 @@ class AppWhitelist {
* @param IL10N $l10n
* @param IAppManager $appManager
* @param IURLGenerator $urlGenerator
* @param ILogger $logger
* @param LoggerInterface $logger
*/
public function __construct(
Config $config,
GuestManager $guestManager,
IL10N $l10n,
IAppManager $appManager,
IURLGenerator $urlGenerator,
ILogger $logger
private Config $config,
private GuestManager $guestManager,
private IL10N $l10n,
private IAppManager $appManager,
private LoggerInterface $logger,
) {
$this->config = $config;
$this->guestManager = $guestManager;
$this->l10n = $l10n;
$this->appManager = $appManager;
$this->baseUrl = $urlGenerator->getBaseUrl();
$this->baseUrlLength = strlen($this->baseUrl);
$this->logger = $logger;
}

public function isAppWhitelisted($appId): bool {
Expand All @@ -104,7 +87,7 @@ public function isUrlAllowed(IUser $user, $url): bool {
echo "[]";
exit;
}
$this->logger->info("Blocking access to non-whitelisted app ($app) for guest", ['app' => 'guests']);
$this->logger->notice("Blocking access to non-whitelisted app ($app) for guest", ['app' => 'guests']);
return false;
}
} else {
Expand Down
22 changes: 6 additions & 16 deletions lib/Listener/ShareAutoAcceptListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,22 @@
use OCA\Guests\UserBackend;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\ILogger;
use OCP\IUserManager;
use OCP\Share\Events\ShareCreatedEvent;
use OCP\Share\IManager as ShareManager;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;

/**
* @template-implements IEventListener<Event>
*/
class ShareAutoAcceptListener implements IEventListener {

/** @var IUserManager */
private $userManager;

/** @var ShareManager */
private $shareManager;

/** @var ILogger */
private $logger;

public function __construct(IUserManager $userManager,
ShareManager $shareManager,
ILogger $logger) {
$this->userManager = $userManager;
$this->shareManager = $shareManager;
$this->logger = $logger;
public function __construct(
private IUserManager $userManager,
private ShareManager $shareManager,
private LoggerInterface $logger,
) {
}

public function handle(Event $event): void {
Expand Down
49 changes: 9 additions & 40 deletions lib/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,58 +23,27 @@

use OCP\Defaults;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
use OCP\Share;
use OCP\Util;
use Psr\Log\LoggerInterface;

class Mail {

/** @var IConfig */
private $config;

/** @var ILogger */
private $logger;

/** @var IUserSession */
private $userSession;

/** @var IMailer */
private $mailer;

/** @var Defaults */
private $defaults;

/** @var IFactory */
private $l10nFactory;

private $userManager;

/** @var IURLGenerator */
private $urlGenerator;

public function __construct(
IConfig $config,
ILogger $logger,
IUserSession $userSession,
IMailer $mailer,
Defaults $defaults,
IFactory $l10nFactory,
IUserManager $userManager,
IURLGenerator $urlGenerator
private IConfig $config,
private LoggerInterface $logger,
private IUserSession $userSession,
private IMailer $mailer,
private Defaults $defaults,
private IFactory $l10nFactory,
private IUserManager $userManager,
private IURLGenerator $urlGenerator,
) {
$this->config = $config;
$this->logger = $logger;
$this->userSession = $userSession;
$this->mailer = $mailer;
$this->defaults = $defaults;
$this->l10nFactory = $l10nFactory;
$this->userManager = $userManager;
$this->urlGenerator = $urlGenerator;
}

/**
Expand Down
32 changes: 0 additions & 32 deletions tests/stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,38 +453,6 @@ public function getFileInfo(): \OCP\Files\FileInfo {
}
}

namespace OC\BackgroundJob {

use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\IJobList;
use OCP\ILogger;

abstract class TimedJob implements IJob {
public function execute(IJobList $jobList, ?ILogger $logger = null) {
}

abstract protected function run($argument);

public function setId(int $id) {
}

public function setLastRun(int $lastRun) {
}

public function setArgument($argument) {
}

public function getId() {
}

public function getLastRun() {
}

public function getArgument() {
}
}
}

namespace OC\Files\Mount {
use OC\Files\Storage\Storage;
use OCP\Files\Mount\IMountPoint;
Expand Down
21 changes: 8 additions & 13 deletions tests/unit/AppWhitelistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,18 @@
use OCA\Guests\GuestManager;
use OCP\App\IAppManager;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

class AppWhitelistTest extends TestCase {
/** @var Config|MockObject */
private $config;
/** @var GuestManager|MockObject */
private $guestManager;
/** @var IL10N|MockObject */
private $l10n;
/** @var IAppManager|MockObject */
private $appManager;
/** @var IURLGenerator|MockObject */
private $urlGenerator;
private Config&MockObject $config;
private GuestManager&MockObject $guestManager;
private IL10N&MockObject $l10n;
private IAppManager&MockObject $appManager;
private IURLGenerator&MockObject $urlGenerator;

/** @var AppWhitelist */
private $appWhitelist;
Expand All @@ -61,12 +56,12 @@ protected function setUp(): void {
->willReturn('');

$this->appWhitelist = new AppWhitelist(
$this->urlGenerator,
$this->config,
$this->guestManager,
$this->l10n,
$this->appManager,
$this->urlGenerator,
$this->createMock(ILogger::class)
$this->createMock(LoggerInterface::class)
);
}

Expand Down

0 comments on commit d0cde95

Please sign in to comment.