federator/php/data/user.php

96 lines
1.9 KiB
PHP

<?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
{
/**
* user id on the external system
*
* @var string $externalid
*/
public $externalid;
/**
* user id
*
* @var string $id
*/
public $id;
/* @var string user language */
//public $lang;
/**
* user permissions
*
* @var array<string> $permissions
* @todo convert to enum
*/
public $permissions = [];
/**
* session id
*
* @var string $session
* */
public $session;
/**
* 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'];
$user->externalid = $data['externalid'];
/// TODO: replace with enums
$user->permissions = $data['permissions'];
return $user;
}
/**
* check if use has asked permission
* @param string $p @unused-param
* permission to check
*
* @return bool true if user has permission, false if not
* @todo convert to ENUM
*/
public function hasPermission(string $p)
{
return in_array($p, $this->permissions, false);
}
/**
* convert internal data to json string
*
* @return string
*/
public function toJson()
{
$data = [
'id' => $this->id,
'externalid' => $this->externalid,
'permissions' => $this->permissions
];
return json_encode($data) | '';
}
}