2024-07-15 20:46:44 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* @author Sascha Nitsch (grumpydeveloper)
|
|
|
|
**/
|
|
|
|
|
|
|
|
namespace Federator\Data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* storage class for user attributes
|
|
|
|
*/
|
|
|
|
class User
|
|
|
|
{
|
2024-07-17 15:45:19 +02:00
|
|
|
/**
|
|
|
|
* user id
|
|
|
|
*
|
|
|
|
* @var string $id
|
|
|
|
*/
|
2024-07-15 20:46:44 +02:00
|
|
|
public $id;
|
|
|
|
|
|
|
|
/* @var string user language */
|
|
|
|
//public $lang;
|
|
|
|
|
2024-07-17 15:45:19 +02:00
|
|
|
/**
|
|
|
|
* user permissions
|
|
|
|
*
|
|
|
|
* @var array<string> $permissions
|
|
|
|
* @todo convert to enum
|
|
|
|
*/
|
2024-07-15 20:46:44 +02:00
|
|
|
public $permissions = [];
|
|
|
|
|
2024-07-17 15:45:19 +02:00
|
|
|
/**
|
|
|
|
* session id
|
|
|
|
*
|
|
|
|
* @var string $session
|
|
|
|
* */
|
2024-07-15 20:46:44 +02:00
|
|
|
public $session;
|
|
|
|
|
2024-07-17 21:45:33 +02:00
|
|
|
/**
|
|
|
|
* create new user object from json string
|
|
|
|
*
|
|
|
|
* @param string $input input string
|
|
|
|
* @return User|false
|
|
|
|
*/
|
|
|
|
public static function createFromJson($input)
|
|
|
|
{
|
|
|
|
$data = json_decode($input, true);
|
|
|
|
if ($data === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$user = new User();
|
|
|
|
$user->id = $data['id'];
|
|
|
|
/// TODO: replace with enums
|
|
|
|
$user->permissions = $data['permissions'];
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2024-07-15 20:46:44 +02:00
|
|
|
/**
|
|
|
|
* check if use has asked permission
|
|
|
|
* @param string $p @unused-param
|
|
|
|
* permission to check
|
2024-07-17 15:45:19 +02:00
|
|
|
*
|
2024-07-15 20:46:44 +02:00
|
|
|
* @return bool true if user has permission, false if not
|
2024-07-17 15:45:19 +02:00
|
|
|
* @todo convert to ENUM
|
2024-07-15 20:46:44 +02:00
|
|
|
*/
|
|
|
|
public function hasPermission(string $p)
|
|
|
|
{
|
2024-07-17 21:45:33 +02:00
|
|
|
return in_array($p, $this->permissions, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* convert internal data to json string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function toJson()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'id' => $this->id,
|
|
|
|
'permissions' => $this->permissions
|
|
|
|
];
|
|
|
|
return json_encode($data) | '';
|
2024-07-15 20:46:44 +02:00
|
|
|
}
|
|
|
|
}
|