Initial xentral_oss_20.3.c9ffacf

This commit is contained in:
Alex
2021-05-21 08:49:41 +02:00
parent 5406e4a551
commit 34e5ac43d9
18884 changed files with 2109867 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace Xentral\Modules\User;
use Xentral\Core\DependencyInjection\ContainerInterface;
use Xentral\Modules\User\Service\UserConfigService;
use Xentral\Modules\User\Service\UserPermissionService;
final class Bootstrap
{
/**
* @return array
*/
public static function registerServices()
{
return [
'UserConfigService' => 'onInitUserConfigService',
'UserPermissionService' => 'onInitUserPermissionService',
];
}
/**
* @param ContainerInterface $container
*
* @return UserConfigService
*/
public static function onInitUserConfigService(ContainerInterface $container)
{
return new UserConfigService($container->get('Database'));
}
/**
* @param ContainerInterface $container
*
* @return UserPermissionService
*/
public static function onInitUserPermissionService(ContainerInterface $container)
{
return new UserPermissionService($container->get('Database'));
}
}
@@ -0,0 +1,7 @@
<?php
namespace Xentral\Modules\User\Exception;
class InvalidArgumentException extends \InvalidArgumentException implements UserExceptionInterface
{
}
@@ -0,0 +1,7 @@
<?php
namespace Xentral\Modules\User\Exception;
class UserConfigKeyNotFoundException extends \InvalidArgumentException implements UserExceptionInterface
{
}
@@ -0,0 +1,9 @@
<?php
namespace Xentral\Modules\User\Exception;
use Xentral\Core\Exception\ModuleExceptionInterface;
interface UserExceptionInterface extends ModuleExceptionInterface
{
}
@@ -0,0 +1,232 @@
<?php
namespace Xentral\Modules\User\Service;
use Xentral\Components\Database\Database;
use Xentral\Modules\User\Exception\InvalidArgumentException;
use Xentral\Modules\User\Exception\UserConfigKeyNotFoundException;
final class UserConfigService
{
/** @var Database $db */
private $db;
/**
* @param Database $database
* @param Database $database
*/
public function __construct(Database $database)
{
$this->db = $database;
}
/**
* @param string $key
* @param int $userId
*
* @throws InvalidArgumentException
*
* @return bool
*/
public function has($key, $userId)
{
$key = $this->ensureKey($key);
$userId = $this->ensureUserId($userId);
$value = $this->db->fetchValue(
'SELECT k.value FROM userkonfiguration AS k
WHERE k.user = :user_id AND k.name = :key',
['user_id' => $userId, 'key' => $key]
);
return $value !== false;
}
/**
* @param string $key
* @param int $userId
*
* @throws UserConfigKeyNotFoundException
*
* @return mixed
*/
public function get($key, $userId)
{
$key = $this->ensureKey($key);
$userId = $this->ensureUserId($userId);
$value = $this->db->fetchValue(
'SELECT k.value FROM userkonfiguration AS k
WHERE k.user = :user_id AND k.name = :key
LIMIT 1',
['user_id' => $userId, 'key' => $key]
);
if ($value === false) {
throw new UserConfigKeyNotFoundException(sprintf(
'User config key "%s" not found for user "%s".',
$key,
$userId
));
}
return $this->transformReturnValue($value);
}
/**
* Like $this->get(); but no exception if key does not exists
*
* @param string $key
* @param int $userId
*
* @return string|null null if config key does not exists
*/
public function tryGet($key, $userId)
{
$key = $this->ensureKey($key);
$userId = $this->ensureUserId($userId);
$value = $this->db->fetchValue(
'SELECT k.value FROM userkonfiguration AS k
WHERE k.user = :user_id AND k.name = :key',
['user_id' => $userId, 'key' => $key]
);
if ($value === false) {
return null;
}
return $this->transformReturnValue($value);
}
/**
* @param string $key
* @param mixed $value
* @param int $userId
*
* @throws InvalidArgumentException
*
* @return void
*/
public function set($key, $value, $userId)
{
$key = $this->ensureKey($key);
$userId = $this->ensureUserId($userId);
$value = $this->transformToDatabaseValue($value);
if ($this->has($key, $userId)) {
$this->db->perform(
'UPDATE userkonfiguration SET `value` = :value
WHERE `name` = :key AND `user` = :user_id LIMIT 1',
['key' => $key, 'value' => $value, 'user_id' => $userId]
);
} else {
$this->db->perform(
'INSERT INTO userkonfiguration (`id`, `user`, `name`, `value`)
VALUES (NULL, :user_id, :key, :value)',
['key' => $key, 'value' => $value, 'user_id' => $userId]
);
}
}
/**
* @param string $key
* @param int $userId
*
* @throws InvalidArgumentException
*
* @return void
*/
public function delete($key, $userId)
{
$key = $this->ensureKey($key);
$userId = $this->ensureUserId($userId);
$this->db->perform(
'DELETE FROM userkonfiguration
WHERE `name` = :key AND `user` = :user_id LIMIT 1',
['key' => $key, 'user_id' => $userId]
);
}
/**
* @param mixed $value
*
* @return mixed
*/
private function transformReturnValue($value)
{
if (!is_string($value)) {
return $value;
}
if (strtoupper($value) === 'NULL') {
return null;
}
if (strtolower($value) === 'false') {
return false;
}
if (strtolower($value) === 'true') {
return true;
}
if (is_numeric($value)) {
$pointCount = substr_count($value, '.');
if ($pointCount === 0) {
return (int)$value;
}
if ($pointCount === 1) {
return (float)$value;
}
}
return $value;
}
/**
* @param mixed $value
*
* @return string
*/
private function transformToDatabaseValue($value)
{
if ($value === null) {
return 'NULL';
}
if (is_bool($value)) {
return $value === true ? 'true' : 'false';
}
return (string)$value;
}
/**
* @param string $key
*
* @throws InvalidArgumentException
*
* @return string
*/
private function ensureKey($key)
{
if (empty($key)) {
throw new InvalidArgumentException('Required parameter "key" is empty.');
}
return (string)$key;
}
/**
* @param int $userId
*
* @throws InvalidArgumentException
*
* @return int
*/
private function ensureUserId($userId)
{
if ((int)$userId <= 0) {
throw new InvalidArgumentException('Required parameter "userId" is empty.');
}
return (int)$userId;
}
}
@@ -0,0 +1,124 @@
<?php
namespace Xentral\Modules\User\Service;
use Xentral\Components\Database\Database;
use Xentral\Modules\User\Exception\InvalidArgumentException;
final class UserPermissionService
{
/** @var Database $db */
private $db;
/**
* @param Database $database
*/
public function __construct(Database $database)
{
$this->db = $database;
}
/**
* @param int $grantingUserId
* @param string $grantingUserName
* @param int $receivingUserId
* @param string $receivingUserName
* @param string $module
* @param string $action
* @param bool $permission
*
* @return void
*/
public function log(
$grantingUserId,
$grantingUserName,
$receivingUserId,
$receivingUserName,
$module,
$action,
$permission
) {
$grantingUserId = $this->ensureUserId($grantingUserId);
$grantingUserName = $this->ensureNotEmptyString($grantingUserName);
$receivingUserId = $this->ensureUserId($receivingUserId);
$receivingUserName = $this->ensureNotEmptyString($receivingUserName);
$module = $this->ensureNotEmptyString($module);
$action = $this->ensureNotEmptyString($action);
$permission = $this->ensureBoolean($permission);
$permission = $this->transformPermissionToDatabaseValue($permission);
$this->db->perform(
'INSERT INTO permissionhistory (
`id`, `granting_user_id`, `granting_user_name`,
`receiving_user_id`,`receiving_user_name`,`module`,`action`,`permission`
) VALUES (
NULL, :granting_user_id, :granting_user_name,
:receiving_user_id, :receiving_user_name, :module, :action, :permission
)',
[
'granting_user_id' => $grantingUserId,
'granting_user_name' => $grantingUserName,
'receiving_user_id' => $receivingUserId,
'receiving_user_name' => $receivingUserName,
'module' => $module,
'action' => $action,
'permission' => $permission,
]
);
}
/**
* @param int $userId
*
* @throws InvalidArgumentException
*
* @return int
*/
private function ensureUserId($userId)
{
if ((int)$userId <= 0) {
throw new InvalidArgumentException('Required parameter user ID is empty.');
}
return (int)$userId;
}
/**
* @param string $value
*
* @return string
*/
private function ensureNotEmptyString($value)
{
if ($value === '') {
throw new InvalidArgumentException('Required parameter is empty string.');
}
return $value;
}
/**
* @param boolean $value
*
* @return boolean
*/
private function ensureBoolean($value)
{
$type = gettype($value);
if ($type !== 'boolean') {
throw new InvalidArgumentException(sprintf('Wrong type "%s". Only "boolean" is allowed.', $type));
}
return $value;
}
/**
* @param boolean $boolean
*
* @return int
*/
private function transformPermissionToDatabaseValue($boolean)
{
return $boolean === true ? 1 : 0;
}
}