#13035 fixed permission user with access node

This commit is contained in:
Daniel Cebrian 2024-04-10 12:11:45 +02:00
parent 47fc3e1977
commit 68633e032d
3 changed files with 23 additions and 73 deletions

View File

@ -1,70 +0,0 @@
<?php
/**
* Json Web Token ajax
*
* @category Ajax library.
* @package Pandora FMS
* @subpackage Modules.
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
* Please see http://pandorafms.org for full contribution list
* This program 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 for version 2.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ============================================================================
*/
global $config;
if (is_ajax() === false) {
exit;
}
// Begin.
require_once $config['homedir'].'/include/class/JWTRepository.class.php';
try {
$class = new JWTRepository($config['JWT_signature']);
} catch (Exception $e) {
exit;
}
// Ajax controller.
$method = get_parameter('method', '');
$only_metaconsole = (bool) get_parameter('only_metaconsole', false);
if (method_exists($class, $method) === true) {
if ($class->ajaxMethod($method) === true) {
if ($only_metaconsole === true) {
if (is_metaconsole() === true) {
$res = $class->{$method}();
echo json_encode(['success' => true, 'data' => $res]);
} else {
echo json_encode(['success' => false, 'error' => 'Environment is not a metaconsole']);
}
} else {
$res = $class->{$method}();
echo json_encode(['success' => true, 'data' => $res]);
}
} else {
echo json_encode(['success' => false, 'error' => 'Unavailable method.']);
}
} else {
echo json_encode(['success' => false, 'error' => 'Unavailable method.']);
}
exit;

View File

@ -25,9 +25,12 @@
* GNU General Public License for more details. * GNU General Public License for more details.
* ============================================================================ * ============================================================================
*/ */
require_once $config['homedir'].'/include/class/JWTRepository.class.php';
$list_user_tokens = (bool) get_parameter('list_user_tokens'); $list_user_tokens = (bool) get_parameter('list_user_tokens');
$get_jwt_for_login = (bool) get_parameter('get_jwt_for_login', false);
// Tokens for api 2.0.
if ($list_user_tokens === true) { if ($list_user_tokens === true) {
global $config; global $config;
@ -162,3 +165,21 @@ if ($list_user_tokens === true) {
return; return;
} }
// Token for JWT auth in metaconsole.
if ($get_jwt_for_login === true) {
global $config;
if (is_metaconsole() === true
&& (users_is_admin($config['id_user']) === true || can_user_access_node() === true)
&& empty($config['JWT_signature']) === false
) {
$jwtRepository = new JWTRepository($config['JWT_signature']);
$token = $jwtRepository->create();
echo json_encode(['success' => true, 'data' => $token]);
} else {
echo json_encode(['success' => false, 'error' => 'User does not have permission or is not a metaconsole.']);
}
return;
}

View File

@ -2744,9 +2744,8 @@ function redirectNode(url, target = "_blank") {
url: "ajax.php", url: "ajax.php",
dataType: "json", dataType: "json",
data: { data: {
page: "include/ajax/jwt.ajax", page: "include/ajax/token",
method: "create", get_jwt_for_login: 1
only_metaconsole: 1
}, },
success: function(data) { success: function(data) {
var $form = $(`<form class='invisible' target='${target}'></form>`); var $form = $(`<form class='invisible' target='${target}'></form>`);