diff --git a/lib/Controller/AuthSettingsController.php b/lib/Controller/AuthSettingsController.php index 79f8960..c57138a 100644 --- a/lib/Controller/AuthSettingsController.php +++ b/lib/Controller/AuthSettingsController.php @@ -59,46 +59,19 @@ * @psalm-api */ class AuthSettingsController extends Controller { - private IProvider $tokenProvider; - private ISession $session; - private IUserSession $userSession; - private string $uid; - private ISecureRandom $random; - private IManager $activityManager; - private RemoteWipe $remoteWipe; - private LoggerInterface $logger; - - /** - * @param string $appName - * @param IRequest $request - * @param IProvider $tokenProvider - * @param ISession $session - * @param ISecureRandom $random - * @param string|null $userId - * @param IUserSession $userSession - * @param IManager $activityManager - * @param RemoteWipe $remoteWipe - * @param LoggerInterface $logger - */ - public function __construct(string $appName, + public function __construct( + string $appName, IRequest $request, - IProvider $tokenProvider, - ISession $session, - ISecureRandom $random, - ?string $userId, - IUserSession $userSession, - IManager $activityManager, - RemoteWipe $remoteWipe, - LoggerInterface $logger) { + private readonly IProvider $tokenProvider, + private readonly ISession $session, + private readonly ISecureRandom $random, + private readonly ?string $userId, + private readonly IUserSession $userSession, + private readonly IManager $activityManager, + private readonly RemoteWipe $remoteWipe, + private readonly LoggerInterface $logger, + ) { parent::__construct($appName, $request); - $this->tokenProvider = $tokenProvider; - $this->uid = $userId; - $this->userSession = $userSession; - $this->session = $session; - $this->random = $random; - $this->activityManager = $activityManager; - $this->remoteWipe = $remoteWipe; - $this->logger = $logger; } /** @@ -140,7 +113,7 @@ public function create($name) { } $token = $this->generateRandomDeviceToken(); - $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN); + $deviceToken = $this->tokenProvider->generateToken($token, $this->userId, $loginName, $password, $name, IToken::PERMANENT_TOKEN); $tokenData = $deviceToken->jsonSerialize(); $tokenData['canDelete'] = true; $tokenData['canRename'] = true; @@ -203,7 +176,7 @@ public function destroy($id) { return new JSONResponse([], Http::STATUS_NOT_FOUND); } - $this->tokenProvider->invalidateTokenById($this->uid, $token->getId()); + $this->tokenProvider->invalidateTokenById($this->userId, $token->getId()); $this->publishActivity(Provider::APP_TOKEN_DELETED, $token->getId(), ['name' => $token->getName()]); return []; } @@ -257,8 +230,8 @@ private function publishActivity(string $subject, int $id, array $parameters = [ $event = $this->activityManager->generateEvent(); $event->setApp('settings') ->setType('security') - ->setAffectedUser($this->uid) - ->setAuthor($this->uid) + ->setAffectedUser($this->userId) + ->setAuthor($this->userId) ->setSubject($subject, $parameters) ->setObject('app_token', $id, 'App Password'); @@ -282,7 +255,7 @@ private function findTokenByIdAndUser(int $id): IToken|INamedToken { } catch (ExpiredTokenException $e) { $token = $e->getToken(); } - if ($token->getUID() !== $this->uid) { + if ($token->getUID() !== $this->userId) { /** @psalm-suppress DeprecatedClass We have to throw the OC version so both OC and OCP catches catch it */ throw new InvalidTokenException('This token does not belong to you!'); } diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index fd95743..1eedc0d 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -26,6 +26,7 @@ use OC\Authentication\Token\INamedToken; use OC\Authentication\Token\IProvider; use OC\Authentication\Token\IToken; +use OC_Helper; use OCA\SimpleSettings\AppInfo\Application; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\Attribute\FrontpageRoute; @@ -37,47 +38,33 @@ use OCP\Authentication\Exceptions\InvalidTokenException; use OCP\Files\FileInfo; use OCP\IConfig; +use OCP\IRequest; use OCP\ISession; use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; use OCP\L10N\IFactory; use OCP\Session\Exceptions\SessionNotAvailableException; +use OCP\Util; /** * @psalm-suppress UnusedClass */ class PageController extends Controller { - private IConfig $config; - private IUserManager $userManager; - private IFactory $l10nFactory; - private IProvider $tokenProvider; - private IInitialState $initialState; - private IUserSession $userSession; - private ISession $session; - private ?string $uid; - private $helper; - public function __construct( - IConfig $config, - IUserManager $userManager, - IFactory $l10nFactory, - IProvider $tokenProvider, - ISession $session, - IInitialState $initialState, - IUserSession $userSession, - ?string $UserId, - \OC_Helper $helper, + string $appName, + IRequest $request, + private readonly IConfig $config, + private readonly IUserManager $userManager, + private readonly IFactory $l10nFactory, + private readonly IProvider $tokenProvider, + private readonly ISession $session, + private readonly IInitialState $initialState, + private readonly IUserSession $userSession, + private readonly ?string $userId, + private readonly Util $util, ) { - $this->config = $config; - $this->userManager = $userManager; - $this->l10nFactory = $l10nFactory; - $this->tokenProvider = $tokenProvider; - $this->session = $session; - $this->initialState = $initialState; - $this->userSession = $userSession; - $this->uid = $UserId; - $this->helper = $helper; + parent::__construct($appName, $request); } #[NoCSRFRequired] @@ -95,7 +82,7 @@ public function index(): TemplateResponse { $this->userSession->getImpersonatingUserID() === null ); - $user = $this->userManager->get($this->uid); + $user = $this->userManager->get($this->userId); $storageInfo = $this->getStorageInfo('/'); if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) { @@ -127,7 +114,7 @@ public function index(): TemplateResponse { } private function getAppTokens(): array { - $tokens = $this->tokenProvider->getTokenByUser($this->uid); + $tokens = $this->tokenProvider->getTokenByUser($this->userId); try { $sessionId = $this->session->getId(); @@ -220,10 +207,10 @@ public function getStorageInfo( $includeMountPoints = true, $useCache = true, ): array { - return $this->helper::getStorageInfo($path, $rootInfo, $includeMountPoints, $useCache); + return OC_Helper::getStorageInfo($path, $rootInfo, $includeMountPoints, $useCache); } public function humanFileSize(int $size): string { - return $this->helper::humanFileSize($size); + return $this->util->humanFileSize($size); } } diff --git a/tests/Controller/PageControllerTest.php b/tests/Controller/PageControllerTest.php index 65a0c2b..ddc3f30 100644 --- a/tests/Controller/PageControllerTest.php +++ b/tests/Controller/PageControllerTest.php @@ -28,11 +28,13 @@ use OC\Authentication\Token\IToken; use OCP\AppFramework\Services\IInitialState; use OCP\IConfig; +use OCP\IRequest; use OCP\ISession; use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; use OCP\L10N\IFactory; +use OCP\Util; use PHPUnit\Framework\MockObject\Exception; use Test\TestCase; @@ -43,6 +45,7 @@ class PageControllerTest extends TestCase { protected function setUp(): void { parent::setUp(); + $this->request = $this->createMock(IRequest::class); $this->config = $this->createMock(IConfig::class); $this->userManager = $this->createMock(IUserManager::class); $this->l10nFactory = $this->createMock(IFactory::class); @@ -51,9 +54,11 @@ protected function setUp(): void { $this->initialState = $this->createMock(IInitialState::class); $this->userSession = $this->createMock(IUserSession::class); $this->uid = 'mock-user-id-123'; - $this->helper = $this->createMock(\OC_Helper::class); + $this->util = $this->createMock(Util::class); $this->controller = $this->getMockBuilder(PageController::class) ->setConstructorArgs([ + 'core', + $this->request, $this->config, $this->userManager, $this->l10nFactory, @@ -62,7 +67,7 @@ protected function setUp(): void { $this->initialState, $this->userSession, $this->uid, - $this->helper + $this->util ]) ->onlyMethods(['getStorageInfo', 'humanFileSize']) ->getMock(); diff --git a/tests/stub.phpstub b/tests/stub.phpstub index 09ec74a..8573ca9 100644 --- a/tests/stub.phpstub +++ b/tests/stub.phpstub @@ -42,13 +42,4 @@ class OC_Helper { */ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoints = true, $useCache = true) { } - /** - * Make a human file size - * @param int|float $bytes file size in bytes - * @return string a human readable file size - * - * Makes 2048 to 2 kB. - */ - public static function humanFileSize(int|float $bytes): string { - } }