This repository has been archived on 2024-04-19. You can view files and clone it, but cannot push or open issues or pull requests.
crispage-lite/patch/core/app/assets/class/User.php

190 lines
4.7 KiB
PHP
Raw Normal View History

2023-12-07 14:07:15 -05:00
<?php
/*
Crispage CMS
crispycat <the@crispy.cat>
https://crispy.cat/software/crispage
Crispage is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
namespace Crispage\Assets;
defined("ROOT") or die();
require_once ROOT . "/core/app/assets/Asset.php";
use \Crispage\Auth\CorePermissions;
class User extends \Crispage\Asset {
public const UNCONFIRMED = 0;
public const CONFIRMED = 1;
public const ACTIVATED = 2;
public static function getNames(): array {
return ["{%USER}", "{%USERS}"];
}
public static function getPermissions(): array {
return [
"create" => CorePermissions::NONE,
"modify" => CorePermissions::MODIFY_OTHERS,
"modify_own"=> CorePermissions::MODIFY_SELF,
"delete" => CorePermissions::NEVER,
"delete_own"=> CorePermissions::NEVER
];
}
public static function getSortingInfo(): array {
return [
"field" => "mtime",
"desc" => true
];
}
public static function getListColumns(): array {
global $Crispage;
return [
"id" => ["label" => "{%ID}"],
"slug" => [
"label" => "{%USERNAME}",
"filter" => "\\Crispage\\Utils\\TextUtils::codeify"
],
"displayname" => [
"label" => "{%DISPLAY_NAME}",
"filter" => "htmlentities"
],
"last_login" => [
"label" => "{%LAST_LOGIN}",
"filter" => [$Crispage->i18n, "datetime"]
],
"state" => [
"label" => "{%STATE}",
"filter" => function(int $s): string {
global $Crispage;
switch ($s) {
case self::CONFIRMED:
$str = "CONFIRMED";
break;
case self::ACTIVATED:
$str = "ACTIVATED";
break;
default:
$str = "UNCONFIRMED";
}
return $Crispage->i18n->translate("{%$str}");
}
],
"mtime" => [
"label" => "{%MODIFIED}",
"filter" => [$Crispage->i18n, "datetime"]
]
];
}
public static function getEditorFields(): array {
return [
"displayname" => [
"column" => 0,
"component" => "\\Crispage\\Components\\Fields\\TextFieldComponent",
"com_data" => [
"label" => "{%DISPLAY_NAME}:",
"attrs" => ["required"]
],
"filter" => "strval"
],
"email" => [
"column" => 0,
"component" => "\\Crispage\\Components\\Fields\\TextFieldComponent",
"com_data" => [
"label" => "{%EMAIL}:",
"attrs" => ["required"]
],
"filter" => "strval"
],
"role_ids" => [
"column" => 1,
"component" => "\\Crispage\\Components\\Fields\\RoleSelectFieldComponent",
"com_data" => [
"label" => "{%ROLES}:"
],
"filter" => function($data): string {
if (!is_array($data)) return "[]";
$data = array_filter($data, "ctype_digit");
return json_encode(array_values($data));
}
],
"state" => [
"column" => 1,
"component" => "\\Crispage\\Components\\Fields\\SelectFieldComponent",
"com_data" => [
"label" => "{%STATE}:",
"options" => [
0 => "{%DEACTIVATED}",
2 => "{%ACTIVATED}"
]
],
"filter" => "intval"
]
];
}
public string $displayname;
public string $email;
public int $last_login;
public string $role_ids;
private array $_role_ids_array;
public int $state;
public function __construct(array $props) {
parent::__construct($props);
$this->displayname = $props["displayname"] ?? $props["slug"] ?? "";
$this->email = $props["email"] ?? "";
$this->last_login = $props["last_login"] ?? 0;
$this->role_ids = $props["role_ids"] ?? "[]";
$this->_role_ids_array = json_decode($this->role_ids, true) ?? [];
$this->state = $props["state"] ?? 0;
}
public function getRoles(): array {
return $this->_role_ids_array;
}
public function hasRole(int $id): bool {
return in_array($id, $this->_role_ids_array);
}
public function setRoles(int ...$ids): array {
$this->_role_ids_array = $ids;
$this->role_ids = json_encode($this->_role_ids_array);
return $this->_role_ids_array;
}
public function addRoles(int ...$ids): array {
$this->_role_ids_array = array_unique(array_merge(
$this->_role_ids_array, $ids
));
$this->role_ids = json_encode($this->_role_ids_array);
return $this->_role_ids_array;
}
public function removeRoles(int ...$ids): array {
$this->_role_ids_array = array_filter(
$this->_role_ids_array, fn(int $id): bool => !in_array($id, $ids)
);
$this->role_ids = json_encode($this->_role_ids_array);
return $this->_role_ids_array;
}
public function getName(): string {
return $this->displayname;
}
public function getOwnerId(): int {
return $this->id;
}
}
?>