246 lines
6.4 KiB
PHP
246 lines
6.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace GlpiPlugin\Urbackup;
|
|
|
|
use CommonGLPI;
|
|
use Glpi\Application\View\TemplateRenderer;
|
|
use Session;
|
|
|
|
class Profile extends \Profile
|
|
{
|
|
public static $rightname = 'plugin_urbackup';
|
|
|
|
public static function getIcon()
|
|
{
|
|
return 'ti ti-server';
|
|
}
|
|
|
|
public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
|
|
{
|
|
if ($item instanceof \Profile && $item->getField('interface') === 'central') {
|
|
return self::createTabEntry(Server::getTypeName(2));
|
|
}
|
|
return '';
|
|
}
|
|
|
|
public static function displayTabContentForItem(
|
|
CommonGLPI $item,
|
|
$tabnum = 1,
|
|
$withtemplate = 0
|
|
) {
|
|
if (!$item instanceof \Profile) {
|
|
return false;
|
|
}
|
|
|
|
$profile = new \Profile();
|
|
$profile->getFromDB($item->getID());
|
|
|
|
$rights = self::getAllRights();
|
|
|
|
$twig = TemplateRenderer::getInstance();
|
|
$twig->display('@urbackup/profile.html.twig', [
|
|
'id' => $item->getID(),
|
|
'profile' => $profile,
|
|
'title' => self::getTypeName(Session::getPluralNumber()),
|
|
'rights' => $rights,
|
|
]);
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function getTypeName($nb = 0): string
|
|
{
|
|
return _n('UrBackup', 'UrBackup', $nb, 'urbackup');
|
|
}
|
|
|
|
public static function getAllRights(): array
|
|
{
|
|
return [
|
|
[
|
|
'itemtype' => Server::class,
|
|
'label' => __('UrBackup Servers', 'urbackup'),
|
|
'field' => 'plugin_urbackup',
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function installRights(): void
|
|
{
|
|
self::registerRights();
|
|
|
|
$profiles_id = (int) ($_SESSION['glpiactiveprofile']['id'] ?? 0);
|
|
|
|
if ($profiles_id > 0) {
|
|
self::setProfileRights($profiles_id, READ | UPDATE | CREATE | DELETE);
|
|
}
|
|
|
|
global $DB;
|
|
$all_profiles = $DB->request([
|
|
'SELECT' => 'id',
|
|
'FROM' => 'glpi_profiles',
|
|
]);
|
|
|
|
foreach ($all_profiles as $profile) {
|
|
if ($profile['id'] !== $profiles_id) {
|
|
$existing = $DB->request([
|
|
'FROM' => 'glpi_profilerights',
|
|
'WHERE' => [
|
|
'profiles_id' => $profile['id'],
|
|
'name' => self::$rightname,
|
|
],
|
|
'LIMIT' => 1,
|
|
]);
|
|
|
|
if (count($existing) === 0) {
|
|
$DB->insert('glpi_profilerights', [
|
|
'profiles_id' => $profile['id'],
|
|
'name' => self::$rightname,
|
|
'rights' => READ,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function uninstallRights(): void
|
|
{
|
|
global $DB;
|
|
|
|
$DB->delete('glpi_profilerights', [
|
|
'name' => self::$rightname,
|
|
]);
|
|
|
|
\ProfileRight::deleteProfileRights([self::$rightname]);
|
|
}
|
|
|
|
public static function registerRights(): void
|
|
{
|
|
global $DB;
|
|
|
|
$iterator = $DB->request([
|
|
'FROM' => 'glpi_profilerights',
|
|
'WHERE' => ['name' => self::$rightname],
|
|
'LIMIT' => 1,
|
|
]);
|
|
|
|
if (count($iterator) > 0) {
|
|
return;
|
|
}
|
|
|
|
\ProfileRight::addProfileRights([self::$rightname]);
|
|
}
|
|
|
|
public static function setProfileRights(int $profiles_id, int $rights): void
|
|
{
|
|
global $DB;
|
|
|
|
if ($profiles_id <= 0) {
|
|
return;
|
|
}
|
|
|
|
$iterator = $DB->request([
|
|
'FROM' => 'glpi_profilerights',
|
|
'WHERE' => [
|
|
'profiles_id' => $profiles_id,
|
|
'name' => self::$rightname,
|
|
],
|
|
'LIMIT' => 1,
|
|
]);
|
|
|
|
if (count($iterator) > 0) {
|
|
$row = $iterator->current();
|
|
$DB->update(
|
|
'glpi_profilerights',
|
|
['rights' => $rights],
|
|
['id' => (int) $row['id']]
|
|
);
|
|
return;
|
|
}
|
|
|
|
$DB->insert('glpi_profilerights', [
|
|
'profiles_id' => $profiles_id,
|
|
'name' => self::$rightname,
|
|
'rights' => $rights,
|
|
]);
|
|
}
|
|
|
|
public static function getProfileRights(int $profiles_id): int
|
|
{
|
|
global $DB;
|
|
|
|
if ($profiles_id <= 0) {
|
|
return 0;
|
|
}
|
|
|
|
$iterator = $DB->request([
|
|
'FROM' => 'glpi_profilerights',
|
|
'WHERE' => [
|
|
'profiles_id' => $profiles_id,
|
|
'name' => self::$rightname,
|
|
],
|
|
'LIMIT' => 1,
|
|
]);
|
|
|
|
if (count($iterator) === 0) {
|
|
return 0;
|
|
}
|
|
|
|
$row = $iterator->current();
|
|
|
|
return (int) ($row['rights'] ?? 0);
|
|
}
|
|
|
|
public static function canCurrentUser(int $right): bool
|
|
{
|
|
$profiles_id = (int) ($_SESSION['glpiactiveprofile']['id'] ?? 0);
|
|
|
|
if ($profiles_id === 0) {
|
|
$user_id = (int) ($_SESSION['glpiID'] ?? 0);
|
|
if ($user_id > 0) {
|
|
global $DB;
|
|
$iterator = $DB->request([
|
|
'SELECT' => 'profiles_id',
|
|
'FROM' => 'glpi_profiles_users',
|
|
'WHERE' => [
|
|
'users_id' => $user_id,
|
|
'is_dynamic' => 0,
|
|
],
|
|
'LIMIT' => 1,
|
|
]);
|
|
if (count($iterator) > 0) {
|
|
$row = $iterator->current();
|
|
$profiles_id = (int) $row['profiles_id'];
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($profiles_id > 0) {
|
|
$rights = self::getProfileRights($profiles_id);
|
|
if (($rights & $right) === $right) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return (bool) Session::haveRight(self::$rightname, $right);
|
|
}
|
|
|
|
public static function initProfile($profile = null): void
|
|
{
|
|
$profile_id = 0;
|
|
|
|
if ($profile instanceof \Profile) {
|
|
$profile_id = $profile->getID();
|
|
} elseif (is_array($profile) && isset($profile['id'])) {
|
|
$profile_id = (int) $profile['id'];
|
|
}
|
|
|
|
if ($profile_id > 0) {
|
|
$current_rights = self::getProfileRights($profile_id);
|
|
if ($current_rights === 0) {
|
|
self::setProfileRights($profile_id, READ);
|
|
}
|
|
}
|
|
}
|
|
} |