Use non-deprecated QBMapper

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-07-13 15:59:39 +02:00
parent 122d6ee11d
commit 221edf0c20
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
2 changed files with 76 additions and 23 deletions

View File

@ -1,4 +1,6 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net> * @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
* *
@ -25,6 +27,24 @@ namespace OCA\Registration\Db;
use OCP\AppFramework\Db\Entity; use OCP\AppFramework\Db\Entity;
/**
* @method string getEmail()
* @method void setEmail(string $email)
* @method string getUsername()
* @method void setUsername(string $username)
* @method string getPassword()
* @method void setPassword(string $password)
* @method string getDisplayname()
* @method void setDisplayname(string $displayname)
* @method bool isEmailConfirmed()
* @method void setEmailConfirmed(bool $emailConfirmed)
* @method string getToken()
* @method void setToken(string $token)
* @method string getClientSecret()
* @method void setClientSecret(string $clientSecret)
* @method string getRequested()
* @method void setRequested(string $requested)
*/
class Registration extends Entity { class Registration extends Entity {
public $id; public $id;
protected $email; protected $email;
@ -37,6 +57,13 @@ class Registration extends Entity {
protected $clientSecret; protected $clientSecret;
public function __construct() { public function __construct() {
$this->addType('email', 'string');
$this->addType('username', 'string');
$this->addType('password', 'string');
$this->addType('displayname', 'string');
$this->addType('emailConfirmed', 'boolean'); $this->addType('emailConfirmed', 'boolean');
$this->addType('token', 'string');
$this->addType('clientSecret', 'string');
$this->addType('requested', 'string'); // TODO datetime is not supported?
} }
} }

View File

@ -1,4 +1,6 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net> * @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
* *
@ -25,13 +27,13 @@ namespace OCA\Registration\Db;
use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity; use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\Mapper; use OCP\AppFramework\Db\QBMapper;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
class RegistrationMapper extends Mapper { class RegistrationMapper extends QBMapper {
/** @var \OCP\Security\ISecureRandom */ /** @var ISecureRandom */
protected $random; protected $random;
public function __construct(IDBConnection $db, ISecureRandom $random) { public function __construct(IDBConnection $db, ISecureRandom $random) {
@ -40,23 +42,43 @@ class RegistrationMapper extends Mapper {
} }
/** /**
* @param $token * @param string $token
* @return Registration|Entity * @return Registration
* @throws DoesNotExistException
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
*/ */
public function findByToken($token) { public function findByToken(string $token): Entity {
return $this->findEntity('SELECT * FROM `*PREFIX*registration` WHERE `token` = ? ', [$token]); $query = $this->db->getQueryBuilder();
$query->select('*')
->from($this->getTableName())
->where($query->expr()->eq('token', $query->createNamedParameter($token)));
return $this->findEntity($query);
} }
public function findBySecret($secret) { /**
return $this->findEntity('SELECT * FROM `*PREFIX*registration` WHERE `client_secret` = ? ', [$secret]); * @param string $secret
* @return Registration
* @throws DoesNotExistException
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
*/
public function findBySecret(string $secret): Entity {
$query = $this->db->getQueryBuilder();
$query->select('*')
->from($this->getTableName())
->where($query->expr()->eq('client_secret', $query->createNamedParameter($secret)));
return $this->findEntity($query);
} }
public function usernameIsPending($username) { public function usernameIsPending(string $username): bool {
try { try {
$entity = $this->findEntity( $query = $this->db->getQueryBuilder();
'SELECT id FROM `*PREFIX*registration` WHERE `username` = ? ', $query->select('*')
[$username] ->from($this->getTableName())
); ->where($query->expr()->eq('username', $query->createNamedParameter($username)));
$this->findEntity($query);
} catch (DoesNotExistException $e) { } catch (DoesNotExistException $e) {
return false; return false;
} }
@ -64,19 +86,23 @@ class RegistrationMapper extends Mapper {
} }
/** /**
* @param $email * @param string $email
* @return Registration|Entity * @return Registration
*/ */
public function find($email) { public function find(string $email): Entity {
$sql = 'SELECT * FROM `*PREFIX*registration` WHERE `email` = ? '; $query = $this->db->getQueryBuilder();
return $this->findEntity($sql, [$email]); $query->select('*')
->from($this->getTableName())
->where($query->expr()->eq('email', $query->createNamedParameter($email)));
return $this->findEntity($query);
} }
/** /**
* @param Entity $entity * @param Entity $entity
* @return Entity * @return Registration
*/ */
public function insert(Entity $entity) { public function insert(Entity $entity): Entity {
$entity->setRequested(date('Y-m-d H:i:s')); $entity->setRequested(date('Y-m-d H:i:s'));
return parent::insert($entity); return parent::insert($entity);
} }
@ -84,7 +110,7 @@ class RegistrationMapper extends Mapper {
/** /**
* @param Registration $registration * @param Registration $registration
*/ */
public function generateNewToken(Registration $registration) { public function generateNewToken(Registration $registration): void {
$token = $this->random->generate(10, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); $token = $this->random->generate(10, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
$registration->setToken($token); $registration->setToken($token);
} }
@ -92,7 +118,7 @@ class RegistrationMapper extends Mapper {
/** /**
* @param Registration $registration * @param Registration $registration
*/ */
public function generateClientSecret(Registration $registration) { public function generateClientSecret(Registration $registration): void {
$token = $this->random->generate(32, 'abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789'); $token = $this->random->generate(32, 'abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789');
//FIXME eqivalent to ISecureRandom::CHAR_HUMAN_READABLE introduced in https://github.com/nextcloud/server/commit/f2a2b34e4639e88f8d948a388a51f010212b42a3 but not supported in ownCloud yet. We'll just use the string for now then switch to constants when supported. //FIXME eqivalent to ISecureRandom::CHAR_HUMAN_READABLE introduced in https://github.com/nextcloud/server/commit/f2a2b34e4639e88f8d948a388a51f010212b42a3 but not supported in ownCloud yet. We'll just use the string for now then switch to constants when supported.
$registration->setClientSecret($token); $registration->setClientSecret($token);