Skip to content

Commit

Permalink
Merge pull request #3814 from nextcloud/enh/add-date-options/next
Browse files Browse the repository at this point in the history
Add Options (Date) alternative
  • Loading branch information
dartcafe authored Dec 24, 2024
2 parents 1f27841 + 003da1f commit 8ec48c6
Show file tree
Hide file tree
Showing 47 changed files with 1,279 additions and 497 deletions.
2 changes: 1 addition & 1 deletion lib/Attributes/ShareTokenRequired.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Attribute;

/**
* Attribute for permission check against Acl
* Attribute for permission check against existing share
*/
#[Attribute]
class ShareTokenRequired {
Expand Down
7 changes: 2 additions & 5 deletions lib/Controller/PollApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace OCA\Polls\Controller;

use OCA\Polls\Model\Acl as Acl;
use OCA\Polls\ResponseDefinitions as ResponseDefinitions;
use OCA\Polls\Service\CommentService;
use OCA\Polls\Service\OptionService;
Expand All @@ -31,7 +30,6 @@ class PollApiController extends BaseApiV2Controller {
public function __construct(
string $appName,
IRequest $request,
private Acl $acl,
private CommentService $commentService,
private PollService $pollService,
private OptionService $optionService,
Expand All @@ -52,7 +50,7 @@ public function __construct(
public function list(): DataResponse {
return $this->response(fn () => ['polls' => $this->pollService->list()]);
}

/**
* get complete poll
* @param int $pollId Poll id
Expand All @@ -69,10 +67,9 @@ public function get(int $pollId): DataResponse {
'comments' => $this->commentService->list($pollId),
'shares' => $this->shareService->list($pollId),
'subscribed' => $this->subscriptionService->get($pollId),
'acl' => $this->acl,
]);
}

/**
* Add poll
* @param string $title Poll title
Expand Down
7 changes: 0 additions & 7 deletions lib/Controller/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace OCA\Polls\Controller;

use OCA\Polls\Model\Acl as Acl;
use OCA\Polls\Model\Settings\AppSettings;
use OCA\Polls\Service\CommentService;
use OCA\Polls\Service\MailService;
Expand All @@ -30,7 +29,6 @@ class PollController extends BaseController {
public function __construct(
string $appName,
IRequest $request,
private Acl $acl,
private MailService $mailService,
private OptionService $optionService,
private PollService $pollService,
Expand Down Expand Up @@ -69,7 +67,6 @@ public function list(): JSONResponse {
public function get(int $pollId): JSONResponse {
return $this->response(fn () => [
'poll' => $this->pollService->get($pollId),
'acl' => $this->acl,
]);
}

Expand All @@ -87,7 +84,6 @@ public function getFull(int $pollId): JSONResponse {
'comments' => $this->commentService->list($pollId),
'shares' => $this->shareService->list($pollId),
'subscribed' => $this->subscriptionService->get($pollId),
'acl' => $this->acl,
]);
}

Expand All @@ -112,7 +108,6 @@ public function add(string $type, string $title): JSONResponse {
public function update(int $pollId, array $poll): JSONResponse {
return $this->response(fn () => [
'poll' => $this->pollService->update($pollId, $poll),
'acl' => $this->acl,
]);
}

Expand Down Expand Up @@ -157,7 +152,6 @@ public function delete(int $pollId): JSONResponse {
public function close(int $pollId): JSONResponse {
return $this->response(fn () => [
'poll' => $this->pollService->close($pollId),
'acl' => $this->acl,
]);
}

Expand All @@ -170,7 +164,6 @@ public function close(int $pollId): JSONResponse {
public function reopen(int $pollId): JSONResponse {
return $this->response(fn () => [
'poll' => $this->pollService->reopen($pollId),
'acl' => $this->acl,
]);
}

Expand Down
26 changes: 5 additions & 21 deletions lib/Controller/PublicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use OCA\Polls\AppConstants;
use OCA\Polls\Attributes\ShareTokenRequired;
use OCA\Polls\Model\Acl as Acl;
use OCA\Polls\Model\Settings\AppSettings;
use OCA\Polls\Service\CommentService;
use OCA\Polls\Service\MailService;
use OCA\Polls\Service\OptionService;
Expand All @@ -32,18 +32,17 @@
use OCP\Util;

/**
* Always use parent's classe response* methods to make sure, the token gets set correctly.
* Always use parent's class response* methods to make sure, the token gets set correctly.
* Requesting the token inside the controller is not possible, because the token is submitted
* as a paramter and not known while contruction time
* i.e. ACL requests are not valid before calling the response* method
* @psalm-api
*/
class PublicController extends BasePublicController {
public function __construct(
string $appName,
IRequest $request,
private Acl $acl,
private UserSession $userSession,
private AppSettings $appSettings,
private CommentService $commentService,
private MailService $mailService,
private OptionService $optionService,
Expand Down Expand Up @@ -92,25 +91,10 @@ public function getPoll(): JSONResponse {
'comments' => $this->commentService->list($this->userSession->getShare()->getPollId()),
'shares' => $this->shareService->list($this->userSession->getShare()->getPollId()),
'subscribed' => $this->subscriptionService->get($this->userSession->getShare()->getPollId()),
'acl' => $this->acl,
];
});
}

/**
* get acl for user
* @deprecated 8.0.0 Use getSession instead
*/
#[PublicPage]
#[ShareTokenRequired]
#[OpenAPI(OpenAPI::SCOPE_IGNORE)]
#[FrontpageRoute(verb: 'GET', url: '/s/{token}/acl')]
public function getAcl(): JSONResponse {
return $this->response(fn () => [
'acl' => $this->acl
]);
}

/**
* get session information
*/
Expand All @@ -122,8 +106,8 @@ public function getSession(): JSONResponse {
return $this->response(fn () => [
'token' => $this->request->getParam('token'),
'currentUser' => $this->userSession->getUser(),
'appPermissions' => $this->acl->getPermissionsArray(),
'appSettings' => $this->acl->getAppSettings(),
'appPermissions' => $this->appSettings->getPermissionsArray(),
'appSettings' => $this->appSettings->getAppSettings(),
'share' => $this->userSession->getShare(),
]);
}
Expand Down
12 changes: 7 additions & 5 deletions lib/Controller/UserApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

namespace OCA\Polls\Controller;

use OCA\Polls\Model\Acl as Acl;
use OCA\Polls\Model\Settings\AppSettings;
use OCA\Polls\Service\PreferencesService;
use OCA\Polls\UserSession;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\CORS;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
Expand All @@ -25,7 +26,8 @@ public function __construct(
string $appName,
IRequest $request,
private PreferencesService $preferencesService,
private Acl $acl,
private UserSession $userSession,
private AppSettings $appSettings,
) {
parent::__construct($appName, $request);
}
Expand All @@ -50,9 +52,9 @@ public function writePreferences(array $preferences): DataResponse {
public function getSession(): DataResponse {
return $this->response(fn () => [
'token' => $this->request->getParam('token'),
'currentUser' => $this->acl->getCurrentUser(),
'appPermissions' => $this->acl->getPermissionsArray(),
'appSettings' => $this->acl->getAppSettings(),
'currentUser' => $this->userSession->getUser(),
'appPermissions' => $this->appSettings->getPermissionsArray(),
'appSettings' => $this->appSettings->getAppSettings(),
]);
}
}
29 changes: 9 additions & 20 deletions lib/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

namespace OCA\Polls\Controller;

use OCA\Polls\Model\Acl;
use OCA\Polls\Model\Settings\AppSettings;
use OCA\Polls\Service\CalendarService;
use OCA\Polls\Service\PreferencesService;
use OCA\Polls\UserSession;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\OpenAPI;
Expand All @@ -26,7 +27,8 @@ public function __construct(
IRequest $request,
private PreferencesService $preferencesService,
private CalendarService $calendarService,
private Acl $acl,
private UserSession $userSession,
private AppSettings $appSettings,
) {
parent::__construct($appName, $request);
}
Expand All @@ -51,7 +53,7 @@ public function getPreferences(): JSONResponse {
public function writePreferences(array $preferences): JSONResponse {
return $this->response(fn () => $this->preferencesService->write($preferences));
}

/**
* get session information
*/
Expand All @@ -61,13 +63,13 @@ public function writePreferences(array $preferences): JSONResponse {
public function getSession(): JSONResponse {
return $this->response(fn () => [
'token' => $this->request->getParam('token'),
'currentUser' => $this->acl->getCurrentUser(),
'appPermissions' => $this->acl->getPermissionsArray(),
'appSettings' => $this->acl->getAppSettings(),
'currentUser' => $this->userSession->getUser(),
'appPermissions' => $this->appSettings->getPermissionsArray(),
'appSettings' => $this->appSettings->getAppSettings(),
'share' => null,
]);
}

/**
* Read all calendars
*/
Expand All @@ -79,17 +81,4 @@ public function getCalendars(): JSONResponse {
'calendars' => $this->calendarService->getCalendars(),
]);
}

/**
* get acl for user
* @deprecated 8.0.0 Use getSession instead
*/
#[NoAdminRequired]
#[OpenAPI(OpenAPI::SCOPE_IGNORE)]
#[FrontpageRoute(verb: 'GET', url: '/acl')]
public function getAcl(): JSONResponse {
return $this->response(fn () => [
'acl' => $this->acl,
]);
}
}
9 changes: 7 additions & 2 deletions lib/Db/EntityWithUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ public function __construct() {
$this->addType('anonymized', 'integer');
$this->addType('poll_expire', 'integer');
}

/**
* Anonymized the user completely (ANON_FULL) or just strips out personal information
*/
public function getAnonymizeLevel(): string {
$currentUserId = Container::queryClass(UserSession::class)->getCurrentUserId();
// Don't censor for poll owner or it is the current user's entity
if ($this->getPollOwnerId() === $currentUserId || $this->getUserId() === $currentUserId) {
if ($this->getPollOwnerId() === $currentUserId || $this->getIsOwner()) {
return self::ANON_NONE;
}

Expand All @@ -63,10 +64,14 @@ public function getAnonymizeLevel(): string {
) {
return self::ANON_FULL;
}

return self::ANON_PRIVACY;
}

public function getIsOwner(): bool {
return Container::queryClass(UserSession::class)->getCurrentUserId() === $this->getUserId();
}

public function getUser(): UserBase {
/** @var UserMapper */
$userMapper = (Container::queryClass(UserMapper::class));
Expand Down
9 changes: 5 additions & 4 deletions lib/Db/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public function jsonSerialize(): array {
'duration' => $this->getDuration(),
'locked' => $this->getIsLocked(),
'hash' => $this->getPollOptionHash(),
'isOwner' => $this->getIsOwner(),
'votes' => $this->getVotes(),
'owner' => $this->getOwnerUser(),
];
Expand Down Expand Up @@ -151,7 +152,7 @@ public function getOwnerUser(): ?UserBase {
* @return void
*/
public function setOption(int $timestamp = 0, int $duration = 0, string $pollOptionText = '', int $order = 0): void {

if ($timestamp) {
$this->setTimestamp($timestamp);
$this->setDuration($duration);
Expand Down Expand Up @@ -190,7 +191,7 @@ public function syncOption(): void {
// timestamp gets precedence over pollOptionText
if ($this->getTimestamp()) {
$this->setOrder($this->getTimestamp());

if ($this->duration) {
$this->setPollOptionText(date('c', $this->getTimestamp()) . ' - ' . date('c', $this->getTimestamp() + $this->getDuration()));
} else {
Expand All @@ -201,7 +202,7 @@ public function syncOption(): void {
// update hash
$this->updateHash();
}

private function updateHash(): void {
$this->setPollOptionHash(hash('md5', $this->getPollId() . $this->getPollOptionText() . $this->getTimestamp()));
}
Expand Down Expand Up @@ -229,7 +230,7 @@ public function getPollOptionTextEnd(): string {
}
return htmlspecialchars_decode($this->pollOptionText);
}

public function getPollOptionTextStart(): string {
if ($this->getTimestamp()) {
return date('c', $this->getTimestamp());
Expand Down
Loading

0 comments on commit 8ec48c6

Please sign in to comment.