misuzu/src/Users/Users.php

143 lines
4.3 KiB
PHP

<?php
namespace Misuzu\Users;
use InvalidArgumentException;
use Index\Data\DbStatementCache;
use Index\Data\DbTools;
use Index\Data\IDbConnection;
class Users {
private IDbConnection $dbConn;
private DbStatementCache $cache;
public function __construct(IDbConnection $dbConn) {
$this->dbConn = $dbConn;
$this->cache = new DbStatementCache($dbConn);
}
public function updateUser(
User|string $userInfo,
RoleInfo|string|null $displayRoleInfo = null
): void {
if($userInfo instanceof User)
$userInfo = (string)$userInfo->getId();
if($displayRoleInfo instanceof RoleInfo)
$displayRoleInfo = $displayRoleInfo->getId();
$stmt = $this->cache->get('UPDATE msz_users SET display_role = COALESCE(?, display_role) WHERE user_id = ?');
$stmt->addParameter(1, $displayRoleInfo);
$stmt->addParameter(2, $userInfo);
$stmt->execute();
}
public function hasRole(
User|string $userInfo,
RoleInfo|string $roleInfo
): bool {
if($userInfo instanceof User)
$userInfo = (string)$userInfo->getId();
if($roleInfo instanceof RoleInfo)
$roleInfo = $roleInfo->getId();
return in_array($roleInfo, $this->hasRoles($userInfo, $roleInfo));
}
public function hasRoles(
User|string $userInfo,
RoleInfo|string|array $roleInfos
): array {
if($userInfo instanceof User)
$userInfo = (string)$userInfo->getId();
if(!is_array($roleInfos))
$roleInfos = [$roleInfos];
elseif(empty($roleInfos))
return [];
$args = 0;
$stmt = $this->cache->get(sprintf(
'SELECT role_id FROM msz_users_roles WHERE user_id = ? AND role_id IN (%s)',
DbTools::prepareListString($roleInfos)
));
$stmt->addParameter(++$args, $userInfo);
foreach($roleInfos as $roleInfo) {
if($roleInfo instanceof RoleInfo)
$roleInfo = $roleInfo->getId();
elseif(!is_string($roleInfo))
throw new InvalidArgumentException('$roleInfos must be strings of instances of RoleInfo.');
$stmt->addParameter(++$args, $roleInfo);
}
$stmt->execute();
$roleIds = [];
$result = $stmt->getResult();
while($result->next())
$roleIds[] = (string)$result->getInteger(0);
return $roleIds;
}
public function addRoles(
User|string $userInfo,
RoleInfo|string|array $roleInfos
): void {
if($userInfo instanceof User)
$userInfo = (string)$userInfo->getId();
if(!is_array($roleInfos))
$roleInfos = [$roleInfos];
elseif(empty($roleInfos))
return;
$stmt = $this->cache->get(sprintf(
'REPLACE INTO msz_users_roles (user_id, role_id) VALUES %s',
DbTools::prepareListString($roleInfos, '(?, ?)')
));
$args = 0;
foreach($roleInfos as $roleInfo) {
if($roleInfo instanceof RoleInfo)
$roleInfo = $roleInfo->getId();
elseif(!is_string($roleInfo))
throw new InvalidArgumentException('$roleInfos must be strings of instances of RoleInfo.');
$stmt->addParameter(++$args, $userInfo);
$stmt->addParameter(++$args, $roleInfo);
}
$stmt->execute();
}
public function removeRoles(
User|string $userInfo,
RoleInfo|string|array $roleInfos
): void {
if($userInfo instanceof User)
$userInfo = (string)$userInfo->getId();
if(!is_array($roleInfos))
$roleInfos = [$roleInfos];
elseif(empty($roleInfos))
return;
$args = 0;
$stmt = $this->cache->get(sprintf(
'DELETE FROM msz_users_roles WHERE user_id = ? AND role_id IN (%s)',
DbTools::prepareListString($roleInfos)
));
$stmt->addParameter(++$args, $userInfo);
foreach($roleInfos as $roleInfo) {
if($roleInfo instanceof RoleInfo)
$roleInfo = $roleInfo->getId();
elseif(!is_string($roleInfo))
throw new InvalidArgumentException('$roleInfos must be strings of instances of RoleInfo.');
$stmt->addParameter(++$args, $roleInfo);
}
$stmt->execute();
}
}