88 */
99namespace OC \User ;
1010
11+ use InvalidArgumentException ;
1112use OCP \AppFramework \Db \TTransactional ;
1213use OCP \Cache \CappedMemoryCache ;
1314use OCP \EventDispatcher \IEventDispatcher ;
2122use OCP \User \Backend \IGetDisplayNameBackend ;
2223use OCP \User \Backend \IGetHomeBackend ;
2324use OCP \User \Backend \IGetRealUIDBackend ;
25+ use OCP \User \Backend \IPasswordHashBackend ;
2426use OCP \User \Backend \ISearchKnownUsersBackend ;
2527use OCP \User \Backend \ISetDisplayNameBackend ;
2628use OCP \User \Backend \ISetPasswordBackend ;
@@ -37,7 +39,8 @@ class Database extends ABackend implements
3739 IGetHomeBackend,
3840 ICountUsersBackend,
3941 ISearchKnownUsersBackend,
40- IGetRealUIDBackend {
42+ IGetRealUIDBackend,
43+ IPasswordHashBackend {
4144 /** @var CappedMemoryCache */
4245 private $ cache ;
4346
@@ -176,6 +179,40 @@ public function setPassword(string $uid, string $password): bool {
176179 return false ;
177180 }
178181
182+ public function getPasswordHash (string $ userId ): ?string {
183+ $ this ->fixDI ();
184+ if (!$ this ->userExists ($ userId )) {
185+ return null ;
186+ }
187+ if (!empty ($ this ->cache [$ userId ]['password ' ])) {
188+ return $ this ->cache [$ userId ]['password ' ];
189+ }
190+ $ qb = $ this ->dbConn ->getQueryBuilder ();
191+ $ qb ->select ('password ' )
192+ ->from ($ this ->table )
193+ ->where ($ qb ->expr ()->eq ('uid_lower ' , $ qb ->createNamedParameter (mb_strtolower ($ userId ))));
194+ /** @var false|string $hash */
195+ $ hash = $ qb ->executeQuery ()->fetchOne ();
196+ if ($ hash === false ) {
197+ return null ;
198+ }
199+ $ this ->cache [$ userId ]['password ' ] = $ hash ;
200+ return $ hash ;
201+ }
202+
203+ public function setPasswordHash (string $ userId , string $ passwordHash ): bool {
204+ if (!\OCP \Server::get (IHasher::class)->validate ($ passwordHash )) {
205+ throw new InvalidArgumentException ();
206+ }
207+ $ this ->fixDI ();
208+ $ result = $ this ->updatePassword ($ userId , $ passwordHash );
209+ if (!$ result ) {
210+ return false ;
211+ }
212+ $ this ->cache [$ userId ]['password ' ] = $ passwordHash ;
213+ return true ;
214+ }
215+
179216 /**
180217 * Set display name
181218 *
0 commit comments