style: auto-fix 1340 phpcs PSR-12 violations via phpcbf; exclude MissingNamespace and SideEffects
This commit is contained in:
+31
-15
@@ -1,20 +1,24 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* UserModel - Handles user authentication and management
|
||||
*/
|
||||
class UserModel {
|
||||
class UserModel
|
||||
{
|
||||
private mysqli $conn;
|
||||
private static array $userCache = []; // ['key' => ['data' => ..., 'expires' => timestamp]]
|
||||
private static int $cacheTTL = 300; // 5 minutes
|
||||
|
||||
public function __construct(mysqli $conn) {
|
||||
public function __construct(mysqli $conn)
|
||||
{
|
||||
$this->conn = $conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached user data if not expired
|
||||
*/
|
||||
private static function getCached(string $key): ?array {
|
||||
private static function getCached(string $key): ?array
|
||||
{
|
||||
if (isset(self::$userCache[$key])) {
|
||||
$cached = self::$userCache[$key];
|
||||
if ($cached['expires'] > time()) {
|
||||
@@ -29,7 +33,8 @@ class UserModel {
|
||||
/**
|
||||
* Store user data in cache with expiration
|
||||
*/
|
||||
private static function setCached(string $key, array $data): void {
|
||||
private static function setCached(string $key, array $data): void
|
||||
{
|
||||
self::$userCache[$key] = [
|
||||
'data' => $data,
|
||||
'expires' => time() + self::$cacheTTL
|
||||
@@ -39,7 +44,8 @@ class UserModel {
|
||||
/**
|
||||
* Invalidate specific user cache entry
|
||||
*/
|
||||
public static function invalidateCache(?int $userId = null, ?string $username = null): void {
|
||||
public static function invalidateCache(?int $userId = null, ?string $username = null): void
|
||||
{
|
||||
if ($userId !== null) {
|
||||
unset(self::$userCache["user_id_$userId"]);
|
||||
}
|
||||
@@ -57,7 +63,8 @@ class UserModel {
|
||||
* @param string $groups Comma-separated groups from Remote-Groups header
|
||||
* @return array User data array
|
||||
*/
|
||||
public function syncUserFromAuthelia(string $username, string $displayName = '', string $email = '', string $groups = ''): array {
|
||||
public function syncUserFromAuthelia(string $username, string $displayName = '', string $email = '', string $groups = ''): array
|
||||
{
|
||||
// Check cache first
|
||||
$cacheKey = "user_$username";
|
||||
$cached = self::getCached($cacheKey);
|
||||
@@ -122,7 +129,8 @@ class UserModel {
|
||||
*
|
||||
* @return array|null System user data or null if not found
|
||||
*/
|
||||
public function getSystemUser(): ?array {
|
||||
public function getSystemUser(): ?array
|
||||
{
|
||||
// Check cache first
|
||||
$cached = self::getCached('system');
|
||||
if ($cached !== null) {
|
||||
@@ -150,7 +158,8 @@ class UserModel {
|
||||
* @param int $userId User ID
|
||||
* @return array|null User data or null if not found
|
||||
*/
|
||||
public function getUserById(int $userId): ?array {
|
||||
public function getUserById(int $userId): ?array
|
||||
{
|
||||
// Check cache first
|
||||
$cacheKey = "user_id_$userId";
|
||||
$cached = self::getCached($cacheKey);
|
||||
@@ -180,7 +189,8 @@ class UserModel {
|
||||
* @param string $username Username
|
||||
* @return array|null User data or null if not found
|
||||
*/
|
||||
public function getUserByUsername(string $username): ?array {
|
||||
public function getUserByUsername(string $username): ?array
|
||||
{
|
||||
// Check cache first
|
||||
$cacheKey = "user_$username";
|
||||
$cached = self::getCached($cacheKey);
|
||||
@@ -210,7 +220,8 @@ class UserModel {
|
||||
* @param string $groups Comma-separated group names
|
||||
* @return bool True if user is in admin group
|
||||
*/
|
||||
private function checkAdminStatus(string $groups): bool {
|
||||
private function checkAdminStatus(string $groups): bool
|
||||
{
|
||||
if (empty($groups)) {
|
||||
return false;
|
||||
}
|
||||
@@ -226,7 +237,8 @@ class UserModel {
|
||||
* @param array $user User data array
|
||||
* @return bool True if user is admin
|
||||
*/
|
||||
public function isAdmin(array $user): bool {
|
||||
public function isAdmin(array $user): bool
|
||||
{
|
||||
return isset($user['is_admin']) && (int)$user['is_admin'] === 1;
|
||||
}
|
||||
|
||||
@@ -237,7 +249,8 @@ class UserModel {
|
||||
* @param array $requiredGroups Array of required group names
|
||||
* @return bool True if user is in at least one required group
|
||||
*/
|
||||
public function hasGroupAccess(array $user, array $requiredGroups = ['admin', 'employee']): bool {
|
||||
public function hasGroupAccess(array $user, array $requiredGroups = ['admin', 'employee']): bool
|
||||
{
|
||||
if (empty($user['groups'])) {
|
||||
return false;
|
||||
}
|
||||
@@ -253,7 +266,8 @@ class UserModel {
|
||||
*
|
||||
* @return array Array of user records
|
||||
*/
|
||||
public function getAllUsers(): array {
|
||||
public function getAllUsers(): array
|
||||
{
|
||||
$stmt = $this->conn->prepare("SELECT * FROM users ORDER BY created_at DESC");
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
@@ -276,7 +290,8 @@ class UserModel {
|
||||
*
|
||||
* @return array Array of unique group names
|
||||
*/
|
||||
public function getAllGroups(): array {
|
||||
public function getAllGroups(): array
|
||||
{
|
||||
$cacheKey = 'all_groups';
|
||||
|
||||
// Check cache first
|
||||
@@ -311,7 +326,8 @@ class UserModel {
|
||||
* Invalidate the groups cache
|
||||
* Call this when user groups are modified
|
||||
*/
|
||||
public static function invalidateGroupsCache(): void {
|
||||
public static function invalidateGroupsCache(): void
|
||||
{
|
||||
unset(self::$userCache['all_groups']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user