142 lines
4.5 KiB
PHP
Executable File
142 lines
4.5 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* FuzeWorks Authentication Plugin.
|
|
*
|
|
* The FuzeWorks PHP FrameWork
|
|
*
|
|
* Copyright (C) 2013 - 2023 i15
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
* @author i15
|
|
* @copyright Copyright (C) 2013 - 2023, i15. (https://i15.nl)
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
*
|
|
* @since Version 1.3.0
|
|
*
|
|
* @version Version 1.3.0
|
|
*/
|
|
|
|
namespace FuzeWorks\Authentication\Drivers;
|
|
|
|
use FuzeWorks\Authentication\Model\User;
|
|
use FuzeWorks\Authentication\UsersModelDriverInterface;
|
|
use FuzeWorks\DatabaseEngine\iDatabaseEngine;
|
|
use FuzeWorks\DatabaseEngine\MongoEngine;
|
|
use MongoDB\Collection;
|
|
|
|
class MongoUsersModelDriver implements UsersModelDriverInterface
|
|
{
|
|
|
|
/** @var MongoEngine */
|
|
protected iDatabaseEngine $engine;
|
|
protected Collection $collection;
|
|
|
|
public function __construct(iDatabaseEngine $engine)
|
|
{
|
|
$this->engine = $engine;
|
|
$this->collection = $this->engine->selectDatabase("auth")->selectCollection("users");
|
|
}
|
|
|
|
/**
|
|
* @param User[] $users
|
|
* @return bool
|
|
*/
|
|
public function createUsers(array $users): bool
|
|
{
|
|
$res = $this->collection->insertMany($users);
|
|
if ($res->getInsertedCount() == count($users))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
public function readUsers(array $filter, array $options = []): array
|
|
{
|
|
// Process options
|
|
$opt = [];
|
|
if (isset($options['limit']))
|
|
$opt['limit'] = $options['limit'];
|
|
if (isset($options['index']))
|
|
$opt['skip'] = $options['index'];
|
|
|
|
$results = $this->collection->find($filter, $opt);
|
|
$out = [];
|
|
foreach ($results as $result)
|
|
{
|
|
$user = new User($result->id);
|
|
if (isset($result->primaryEmail))
|
|
$user->primaryEmail = $result->primaryEmail;
|
|
if (isset($result->password))
|
|
$user->password = $result->password;
|
|
if (isset($result->permissions))
|
|
$user->permissions = (array) $result->permissions;
|
|
if (isset($result->active))
|
|
$user->active = (bool) $result->active;
|
|
if (isset($result->emailVerifyToken))
|
|
$user->emailVerifyToken = $result->emailVerifyToken;
|
|
if (isset($result->emailVerifyExpiry))
|
|
$user->emailVerifyExpiry = $result->emailVerifyExpiry;
|
|
if (isset($result->properties))
|
|
$user->properties = (array) $result->properties;
|
|
|
|
$out[] = $user;
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
|
|
/**
|
|
* @param User[] $users
|
|
* @return bool
|
|
*/
|
|
public function updateUsers(array $users): bool
|
|
{
|
|
$modified = 0;
|
|
foreach ($users as $user)
|
|
{
|
|
$res = $this->collection->updateOne(["id" => $user->id], ['$set' => $user]);
|
|
$modified += $res->getModifiedCount();
|
|
}
|
|
|
|
if ($modified === count($users))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @param User[] $users
|
|
* @return bool
|
|
*/
|
|
public function deleteUsers(array $users): bool
|
|
{
|
|
$deleted = 0;
|
|
foreach ($users as $user)
|
|
{
|
|
$res = $this->collection->deleteOne(["id" => $user->id]);
|
|
$deleted += $res->getDeletedCount();
|
|
}
|
|
|
|
if ($deleted === count($users))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
} |