Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 16 additions & 43 deletions lib/Controller/AuthSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 [];
}
Expand Down Expand Up @@ -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');

Expand All @@ -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!');
}
Expand Down
51 changes: 19 additions & 32 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
9 changes: 7 additions & 2 deletions tests/Controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -62,7 +67,7 @@ protected function setUp(): void {
$this->initialState,
$this->userSession,
$this->uid,
$this->helper
$this->util
])
->onlyMethods(['getStorageInfo', 'humanFileSize'])
->getMock();
Expand Down
9 changes: 0 additions & 9 deletions tests/stub.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
}
Loading