Skip to content

Commit

Permalink
Merge pull request #38636 from fsamapoor/constructor_property_promoti…
Browse files Browse the repository at this point in the history
…on_part1

[1/3] Refactors /core controllers using constructor property promotion.
  • Loading branch information
szaimen authored Jun 12, 2023
2 parents 1001adc + fc0e2a9 commit 0d18929
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 192 deletions.
28 changes: 9 additions & 19 deletions core/Controller/AppPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,16 @@
use OCP\Security\ISecureRandom;

class AppPasswordController extends \OCP\AppFramework\OCSController {
private ISession $session;
private ISecureRandom $random;
private IProvider $tokenProvider;
private IStore $credentialStore;
private IEventDispatcher $eventDispatcher;

public function __construct(string $appName,
IRequest $request,
ISession $session,
ISecureRandom $random,
IProvider $tokenProvider,
IStore $credentialStore,
IEventDispatcher $eventDispatcher) {
public function __construct(
string $appName,
IRequest $request,
private ISession $session,
private ISecureRandom $random,
private IProvider $tokenProvider,
private IStore $credentialStore,
private IEventDispatcher $eventDispatcher,
) {
parent::__construct($appName, $request);

$this->session = $session;
$this->random = $random;
$this->tokenProvider = $tokenProvider;
$this->credentialStore = $credentialStore;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down
20 changes: 7 additions & 13 deletions core/Controller/AutoCompleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,14 @@
use OCP\Share\IShare;

class AutoCompleteController extends OCSController {
private ISearch $collaboratorSearch;
private IManager $autoCompleteManager;
private IEventDispatcher $dispatcher;

public function __construct(string $appName,
IRequest $request,
ISearch $collaboratorSearch,
IManager $autoCompleteManager,
IEventDispatcher $dispatcher) {
public function __construct(
string $appName,
IRequest $request,
private ISearch $collaboratorSearch,
private IManager $autoCompleteManager,
private IEventDispatcher $dispatcher,
) {
parent::__construct($appName, $request);

$this->collaboratorSearch = $collaboratorSearch;
$this->autoCompleteManager = $autoCompleteManager;
$this->dispatcher = $dispatcher;
}

/**
Expand Down
94 changes: 38 additions & 56 deletions core/Controller/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -52,36 +51,19 @@
* @package OC\Core\Controller
*/
class AvatarController extends Controller {
protected IAvatarManager $avatarManager;
protected ICache $cache;
protected IL10N $l;
protected IUserManager $userManager;
protected IUserSession $userSession;
protected IRootFolder $rootFolder;
protected LoggerInterface $logger;
protected ?string $userId;
protected TimeFactory $timeFactory;

public function __construct(string $appName,
IRequest $request,
IAvatarManager $avatarManager,
ICache $cache,
IL10N $l10n,
IUserManager $userManager,
IRootFolder $rootFolder,
LoggerInterface $logger,
?string $userId,
TimeFactory $timeFactory) {
public function __construct(
string $appName,
IRequest $request,
protected IAvatarManager $avatarManager,
protected ICache $cache,
protected IL10N $l10n,
protected IUserManager $userManager,
protected IRootFolder $rootFolder,
protected LoggerInterface $logger,
protected ?string $userId,
protected TimeFactory $timeFactory,
) {
parent::__construct($appName, $request);

$this->avatarManager = $avatarManager;
$this->cache = $cache;
$this->l = $l10n;
$this->userManager = $userManager;
$this->rootFolder = $rootFolder;
$this->logger = $logger;
$this->userId = $userId;
$this->timeFactory = $timeFactory;
}

/**
Expand Down Expand Up @@ -173,18 +155,18 @@ public function postAvatar(?string $path = null): JSONResponse {
/** @var File $node */
$node = $userFolder->get($path);
if (!($node instanceof File)) {
return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]);
return new JSONResponse(['data' => ['message' => $this->l10n->t('Please select a file.')]]);
}
if ($node->getSize() > 20 * 1024 * 1024) {
return new JSONResponse(
['data' => ['message' => $this->l->t('File is too big')]],
['data' => ['message' => $this->l10n->t('File is too big')]],
Http::STATUS_BAD_REQUEST
);
}

if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') {
return new JSONResponse(
['data' => ['message' => $this->l->t('The selected file is not an image.')]],
['data' => ['message' => $this->l10n->t('The selected file is not an image.')]],
Http::STATUS_BAD_REQUEST
);
}
Expand All @@ -193,7 +175,7 @@ public function postAvatar(?string $path = null): JSONResponse {
$content = $node->getContent();
} catch (\OCP\Files\NotPermittedException $e) {
return new JSONResponse(
['data' => ['message' => $this->l->t('The selected file cannot be read.')]],
['data' => ['message' => $this->l10n->t('The selected file cannot be read.')]],
Http::STATUS_BAD_REQUEST
);
}
Expand All @@ -205,7 +187,7 @@ public function postAvatar(?string $path = null): JSONResponse {
) {
if ($files['size'][0] > 20 * 1024 * 1024) {
return new JSONResponse(
['data' => ['message' => $this->l->t('File is too big')]],
['data' => ['message' => $this->l10n->t('File is too big')]],
Http::STATUS_BAD_REQUEST
);
}
Expand All @@ -214,16 +196,16 @@ public function postAvatar(?string $path = null): JSONResponse {
unlink($files['tmp_name'][0]);
} else {
$phpFileUploadErrors = [
UPLOAD_ERR_OK => $this->l->t('The file was uploaded'),
UPLOAD_ERR_INI_SIZE => $this->l->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
UPLOAD_ERR_FORM_SIZE => $this->l->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
UPLOAD_ERR_PARTIAL => $this->l->t('The file was only partially uploaded'),
UPLOAD_ERR_NO_FILE => $this->l->t('No file was uploaded'),
UPLOAD_ERR_NO_TMP_DIR => $this->l->t('Missing a temporary folder'),
UPLOAD_ERR_CANT_WRITE => $this->l->t('Could not write file to disk'),
UPLOAD_ERR_EXTENSION => $this->l->t('A PHP extension stopped the file upload'),
UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'),
UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'),
UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'),
UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'),
UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'),
UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'),
];
$message = $phpFileUploadErrors[$files['error'][0]] ?? $this->l->t('Invalid file provided');
$message = $phpFileUploadErrors[$files['error'][0]] ?? $this->l10n->t('Invalid file provided');
$this->logger->warning($message, ['app' => 'core']);
return new JSONResponse(
['data' => ['message' => $message]],
Expand All @@ -233,7 +215,7 @@ public function postAvatar(?string $path = null): JSONResponse {
} else {
//Add imgfile
return new JSONResponse(
['data' => ['message' => $this->l->t('No image or file provided')]],
['data' => ['message' => $this->l10n->t('No image or file provided')]],
Http::STATUS_BAD_REQUEST
);
}
Expand All @@ -248,7 +230,7 @@ public function postAvatar(?string $path = null): JSONResponse {
$mimeType = $image->mimeType();
if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
return new JSONResponse(
['data' => ['message' => $this->l->t('Unknown filetype')]],
['data' => ['message' => $this->l10n->t('Unknown filetype')]],
Http::STATUS_OK
);
}
Expand All @@ -262,7 +244,7 @@ public function postAvatar(?string $path = null): JSONResponse {
return new JSONResponse(['status' => 'success']);
} catch (\Throwable $e) {
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
}
}

Expand All @@ -273,13 +255,13 @@ public function postAvatar(?string $path = null): JSONResponse {
);
} else {
return new JSONResponse(
['data' => ['message' => $this->l->t('Invalid image')]],
['data' => ['message' => $this->l10n->t('Invalid image')]],
Http::STATUS_OK
);
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK);
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK);
}
}

Expand All @@ -293,7 +275,7 @@ public function deleteAvatar(): JSONResponse {
return new JSONResponse();
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
}
}

Expand All @@ -306,7 +288,7 @@ public function getTmpAvatar() {
$tmpAvatar = $this->cache->get('tmpAvatar');
if (is_null($tmpAvatar)) {
return new JSONResponse(['data' => [
'message' => $this->l->t("No temporary profile picture available, try again")
'message' => $this->l10n->t("No temporary profile picture available, try again")
]],
Http::STATUS_NOT_FOUND);
}
Expand All @@ -330,19 +312,19 @@ public function getTmpAvatar() {
*/
public function postCroppedAvatar(?array $crop = null): JSONResponse {
if (is_null($crop)) {
return new JSONResponse(['data' => ['message' => $this->l->t("No crop data provided")]],
return new JSONResponse(['data' => ['message' => $this->l10n->t("No crop data provided")]],
Http::STATUS_BAD_REQUEST);
}

if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) {
return new JSONResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]],
return new JSONResponse(['data' => ['message' => $this->l10n->t("No valid crop data provided")]],
Http::STATUS_BAD_REQUEST);
}

$tmpAvatar = $this->cache->get('tmpAvatar');
if (is_null($tmpAvatar)) {
return new JSONResponse(['data' => [
'message' => $this->l->t("No temporary profile picture available, try again")
'message' => $this->l10n->t("No temporary profile picture available, try again")
]],
Http::STATUS_BAD_REQUEST);
}
Expand All @@ -357,11 +339,11 @@ public function postCroppedAvatar(?array $crop = null): JSONResponse {
$this->cache->remove('tmpAvatar');
return new JSONResponse(['status' => 'success']);
} catch (\OC\NotSquareException $e) {
return new JSONResponse(['data' => ['message' => $this->l->t('Crop is not square')]],
return new JSONResponse(['data' => ['message' => $this->l10n->t('Crop is not square')]],
Http::STATUS_BAD_REQUEST);
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
}
}
}
10 changes: 5 additions & 5 deletions core/Controller/CSRFTokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
use OCP\IRequest;

class CSRFTokenController extends Controller {
private CsrfTokenManager $tokenManager;

public function __construct(string $appName, IRequest $request,
CsrfTokenManager $tokenManager) {
public function __construct(
string $appName,
IRequest $request,
private CsrfTokenManager $tokenManager,
) {
parent::__construct($appName, $request);
$this->tokenManager = $tokenManager;
}

/**
Expand Down
51 changes: 15 additions & 36 deletions core/Controller/ClientFlowLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,45 +57,24 @@
use OCP\Session\Exceptions\SessionNotAvailableException;

class ClientFlowLoginController extends Controller {
private IUserSession $userSession;
private IL10N $l10n;
private Defaults $defaults;
private ISession $session;
private IProvider $tokenProvider;
private ISecureRandom $random;
private IURLGenerator $urlGenerator;
private ClientMapper $clientMapper;
private AccessTokenMapper $accessTokenMapper;
private ICrypto $crypto;
private IEventDispatcher $eventDispatcher;

public const STATE_NAME = 'client.flow.state.token';

public function __construct(string $appName,
IRequest $request,
IUserSession $userSession,
IL10N $l10n,
Defaults $defaults,
ISession $session,
IProvider $tokenProvider,
ISecureRandom $random,
IURLGenerator $urlGenerator,
ClientMapper $clientMapper,
AccessTokenMapper $accessTokenMapper,
ICrypto $crypto,
IEventDispatcher $eventDispatcher) {
public function __construct(
string $appName,
IRequest $request,
private IUserSession $userSession,
private IL10N $l10n,
private Defaults $defaults,
private ISession $session,
private IProvider $tokenProvider,
private ISecureRandom $random,
private IURLGenerator $urlGenerator,
private ClientMapper $clientMapper,
private AccessTokenMapper $accessTokenMapper,
private ICrypto $crypto,
private IEventDispatcher $eventDispatcher,
) {
parent::__construct($appName, $request);
$this->userSession = $userSession;
$this->l10n = $l10n;
$this->defaults = $defaults;
$this->session = $session;
$this->tokenProvider = $tokenProvider;
$this->random = $random;
$this->urlGenerator = $urlGenerator;
$this->clientMapper = $clientMapper;
$this->accessTokenMapper = $accessTokenMapper;
$this->crypto = $crypto;
$this->eventDispatcher = $eventDispatcher;
}

private function getClientName(): string {
Expand Down
Loading

0 comments on commit 0d18929

Please sign in to comment.