Skip to content

Commit 62a3ef5

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 1550f50 commit 62a3ef5

8 files changed

Lines changed: 87 additions & 113 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>
@@ -4078,11 +4083,6 @@
40784083
<code><![CDATA[$value]]></code>
40794084
</MoreSpecificImplementedParamType>
40804085
</file>
4081-
<file src="lib/private/Session/Memory.php">
4082-
<MoreSpecificImplementedParamType>
4083-
<code><![CDATA[$value]]></code>
4084-
</MoreSpecificImplementedParamType>
4085-
</file>
40864086
<file src="lib/private/Setup.php">
40874087
<RedundantCondition>
40884088
<code><![CDATA[$type === 'pdo']]></code>
@@ -4188,11 +4188,6 @@
41884188
<code><![CDATA[getQuota]]></code>
41894189
</UndefinedInterfaceMethod>
41904190
</file>
4191-
<file src="lib/private/legacy/OC_User.php">
4192-
<UndefinedClass>
4193-
<code><![CDATA[\Test\Util\User\Dummy]]></code>
4194-
</UndefinedClass>
4195-
</file>
41964191
<file src="lib/public/AppFramework/Http/Response.php">
41974192
<LessSpecificReturnStatement>
41984193
<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
#[\Override]
2828
public function set(string $key, $value) {

lib/private/User/Session.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OCP\Authentication\Exceptions\InvalidTokenException;
2828
use OCP\EventDispatcher\GenericEvent;
2929
use OCP\EventDispatcher\IEventDispatcher;
30+
use OCP\Files\IRootFolder;
3031
use OCP\IConfig;
3132
use OCP\IDBConnection;
3233
use OCP\IRequest;
@@ -46,6 +47,7 @@
4647
use OCP\User\Events\UserLoggedInWithCookieEvent;
4748
use OCP\User\Events\UserLoggedOutEvent;
4849
use OCP\Util;
50+
use Psr\Container\ContainerInterface;
4951
use Psr\Log\LoggerInterface;
5052

5153
/**
@@ -65,8 +67,7 @@
6567
class Session implements IUserSession, Emitter {
6668
use TTransactional;
6769

68-
/** @var User $activeUser */
69-
protected $activeUser;
70+
protected ?User $activeUser = null;
7071

7172
public function __construct(
7273
private Manager $manager,
@@ -78,6 +79,8 @@ public function __construct(
7879
private ILockdownManager $lockdownManager,
7980
private LoggerInterface $logger,
8081
private IEventDispatcher $dispatcher,
82+
private ContainerInterface $container,
83+
private IRootFolder $rootFolder,
8184
) {
8285
}
8386

@@ -524,8 +527,8 @@ protected function prepareUserLogin($firstTimeLogin, $refreshCsrfToken = true) {
524527

525528
if ($firstTimeLogin) {
526529
// trigger any other initialization
527-
Server::get(IEventDispatcher::class)->dispatch(IUser::class . '::firstLogin', new GenericEvent($this->getUser()));
528-
Server::get(IEventDispatcher::class)->dispatchTyped(new UserFirstTimeLoggedInEvent($this->getUser()));
530+
$this->dispatcher->dispatch(IUser::class . '::firstLogin', new GenericEvent($this->getUser()));
531+
$this->dispatcher->dispatchTyped(new UserFirstTimeLoggedInEvent($this->getUser()));
529532
}
530533
}
531534

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
@@ -67,6 +67,8 @@
6767
<file name="version.php"/>
6868
<file name="tests/lib/TestCase.php"/>
6969
<file name="tests/lib/Files/Template/*.php"/>
70+
<file name="tests/lib/Util/User/Dummy.php"/>
71+
<file name="tests/lib/User/SessionTest.php"/>
7072
<ignoreFiles>
7173
<directory name="apps/**/tests"/>
7274
<directory name="apps/**/composer"/>
@@ -177,6 +179,7 @@
177179
<DeprecatedClass>
178180
<errorLevel type="suppress">
179181
<directory name="lib" />
182+
<directory name="tests" />
180183
<directory name="apps/*/tests" />
181184
</errorLevel>
182185
</DeprecatedClass>

0 commit comments

Comments
 (0)