Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor core/Controller #39212

Closed
wants to merge 17 commits into from
Closed
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
2 changes: 2 additions & 0 deletions core/Controller/AppPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function getAppPassword(): DataResponse {

/**
* @NoAdminRequired
* @throws OCSForbiddenException
*/
public function deleteAppPassword(): DataResponse {
if (!$this->session->exists('app_password')) {
Expand All @@ -122,6 +123,7 @@ public function deleteAppPassword(): DataResponse {

/**
* @NoAdminRequired
* @throws OCSForbiddenException
*/
public function rotateAppPassword(): DataResponse {
if (!$this->session->exists('app_password')) {
Expand Down
22 changes: 13 additions & 9 deletions core/Controller/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@
namespace OC\Core\Controller;

use OC\AppFramework\Utility\TimeFactory;
use OC\Files\Filesystem;
use OC\NotSquareException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\IAvatarManager;
use OCP\ICache;
use OCP\IL10N;
use OCP\Image;
use OCP\IRequest;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -74,7 +78,7 @@ public function __construct(
*
* @return JSONResponse|FileDisplayResponse
*/
public function getAvatarDark(string $userId, int $size) {
public function getAvatarDark(string $userId, int $size): FileDisplayResponse|JSONResponse {
if ($size <= 64) {
if ($size !== 64) {
$this->logger->debug('Avatar requested in deprecated size ' . $size);
Expand Down Expand Up @@ -113,7 +117,7 @@ public function getAvatarDark(string $userId, int $size) {
*
* @return JSONResponse|FileDisplayResponse
*/
public function getAvatar(string $userId, int $size) {
public function getAvatar(string $userId, int $size): FileDisplayResponse|JSONResponse {
if ($size <= 64) {
if ($size !== 64) {
$this->logger->debug('Avatar requested in deprecated size ' . $size);
Expand Down Expand Up @@ -173,7 +177,7 @@ public function postAvatar(?string $path = null): JSONResponse {

try {
$content = $node->getContent();
} catch (\OCP\Files\NotPermittedException $e) {
} catch (NotPermittedException $e) {
return new JSONResponse(
['data' => ['message' => $this->l10n->t('The selected file cannot be read.')]],
Http::STATUS_BAD_REQUEST
Expand All @@ -183,7 +187,7 @@ public function postAvatar(?string $path = null): JSONResponse {
if (
$files['error'][0] === 0 &&
is_uploaded_file($files['tmp_name'][0]) &&
!\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
!Filesystem::isFileBlacklisted($files['tmp_name'][0])
) {
if ($files['size'][0] > 20 * 1024 * 1024) {
return new JSONResponse(
Expand Down Expand Up @@ -221,7 +225,7 @@ public function postAvatar(?string $path = null): JSONResponse {
}

try {
$image = new \OCP\Image();
$image = new Image();
$image->loadFromData($content);
$image->readExif($content);
$image->fixOrientation();
Expand Down Expand Up @@ -284,7 +288,7 @@ public function deleteAvatar(): JSONResponse {
*
* @return JSONResponse|DataDisplayResponse
*/
public function getTmpAvatar() {
public function getTmpAvatar(): JSONResponse|DataDisplayResponse {
$tmpAvatar = $this->cache->get('tmpAvatar');
if (is_null($tmpAvatar)) {
return new JSONResponse(['data' => [
Expand All @@ -293,7 +297,7 @@ public function getTmpAvatar() {
Http::STATUS_NOT_FOUND);
}

$image = new \OCP\Image();
$image = new Image();
$image->loadFromData($tmpAvatar);

$resp = new DataDisplayResponse(
Expand Down Expand Up @@ -329,7 +333,7 @@ public function postCroppedAvatar(?array $crop = null): JSONResponse {
Http::STATUS_BAD_REQUEST);
}

$image = new \OCP\Image();
$image = new Image();
$image->loadFromData($tmpAvatar);
$image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h']));
try {
Expand All @@ -338,7 +342,7 @@ public function postCroppedAvatar(?array $crop = null): JSONResponse {
// Clean up
$this->cache->remove('tmpAvatar');
return new JSONResponse(['status' => 'success']);
} catch (\OC\NotSquareException $e) {
} catch (NotSquareException $e) {
return new JSONResponse(['data' => ['message' => $this->l10n->t('Crop is not square')]],
Http::STATUS_BAD_REQUEST);
} catch (\Exception $e) {
Expand Down
8 changes: 6 additions & 2 deletions core/Controller/ClientFlowLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@
use OCA\OAuth2\Db\AccessToken;
use OCA\OAuth2\Db\AccessTokenMapper;
use OCA\OAuth2\Db\ClientMapper;
use OCA\OAuth2\Exceptions\ClientNotFoundException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\UseSession;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\StandaloneTemplateResponse;
use OCP\DB\Exception;
use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IL10N;
Expand Down Expand Up @@ -224,11 +227,12 @@ public function grantPage(string $stateToken = '',
/**
* @NoAdminRequired
*
* @return Http\RedirectResponse|Response
* @throws ClientNotFoundException
* @throws Exception
*/
#[UseSession]
public function generateAppPassword(string $stateToken,
string $clientIdentifier = '') {
string $clientIdentifier = ''): Response|StandaloneTemplateResponse|RedirectResponse {
if (!$this->isValidToken($stateToken)) {
$this->session->remove(self::STATE_NAME);
return $this->stateTokenForbiddenResponse();
Expand Down
5 changes: 3 additions & 2 deletions core/Controller/ClientFlowLoginV2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
namespace OC\Core\Controller;

use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Token\IProvider;
use OC\Core\Db\LoginFlowV2;
use OC\Core\Exception\LoginFlowV2NotFoundException;
use OC\Core\Service\LoginFlowV2Service;
Expand Down Expand Up @@ -170,7 +171,7 @@ public function grantPage(?string $stateToken): StandaloneTemplateResponse {
/**
* @PublicPage
*/
public function apptokenRedirect(?string $stateToken, string $user, string $password) {
public function apptokenRedirect(?string $stateToken, string $user, string $password): StandaloneTemplateResponse {
if ($stateToken === null) {
return $this->stateTokenMissingResponse();
}
Expand All @@ -192,7 +193,7 @@ public function apptokenRedirect(?string $stateToken, string $user, string $pass
$this->session->remove(self::STATE_NAME);

try {
$token = \OC::$server->get(\OC\Authentication\Token\IProvider::class)->getToken($password);
$token = \OC::$server->get(IProvider::class)->getToken($password);
if ($token->getLoginName() !== $user) {
throw new InvalidTokenException('login name does not match');
}
Expand Down
3 changes: 1 addition & 2 deletions core/Controller/ContactsMenuController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ public function index(?string $filter = null): array {
/**
* @NoAdminRequired
*
* @return JSONResponse|\JsonSerializable
* @throws Exception
*/
public function findOne(int $shareType, string $shareWith) {
public function findOne(int $shareType, string $shareWith): JSONResponse|\JsonSerializable {
$contact = $this->manager->findOne($this->userSession->getUser(), $shareType, $shareWith);

if ($contact) {
Expand Down
2 changes: 1 addition & 1 deletion core/Controller/CssController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(
* @param string $appName css folder name
* @return FileDisplayResponse|NotFoundResponse
*/
public function getCss(string $fileName, string $appName): Response {
public function getCss(string $fileName, string $appName): FileDisplayResponse|NotFoundResponse {
try {
$folder = $this->appData->getFolder($appName);
$gzip = false;
Expand Down
8 changes: 5 additions & 3 deletions core/Controller/GuestAvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\Response;
use OCP\IAvatarManager;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -53,9 +54,10 @@ public function __construct(
*
* @param string $guestName The guest name, e.g. "Albert"
* @param string $size The desired avatar size, e.g. 64 for 64x64px
* @return FileDisplayResponse|Http\Response
* @param bool|null $darkTheme
* @return FileDisplayResponse|Response
*/
public function getAvatar(string $guestName, string $size, ?bool $darkTheme = false) {
public function getAvatar(string $guestName, string $size, ?bool $darkTheme = false): Response|FileDisplayResponse {
$size = (int) $size;
$darkTheme = $darkTheme ?? false;

Expand Down Expand Up @@ -98,7 +100,7 @@ public function getAvatar(string $guestName, string $size, ?bool $darkTheme = fa
* @PublicPage
* @NoCSRFRequired
*/
public function getAvatarDark(string $guestName, string $size) {
public function getAvatarDark(string $guestName, string $size): Response|FileDisplayResponse {
return $this->getAvatar($guestName, $size, true);
}
}
2 changes: 1 addition & 1 deletion core/Controller/JsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(
* @param string $appName js folder name
* @return FileDisplayResponse|NotFoundResponse
*/
public function getJs(string $fileName, string $appName): Response {
public function getJs(string $fileName, string $appName): FileDisplayResponse|NotFoundResponse {
try {
$folder = $this->appData->getFolder($appName);
$gzip = false;
Expand Down
12 changes: 5 additions & 7 deletions core/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function __construct(
* @return RedirectResponse
*/
#[UseSession]
public function logout() {
public function logout(): RedirectResponse {
$loginToken = $this->request->getCookie('nc_token');
if (!is_null($loginToken)) {
$this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken);
Expand All @@ -113,8 +113,8 @@ public function logout() {
* @PublicPage
* @NoCSRFRequired
*
* @param string $user
* @param string $redirect_url
* @param string|null $user
* @param string|null $redirect_url
*
* @return TemplateResponse|RedirectResponse
*/
Expand Down Expand Up @@ -196,7 +196,7 @@ public function showLoginForm(string $user = null, string $redirect_url = null):
/**
* Sets the password reset state
*
* @param string $username
* @param string|null $username
*/
private function setPasswordResetInitialState(?string $username): void {
if ($username !== null && $username !== '') {
Expand Down Expand Up @@ -263,8 +263,6 @@ private function generateRedirect(?string $redirectUrl): RedirectResponse {
* @PublicPage
* @NoCSRFRequired
* @BruteForceProtection(action=login)
*
* @return RedirectResponse
*/
#[UseSession]
public function tryLogin(Chain $loginChain,
Expand Down Expand Up @@ -327,7 +325,7 @@ public function tryLogin(Chain $loginChain,
* @return RedirectResponse
*/
private function createLoginFailedResponse(
$user, $originalUser, $redirect_url, string $loginMessage) {
string $user, string $originalUser, string $redirect_url, string $loginMessage): RedirectResponse {
// Read current user and append if possible we need to
// return the unmodified user otherwise we will leak the login name
$args = $user !== null ? ['user' => $originalUser, 'direct' => 1] : [];
Expand Down
4 changes: 2 additions & 2 deletions core/Controller/LostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use OC\Core\Exception\ResetPasswordException;
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
use OC\Security\RateLimiting\Limiter;
use OCP\Util;
use Psr\Log\LoggerInterface;
use function array_filter;
use function count;
Expand Down Expand Up @@ -176,7 +177,7 @@ public function email(string $user): JSONResponse {

$user = trim($user);

\OCP\Util::emitHook(
Util::emitHook(
'\OCA\Files_Sharing\API\Server2Server',
'preLoginNameUsedAsUserName',
['uid' => &$user]
Expand Down Expand Up @@ -252,7 +253,6 @@ public function setPassword(string $token, string $userId, string $password, boo

/**
* @throws ResetPasswordException
* @throws \OCP\PreConditionNotMetException
*/
protected function sendEmail(string $input): void {
$user = $this->findUserByIdOrMail($input);
Expand Down
15 changes: 8 additions & 7 deletions core/Controller/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@
*/
namespace OC\Core\Controller;

use OC\User\NoUserException;
use OCA\Files_Sharing\SharedStorage;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\Response;
use OCP\Files\File;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IPreview;
use OCP\IRequest;

Expand All @@ -54,7 +58,9 @@ public function __construct(
* @NoAdminRequired
* @NoCSRFRequired
*
* @return DataResponse|FileDisplayResponse
* @return Response
* @throws NotPermittedException
* @throws NoUserException
*/
public function getPreview(
string $file = '',
Expand All @@ -80,16 +86,14 @@ public function getPreview(
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @return DataResponse|FileDisplayResponse
*/
public function getPreviewByFileId(
int $fileId = -1,
int $x = 32,
int $y = 32,
bool $a = false,
bool $forceIcon = true,
string $mode = 'fill') {
string $mode = 'fill'): DataResponse|FileDisplayResponse {
if ($fileId === -1 || $x === 0 || $y === 0) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
Expand All @@ -106,9 +110,6 @@ public function getPreviewByFileId(
return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode);
}

/**
* @return DataResponse|FileDisplayResponse
*/
private function fetchPreview(
Node $node,
int $x,
Expand Down
1 change: 1 addition & 0 deletions core/Controller/ProfileApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\DB\Exception;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
Expand Down
2 changes: 1 addition & 1 deletion core/Controller/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function run(array $post): void {
}
}

private function displaySetupForbidden() {
private function displaySetupForbidden(): void {
\OC_Template::printGuestPage('', 'installation_forbidden');
}

Expand Down
Loading