From 221edf0c200d36a86ddfce95e1b9bc9f77c1f874 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 13 Jul 2020 15:59:39 +0200 Subject: [PATCH 1/2] Use non-deprecated QBMapper Signed-off-by: Joas Schilling --- lib/Db/Registration.php | 27 +++++++++++++ lib/Db/RegistrationMapper.php | 72 ++++++++++++++++++++++++----------- 2 files changed, 76 insertions(+), 23 deletions(-) diff --git a/lib/Db/Registration.php b/lib/Db/Registration.php index f9899b4..9d9d4a5 100644 --- a/lib/Db/Registration.php +++ b/lib/Db/Registration.php @@ -1,4 +1,6 @@ * @@ -25,6 +27,24 @@ namespace OCA\Registration\Db; 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 { public $id; protected $email; @@ -37,6 +57,13 @@ class Registration extends Entity { protected $clientSecret; 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('token', 'string'); + $this->addType('clientSecret', 'string'); + $this->addType('requested', 'string'); // TODO datetime is not supported? } } diff --git a/lib/Db/RegistrationMapper.php b/lib/Db/RegistrationMapper.php index c2be0e6..56d2a75 100644 --- a/lib/Db/RegistrationMapper.php +++ b/lib/Db/RegistrationMapper.php @@ -1,4 +1,6 @@ * @@ -25,13 +27,13 @@ namespace OCA\Registration\Db; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\Entity; -use OCP\AppFramework\Db\Mapper; +use OCP\AppFramework\Db\QBMapper; use OCP\IDBConnection; use OCP\Security\ISecureRandom; -class RegistrationMapper extends Mapper { +class RegistrationMapper extends QBMapper { - /** @var \OCP\Security\ISecureRandom */ + /** @var ISecureRandom */ protected $random; public function __construct(IDBConnection $db, ISecureRandom $random) { @@ -40,23 +42,43 @@ class RegistrationMapper extends Mapper { } /** - * @param $token - * @return Registration|Entity + * @param string $token + * @return Registration + * @throws DoesNotExistException + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException */ - public function findByToken($token) { - return $this->findEntity('SELECT * FROM `*PREFIX*registration` WHERE `token` = ? ', [$token]); + public function findByToken(string $token): Entity { + $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 { - $entity = $this->findEntity( - 'SELECT id FROM `*PREFIX*registration` WHERE `username` = ? ', - [$username] - ); + $query = $this->db->getQueryBuilder(); + $query->select('*') + ->from($this->getTableName()) + ->where($query->expr()->eq('username', $query->createNamedParameter($username))); + + $this->findEntity($query); } catch (DoesNotExistException $e) { return false; } @@ -64,19 +86,23 @@ class RegistrationMapper extends Mapper { } /** - * @param $email - * @return Registration|Entity + * @param string $email + * @return Registration */ - public function find($email) { - $sql = 'SELECT * FROM `*PREFIX*registration` WHERE `email` = ? '; - return $this->findEntity($sql, [$email]); + public function find(string $email): Entity { + $query = $this->db->getQueryBuilder(); + $query->select('*') + ->from($this->getTableName()) + ->where($query->expr()->eq('email', $query->createNamedParameter($email))); + + return $this->findEntity($query); } /** * @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')); return parent::insert($entity); } @@ -84,7 +110,7 @@ class RegistrationMapper extends Mapper { /** * @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); $registration->setToken($token); } @@ -92,7 +118,7 @@ class RegistrationMapper extends Mapper { /** * @param Registration $registration */ - public function generateClientSecret(Registration $registration) { + public function generateClientSecret(Registration $registration): void { $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. $registration->setClientSecret($token); From ffa02627c633070940b5d5f695ee68495ac88407 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 13 Jul 2020 16:54:28 +0200 Subject: [PATCH 2/2] Use backwards compatible "getX" for boolean value for now Signed-off-by: Joas Schilling --- lib/Db/Registration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Db/Registration.php b/lib/Db/Registration.php index 9d9d4a5..46dbbb1 100644 --- a/lib/Db/Registration.php +++ b/lib/Db/Registration.php @@ -36,7 +36,7 @@ use OCP\AppFramework\Db\Entity; * @method void setPassword(string $password) * @method string getDisplayname() * @method void setDisplayname(string $displayname) - * @method bool isEmailConfirmed() + * @method bool getEmailConfirmed() * @method void setEmailConfirmed(bool $emailConfirmed) * @method string getToken() * @method void setToken(string $token)