Skip to content

Commit 64857ac

Browse files
committed
refactor: Use UserSession::completeLogin in OC_User
Allow to get rid of more legacy code and make sure UserSessionTest is also in psalm Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent e554f7e commit 64857ac

8 files changed

Lines changed: 96 additions & 122 deletions

File tree

build/psalm-baseline.xml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
<code><![CDATA[getAppValue]]></code>
3131
</DeprecatedMethod>
3232
</file>
33+
<file src="apps/cloud_federation_api/lib/Controller/RequestHandlerController.php">
34+
<DeprecatedMethod>
35+
<code><![CDATA[getFederationIdFromSharedSecret]]></code>
36+
</DeprecatedMethod>
37+
</file>
3338
<file src="apps/comments/lib/Activity/Listener.php">
3439
<DeprecatedConstant>
3540
<code><![CDATA[CommentsEvent::EVENT_ADD]]></code>
@@ -3663,11 +3668,6 @@
36633668
<code><![CDATA[$value]]></code>
36643669
</MoreSpecificImplementedParamType>
36653670
</file>
3666-
<file src="lib/private/Session/Memory.php">
3667-
<MoreSpecificImplementedParamType>
3668-
<code><![CDATA[$value]]></code>
3669-
</MoreSpecificImplementedParamType>
3670-
</file>
36713671
<file src="lib/private/Setup.php">
36723672
<RedundantCondition>
36733673
<code><![CDATA[$type === 'pdo']]></code>
@@ -3763,11 +3763,6 @@
37633763
<code><![CDATA[getQuota]]></code>
37643764
</UndefinedInterfaceMethod>
37653765
</file>
3766-
<file src="lib/private/legacy/OC_User.php">
3767-
<UndefinedClass>
3768-
<code><![CDATA[\Test\Util\User\Dummy]]></code>
3769-
</UndefinedClass>
3770-
</file>
37713766
<file src="lib/public/AppFramework/Http/Response.php">
37723767
<LessSpecificReturnStatement>
37733768
<code><![CDATA[array_merge($mergeWith, $this->headers)]]></code>

lib/private/Server.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace OC;
99

1010
use bantu\IniGetWrapper\IniGetWrapper;
11+
use lib\private\Files\Listeners\UserMountCacheListener;
1112
use OC\Accounts\AccountManager;
1213
use OC\Activity\EventMerger;
1314
use OC\App\AppManager;
@@ -529,6 +530,8 @@ public function __construct(
529530
$c->get('LockdownManager'),
530531
$c->get(LoggerInterface::class),
531532
$c->get(IEventDispatcher::class),
533+
$c,
534+
$c->get(IRootFolder::class),
532535
);
533536
$dispatcher = $this->get(IEventDispatcher::class);
534537
$dispatcher->addListener(UserLoggedInEvent::class, function (UserLoggedInEvent $event) {
@@ -749,11 +752,11 @@ public function __construct(
749752

750753
return new DateTimeFormatter(
751754
$c->get(IDateTimeZone::class)->getTimeZone(),
752-
$c->getL10N('lib', $language)
755+
$c->get(IFactory::class)->get('lib', $language)
753756
);
754757
});
755758

756-
$this->registerService(IUserMountCache::class, function (ContainerInterface $c) {
759+
$this->registerService(IUserMountCache::class, function (ContainerInterface $c): IUserMountCache {
757760
$mountCache = $c->get(UserMountCache::class);
758761
/** @var IEventDispatcher $eventDispatcher */
759762
$eventDispatcher = $c->get(IEventDispatcher::class);

lib/private/Session/Memory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Memory extends Session {
2222

2323
/**
2424
* @param string $key
25-
* @param integer $value
25+
* @param mixed $value
2626
*/
2727
public function set(string $key, $value) {
2828
$this->data[$key] = $value;

lib/private/User/Session.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
use OC\Http\CookieHelper;
2121
use OC\Security\CSRF\CsrfTokenManager;
2222
use OC_User;
23-
use OC_Util;
2423
use OCA\DAV\Connector\Sabre\Auth;
2524
use OCP\AppFramework\Db\TTransactional;
2625
use OCP\AppFramework\Utility\ITimeFactory;
2726
use OCP\Authentication\Exceptions\ExpiredTokenException;
2827
use OCP\Authentication\Exceptions\InvalidTokenException;
2928
use OCP\EventDispatcher\GenericEvent;
3029
use OCP\EventDispatcher\IEventDispatcher;
30+
use OCP\Files\IRootFolder;
31+
use OCP\Files\ISetupManager;
3132
use OCP\Files\NotPermittedException;
3233
use OCP\IConfig;
3334
use OCP\IDBConnection;
@@ -48,6 +49,7 @@
4849
use OCP\User\Events\UserLoggedInWithCookieEvent;
4950
use OCP\User\Events\UserLoggedOutEvent;
5051
use OCP\Util;
52+
use Psr\Container\ContainerInterface;
5153
use Psr\Log\LoggerInterface;
5254

5355
/**
@@ -67,8 +69,7 @@
6769
class Session implements IUserSession, Emitter {
6870
use TTransactional;
6971

70-
/** @var User $activeUser */
71-
protected $activeUser;
72+
protected ?User $activeUser = null;
7273

7374
public function __construct(
7475
private Manager $manager,
@@ -80,6 +81,8 @@ public function __construct(
8081
private ILockdownManager $lockdownManager,
8182
private LoggerInterface $logger,
8283
private IEventDispatcher $dispatcher,
84+
private ContainerInterface $container,
85+
private IRootFolder $rootFolder,
8386
) {
8487
}
8588

@@ -516,24 +519,23 @@ protected function prepareUserLogin($firstTimeLogin, $refreshCsrfToken = true) {
516519
}
517520

518521
if ($firstTimeLogin) {
519-
//we need to pass the user name, which may differ from login name
520-
$user = $this->getUser()->getUID();
521-
OC_Util::setupFS($user);
522+
//we need to pass the username, which may differ from login name
523+
$user = $this->getUser();
524+
$this->container->get(ISetupManager::class)->setupForUser($user);
522525

523-
// TODO: lock necessary?
524-
//trigger creation of user home and /files folder
525-
$userFolder = \OC::$server->getUserFolder($user);
526+
// trigger creation of user home and /files folder
527+
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
526528

527529
try {
528530
// copy skeleton
529-
\OC_Util::copySkeleton($user, $userFolder);
530-
} catch (NotPermittedException $ex) {
531+
\OC_Util::copySkeleton($user->getUID(), $userFolder);
532+
} catch (NotPermittedException) {
531533
// read only uses
532534
}
533535

534536
// trigger any other initialization
535-
Server::get(IEventDispatcher::class)->dispatch(IUser::class . '::firstLogin', new GenericEvent($this->getUser()));
536-
Server::get(IEventDispatcher::class)->dispatchTyped(new UserFirstTimeLoggedInEvent($this->getUser()));
537+
$this->dispatcher->dispatch(IUser::class . '::firstLogin', new GenericEvent($user));
538+
$this->dispatcher->dispatchTyped(new UserFirstTimeLoggedInEvent($user));
537539
}
538540
}
539541

lib/private/legacy/OC_User.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use OCP\Authentication\IProvideUserSecretBackend;
1717
use OCP\Authentication\Token\IToken;
1818
use OCP\EventDispatcher\IEventDispatcher;
19-
use OCP\Files\IRootFolder;
2019
use OCP\IGroupManager;
2120
use OCP\IRequest;
2221
use OCP\ISession;
@@ -150,8 +149,6 @@ public static function loginWithApache(IApacheBackend $backend): bool {
150149
self::setUserId($uid);
151150
/** @var Session $userSession */
152151
$userSession = Server::get(IUserSession::class);
153-
154-
/** @var IEventDispatcher $dispatcher */
155152
$dispatcher = Server::get(IEventDispatcher::class);
156153

157154
if ($userSession->getUser() && !$userSession->getUser()->isEnabled()) {
@@ -165,11 +162,12 @@ public static function loginWithApache(IApacheBackend $backend): bool {
165162
$password = $backend->getCurrentUserSecret();
166163
}
167164

168-
/** @var IEventDispatcher $dispatcher */
169165
$dispatcher->dispatchTyped(new BeforeUserLoggedInEvent($uid, $password, $backend));
170166

167+
$user = $userSession->getUser();
168+
$userSession->completeLogin($user, ['loginName' => $uid, 'password' => $password]);
171169
$userSession->createSessionToken($request, $uid, $uid, $password);
172-
$userSession->createRememberMeToken($userSession->getUser());
170+
$userSession->createRememberMeToken($user);
173171

174172
if (empty($password)) {
175173
$tokenProvider = Server::get(IProvider::class);
@@ -186,11 +184,6 @@ public static function loginWithApache(IApacheBackend $backend): bool {
186184
}
187185
}
188186

189-
$user = Server::get(IUserManager::class)->get($uid);
190-
191-
// set up the filesystem
192-
Server::get(\OCP\Files\ISetupManager::class)->setupForUser($user);
193-
194187
// first call the UserLoggedIn event, the login-process needs to be
195188
// completed before we can safely create the user's folder.
196189
// For example encryption needs to initialize the users keys first
@@ -201,9 +194,6 @@ public static function loginWithApache(IApacheBackend $backend): bool {
201194
null,
202195
false)
203196
);
204-
205-
// trigger creation of user home and /files folder
206-
Server::get(IRootFolder::class)->getUserFolder($uid);
207197
}
208198
return true;
209199
}

psalm.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
<file name="status.php"/>
6464
<file name="version.php"/>
6565
<file name="tests/lib/TestCase.php"/>
66+
<file name="tests/lib/Util/User/Dummy.php"/>
67+
<file name="tests/lib/User/SessionTest.php"/>
6668
<ignoreFiles>
6769
<directory name="apps/**/tests"/>
6870
<directory name="apps/**/composer"/>
@@ -172,6 +174,7 @@
172174
<DeprecatedClass>
173175
<errorLevel type="suppress">
174176
<directory name="lib" />
177+
<directory name="tests" />
175178
<directory name="apps/*/tests" />
176179
</errorLevel>
177180
</DeprecatedClass>

0 commit comments

Comments
 (0)