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

[stable-6] intoduce internal UserSession #3503

Closed
wants to merge 6 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
4 changes: 0 additions & 4 deletions lib/AppConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ abstract class AppConstants {
/** @var string */
public const APP_ID = 'polls';
/** @var string */
public const SESSION_KEY_USER_ID = 'ncPollsUserId';
/** @var string */
public const SESSION_KEY_SHARE_TOKEN = 'ncPollsPublicToken';
/** @var string */
public const CLIENT_ID = 'ncPollsClientId';
/** @var string */
public const CLIENT_TZ = 'ncPollsClientTimeZone';
Expand Down
35 changes: 35 additions & 0 deletions lib/Attributes/ShareTokenRequired.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2024 René Gieling <[email protected]>
*
* @author René Gieling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Polls\Attribute;

use Attribute;

/**
* Attribute for permission check against Acl
*/
#[Attribute]
class ShareTokenRequired {
}
4 changes: 1 addition & 3 deletions lib/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
namespace OCA\Polls\Controller;

use OCA\Polls\AppConstants;
use OCA\Polls\Db\UserMapper;
use OCA\Polls\Service\PollService;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
Expand All @@ -46,7 +45,6 @@ public function __construct(
private IURLGenerator $urlGenerator,
private PollService $pollService,
private IEventDispatcher $eventDispatcher,
private UserMapper $userMapper,
) {
parent::__construct($appName, $request);
}
Expand All @@ -71,7 +69,7 @@ public function list(): JSONResponse {
* Get list of polls for administrative purposes
*/
public function takeover(int $pollId): JSONResponse {
return $this->response(fn () => $this->pollService->takeover($pollId, $this->userMapper->getCurrentUser()));
return $this->response(fn () => $this->pollService->takeover($pollId));
}

/**
Expand Down
22 changes: 6 additions & 16 deletions lib/Controller/BasePublicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@
namespace OCA\Polls\Controller;

use Closure;
use OCA\Polls\AppConstants;
use OCA\Polls\Exceptions\Exception;
use OCA\Polls\Exceptions\NoUpdatesException;
use OCA\Polls\Model\Acl;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\ISession;

/**
* @psalm-api
Expand All @@ -43,19 +41,17 @@ class BasePublicController extends Controller {
public function __construct(
string $appName,
IRequest $request,
protected ISession $session,
protected Acl $acl,
) {
parent::__construct($appName, $request);
}

/**
* response
* @param Closure $callback Callback function
* @NoAdminRequired
*/
protected function response(Closure $callback, string $token): JSONResponse {
$this->updateSessionToken($token);

protected function response(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback(), Http::STATUS_OK);
} catch (Exception $e) {
Expand All @@ -65,11 +61,10 @@ protected function response(Closure $callback, string $token): JSONResponse {

/**
* response
* @param Closure $callback Callback function
* @NoAdminRequired
*/
protected function responseLong(Closure $callback, string $token): JSONResponse {
$this->updateSessionToken($token);

protected function responseLong(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback(), Http::STATUS_OK);
} catch (NoUpdatesException $e) {
Expand All @@ -78,19 +73,14 @@ protected function responseLong(Closure $callback, string $token): JSONResponse
}
/**
* responseCreate
* @param Closure $callback Callback function
* @NoAdminRequired
*/
protected function responseCreate(Closure $callback, string $token): JSONResponse {
$this->updateSessionToken($token);

protected function responseCreate(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback(), Http::STATUS_CREATED);
} catch (Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}

private function updateSessionToken(string $token): void {
$this->session->set(AppConstants::SESSION_KEY_SHARE_TOKEN, $token);
}
}
6 changes: 3 additions & 3 deletions lib/Controller/PreferencesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

namespace OCA\Polls\Controller;

use OCA\Polls\Db\UserMapper;
use OCA\Polls\Service\CalendarService;
use OCA\Polls\Service\PreferencesService;
use OCA\Polls\UserSession;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
Expand All @@ -41,7 +41,7 @@ public function __construct(
IRequest $request,
private PreferencesService $preferencesService,
private CalendarService $calendarService,
private UserMapper $userMapper,
private UserSession $userSession,
) {
parent::__construct($appName, $request);
}
Expand All @@ -60,7 +60,7 @@ public function get(): JSONResponse {
* @NoAdminRequired
*/
public function write(array $preferences): JSONResponse {
if (!$this->userMapper->getCurrentUser()->getIsLoggedIn()) {
if (!$this->userSession->getIsLoggedIn()) {
return new JSONResponse([], Http::STATUS_OK);
}
return $this->response(fn () => $this->preferencesService->write($preferences));
Expand Down
Loading
Loading