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
24 changes: 15 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "nextcloud/guests",
"config": {
"optimize-autoloader": true,
"classmap-authoritative": true,
"platform": {
"php": "8.1.0"
"php": "8.1"
},
"sort-packages": true,
"allow-plugins": {
"bamarni/composer-bin-plugin": true
},
"sort-packages": true
"optimize-autoloader": true,
"autoloader-suffix": "Guests"
},
"autoload-dev": {
"autoload": {
"psr-4": {
"OCP\\": "vendor/nextcloud/ocp/OCP"
"OCA\\Guests\\": "lib/"
}
},
"scripts": {
Expand All @@ -20,14 +23,17 @@
"psalm": "psalm --threads=1",
"psalm:update-baseline": "psalm --threads=1 --update-baseline",
"psalm:clear": "psalm --clear-cache && psalm --clear-global-cache",
"psalm:fix": "psalm --alter --issues=InvalidReturnType,InvalidNullableReturnType,MissingParamType,InvalidFalsableReturnType",
"psalm:fix": "psalm --alter --issues=InvalidReturnType,InvalidNullableReturnType,MismatchingDocblockParamType,MismatchingDocblockReturnType,MissingParamType,InvalidFalsableReturnType",
"test:unit": "vendor/bin/phpunit -c phpunit.xml"
},
"require": {
"php": ">=8.1 <=8.4"
},
"require-dev": {
"nextcloud/coding-standard": "^1.3.2",
"nextcloud/ocp": "dev-master",
"nextcloud/ocp": "dev-stable30",
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpunit/phpunit": "^9",
"vimeo/psalm": "^5.14"
"vimeo/psalm": "^5.26.1"
}
}
354 changes: 210 additions & 144 deletions composer.lock

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions lib/AppConfigOverwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ public function setOverwrite(array $overwrite): void {
$this->overWrite = $overwrite;
}

/**
* @psalm-suppress MethodSignatureMustProvideReturnType
*/
public function getValue($app, $key, $default = null) {
public function getValue(string $app, string $key, ?string $default = null): string {
if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) {
return $this->overWrite[$app][$key];
}
Expand Down
25 changes: 13 additions & 12 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\AppFramework\IAppContainer;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IGroupManager;
use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Notification\IManager as INotificationManager;
use OCP\Share\Events\ShareCreatedEvent;
use OCP\User\Events\UserChangedEvent;
use OCP\User\Events\UserFirstTimeLoggedInEvent;
use Psr\Container\ContainerInterface;

class Application extends App implements IBootstrap {
public const APP_ID = 'guests';
Expand All @@ -55,26 +55,27 @@ public function register(IRegistrationContext $context): void {
public function boot(IBootContext $context): void {
// need to cheat here since there's no way to register these in IRegistrationContext
$container = $context->getServerContainer();
$container->get(IUserManager::class)->registerBackend($container->query(UserBackend::class));
$container->get(IGroupManager::class)->addBackend($container->query(GroupBackend::class));
$container->get(IUserManager::class)->registerBackend($container->get(UserBackend::class));
$container->get(IGroupManager::class)->addBackend($container->get(GroupBackend::class));

$this->setupGuestManagement($context->getAppContainer(), $context->getServerContainer());
$this->setupGuestRestrictions($context->getAppContainer(), $context->getServerContainer());
$this->setupNotifications($context->getAppContainer());
$context->getAppContainer()->query(RestrictionManager::class)->lateSetupRestrictions();
$context->getAppContainer()->get(RestrictionManager::class)->lateSetupRestrictions();
}

private function setupGuestManagement(IAppContainer $container, IServerContainer $server): void {
$hookManager = $container->query(Hooks::class);
private function setupGuestManagement(ContainerInterface $container, ContainerInterface $server): void {
$hookManager = $container->get(Hooks::class);
$server->get(IEventDispatcher::class)->addListener(ShareCreatedEvent::class, [$hookManager, 'handlePostShare']);
$server->get(IEventDispatcher::class)->addListener(UserFirstTimeLoggedInEvent::class, [$hookManager, 'handleFirstLogin']);
}

private function setupGuestRestrictions(IAppContainer $container, IServerContainer $server): void {
$userSession = $server->getUserSession();
private function setupGuestRestrictions(ContainerInterface $container, ContainerInterface $server): void {
/** @var IUserSession $userSession */
$userSession = $server->get(IUserSession::class);
$user = $userSession->getUser();
/** @var RestrictionManager $restrictionManager */
$restrictionManager = $container->query(RestrictionManager::class);
$restrictionManager = $container->get(RestrictionManager::class);

if ($user) {
$restrictionManager->verifyAccess();
Expand All @@ -87,8 +88,8 @@ private function setupGuestRestrictions(IAppContainer $container, IServerContain
}
}

private function setupNotifications(IAppContainer $container): void {
$notificationManager = $container->query(INotificationManager::class);
private function setupNotifications(ContainerInterface $container): void {
$notificationManager = $container->get(INotificationManager::class);
$notificationManager->registerNotifierService(Notifier::class);
}
}
11 changes: 8 additions & 3 deletions lib/AppWhitelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(
$this->baseUrlLength = strlen($this->baseUrl);
}

public function isAppWhitelisted($appId): bool {
public function isAppWhitelisted(string $appId): bool {
$whitelist = $this->config->getAppWhitelist();
$alwaysEnabled = explode(',', self::WHITELIST_ALWAYS);

Expand All @@ -62,7 +62,10 @@ public function isWhitelistEnabled(): bool {
return $this->config->useWhitelist();
}

public function isUrlAllowed(IUser $user, $url): bool {
/**
* @param false|string $url
*/
public function isUrlAllowed(IUser $user, string|false $url): bool {
if ($this->guestManager->isGuest($user) && $this->isWhitelistEnabled()) {
$app = $this->getRequestedApp($url);

Expand Down Expand Up @@ -95,8 +98,10 @@ public function verifyAccess(IUser $user, IRequest $request): void {
/**
* Core has \OC::$REQUESTEDAPP but it isn't set until the routes are matched
* taken from \OC\Route\Router::match()
*
* @param false|string $url
*/
private function getRequestedApp($url): string {
private function getRequestedApp(string|false $url): string {
if (substr($url, 0, $this->baseUrlLength) === $this->baseUrl) {
$url = substr($url, $this->baseUrlLength);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function allowExternalStorage(): bool {
/**
* @param string|bool $allow
*/
public function setAllowExternalStorage($allow) {
public function setAllowExternalStorage($allow): void {
$this->appConfig->setAppValueBool('allow_external_storage', $allow === true || $allow === 'true') ;
}

Expand All @@ -57,7 +57,7 @@ public function useWhitelist(): bool {
/**
* @param string|bool $use
*/
public function setUseWhitelist($use) {
public function setUseWhitelist($use): void {
$this->appConfig->setAppValueBool('usewhitelist', $use === true || $use === 'true') ;
}

Expand Down Expand Up @@ -94,7 +94,7 @@ public function canCreateGuests(): bool {
|| $this->subAdmin->isSubAdmin($user)) {
return true;
}

// Check if we have a group restriction
// and if the user belong to that group
$groupRestriction = $this->getCreateRestrictedToGroup();
Expand Down
6 changes: 6 additions & 0 deletions lib/FilteredSettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ private function isSettingAllowed(string $setting): bool {
return $this->appWhitelist->isAppWhitelisted($appId);
}

/**
* @return void
*/
public function registerSection(string $type, string $section) {
$this->manager->registerSection($type, $section);
}

/**
* @return void
*/
public function registerSetting(string $type, string $setting) {
if (!$this->isSettingAllowed($setting)) {
return;
Expand Down
4 changes: 2 additions & 2 deletions lib/GroupBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function inGroup($uid, $gid): bool {
* Get all groups a user belongs to
*
* @param string $uid Name of the user
* @return array an array of group names
* @return list<string> an array of group names
* @since 4.5.0
*
* This function fetches all groups a user belongs to. It does not check
Expand Down Expand Up @@ -120,7 +120,7 @@ public function groupExists($gid): bool {
* @param string $search
* @param int $limit
* @param int $offset
* @return array an array of user ids
* @return array<int, string> an array of user ids
* @since 4.5.0
*/
public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0): array {
Expand Down
8 changes: 4 additions & 4 deletions lib/GuestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public function createGuest(?IUser $createdBy, string $userId, string $email, st
// generate token for lost password so that a link can be sent by email
$token = $this->secureRandom->generate(
21,
ISecureRandom::CHAR_DIGITS .
ISecureRandom::CHAR_LOWER .
ISecureRandom::CHAR_UPPER);
ISecureRandom::CHAR_DIGITS
. ISecureRandom::CHAR_LOWER
. ISecureRandom::CHAR_UPPER);

$endOfTime = PHP_INT_MAX - 50000;
$token = sprintf('%s:%s', $endOfTime, $token);
Expand Down Expand Up @@ -149,7 +149,7 @@ private function getShareCountForUsers(array $guests): array {
return $data;
}

public function getGuestInfo($userId): array {
public function getGuestInfo(string $userId): array {
$shares = array_merge(
$this->shareManager->getSharedWith($userId, IShare::TYPE_USER, null, -1, 0),
$this->shareManager->getSharedWith($userId, IShare::TYPE_GROUP, null, -1, 0),
Expand Down
3 changes: 3 additions & 0 deletions lib/Listener/BeforeUserManagementRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* @template-implements IEventListener<BeforeTemplateRenderedEvent>
*/
class BeforeUserManagementRenderedListener implements IEventListener {
/**
* @param Event $event
*/
public function handle(Event $event): void {
if (!($event instanceof BeforeTemplateRenderedEvent)) {
return;
Expand Down
3 changes: 3 additions & 0 deletions lib/Listener/LoadAdditionalScriptsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function __construct(
) {
}

/**
* @param Event $event
*/
public function handle(Event $event): void {
// If the user cannot create guests, we don't need to load the script
if (!$this->config->canCreateGuests()) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public function sendGuestInviteMail(string $uid, string $shareWith, Share\IShare
$message->setHtmlBody($emailTemplate->renderHtml());
$message->setPlainBody($emailTemplate->renderText());
$message->setFrom([
Util::getDefaultEmailAddress('sharing-noreply') =>
$l10n->t('%s via %s', [
Util::getDefaultEmailAddress('sharing-noreply')
=> $l10n->t('%s via %s', [
$senderDisplayName,
$this->defaults->getName()
]),
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/OwncloudGuestsMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function shouldRun(): bool {
* 4. Delete the old 'isGuest' preference row
* 5. Set the quota to '0 B'
*/
protected function runStep(IOutput $output) {
protected function runStep(IOutput $output): void {
$output->startProgress(count($this->cachedUserIDs));
foreach ($this->cachedUserIDs as $userID) {
if ($ocGuest = $this->userManager->get($userID)) {
Expand Down
7 changes: 5 additions & 2 deletions lib/Repair/ResetEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ public function getName(): string {
return 'Reset the email of all guest accounts';
}

/**
* @return void
*/
public function run(IOutput $output) {
foreach ($this->guestManager->listGuests() as $guestId) {
$guest = $this->userManager->get($guestId);
if (strtolower($guest->getSystemEMailAddress() ?? '') !== strtolower($guestId)) {
$this->config->setUserValue($guestId, 'guests', 'old_email', $guest->getSystemEMailAddress());
if (strtolower($guest?->getSystemEMailAddress() ?? '') !== strtolower($guestId)) {
$this->config->setUserValue($guestId, 'guests', 'old_email', $guest?->getSystemEMailAddress() ?? '');
$guest->setSystemEMailAddress(strtolower($guestId));
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/UserBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(
$this->cache = new CappedMemoryCache();
}

public function setAllowListing(bool $allow) {
public function setAllowListing(bool $allow): void {
$this->allowListing = $allow;
}

Expand Down
Loading
Loading