Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBS-9341: Fix tenant restriction for tables #15

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions classes/form/rights_config_filter_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@

namespace local_ai_manager\form;

use core_plugin_manager;
use local_ai_manager\base_connector;
use local_ai_manager\base_purpose;
use local_ai_manager\local\tenant;
use local_ai_manager\local\userinfo;
use local_ai_manager\manager;
use local_bycsauth\idmgroup;
use local_bycsauth\school;

defined('MOODLE_INTERNAL') || die;

global $CFG;
Expand Down
2 changes: 1 addition & 1 deletion classes/hook/userinfo_extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace local_ai_manager\hook;

use core\exception\coding_exception;
use coding_exception;
use local_ai_manager\local\userinfo;

/**
Expand Down
4 changes: 2 additions & 2 deletions classes/local/rights_config_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function __construct(
$from =
'{user} u LEFT JOIN {local_ai_manager_userinfo} ui ON u.id = ui.userid';
$where = 'u.deleted != 1 AND u.suspended != 1 AND ' . $tenantfield . ' = :tenant';
$params = ['tenant' => $tenant->get_identifier()];
$params = ['tenant' => $tenant->get_sql_identifier()];

$usertableextend = new usertable_extend($tenant, $columns, $headers, $filterids, $fields, $from, $where, $params);
\core\di::get(\core\hook\manager::class)->dispatch($usertableextend);
Expand All @@ -86,7 +86,7 @@ public function __construct(

$this->set_count_sql(
"SELECT COUNT(DISTINCT id) FROM {user} WHERE " . $tenantfield . " = :tenant",
['tenant' => $tenant->get_identifier()]
['tenant' => $tenant->get_sql_identifier()]
);

$this->set_sql($usertableextend->get_fields(), $usertableextend->get_from(),
Expand Down
13 changes: 9 additions & 4 deletions classes/local/statistics_overview_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ public function __construct(
$this->collapsible(false);

$fields = 'model, modelinfo, COUNT(modelinfo) AS requestcount, SUM(value) AS userusage';
$from = '{local_ai_manager_request_log}';
$where = '1=1 GROUP BY modelinfo';
$this->set_sql($fields, $from, $where);
$this->set_count_sql('SELECT COUNT(DISTINCT modelinfo) FROM {local_ai_manager_request_log}');
$from = '{local_ai_manager_request_log} rl JOIN {user} u ON rl.userid = u.id';
$tenantfield = get_config('local_ai_manager', 'tenantcolumn');
$tenant = \core\di::get(tenant::class);
$where = 'u.' . $tenantfield . ' = :tenant GROUP BY modelinfo';
$params = ['tenant' => $tenant->get_sql_identifier()];
$this->set_sql($fields, $from, $where, $params);
$this->set_count_sql('SELECT COUNT(DISTINCT modelinfo) FROM {local_ai_manager_request_log} rl'
. ' JOIN {user} u ON rl.userid = u.id WHERE u.' . $tenantfield . ' = :tenant',
$params);

parent::setup();
}
Expand Down
9 changes: 9 additions & 0 deletions classes/local/tenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function get_identifier(): string {
return $this->identifier;
}

/**
* Returns the identifier which needs to be used in SQL statements.
*
* @return string the tenant identifier for SQL statements
*/
public function get_sql_identifier(): string {
return $this->is_default_tenant() ? '' : $this->identifier;
}

/**
* Returns if the current tenant of this object is the default tenant.
*
Expand Down
1 change: 0 additions & 1 deletion classes/local/userinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace local_ai_manager\local;

use local_ai_manager\hook\userinfo_extend;
use local_bycsauth\school;
use stdClass;

/**
Expand Down
15 changes: 9 additions & 6 deletions classes/local/userstats_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,24 @@ public function __construct(
$from = '{local_ai_manager_request_log} rl LEFT JOIN {local_ai_manager_userinfo} ui ON rl.userid = ui.userid'
. ' JOIN {user} u ON u.id = rl.userid';
$where = $tenantfield . ' = :tenant AND purpose = :purpose GROUP BY u.id';
$params = ['tenant' => $tenant->get_identifier(), 'purpose' => $purpose];
$params = [
'tenant' => $tenant->get_sql_identifier(),
'purpose' => $purpose,
];
$this->set_count_sql(
"SELECT COUNT(DISTINCT userid) FROM {local_ai_manager_request_log} rl JOIN {user} u ON rl.userid = u.id "
. "WHERE " . $tenantfield . " = :tenant AND purpose = :purpose",
['tenant' => $tenant->get_identifier(), 'purpose' => $purpose]
$params
);
} else {
$fields = 'u.id as id, lastname, firstname, COUNT(value) AS requestcount';
$from =
'{user} u LEFT JOIN {local_ai_manager_request_log} rl ON u.id = rl.userid';
$where = $tenantfield . ' = :tenant GROUP BY u.id';
$params = ['tenant' => $tenant->get_identifier()];
'{local_ai_manager_request_log} rl LEFT JOIN {user} u ON rl.userid = u.id';
$where = 'u.' . $tenantfield . ' = :tenant GROUP BY u.id';
$params = ['tenant' => $tenant->get_sql_identifier()];
$this->set_count_sql(
"SELECT COUNT(DISTINCT id) FROM {user} WHERE " . $tenantfield . " = :tenant",
['tenant' => $tenant->get_identifier()]
$params
);
}
$this->set_sql($fields, $from, $where, $params);
Expand Down
2 changes: 1 addition & 1 deletion lang/de/local_ai_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
$string['assignpurposes'] = 'Einsatzzwecke festlegen';
$string['assignrole'] = 'Rolle zuweisen';
$string['basicsettings'] = 'Grundeinstellungen';
$string['basicsettingsdesc'] = 'Grundeinstellungn des AI-Managers konfigurieren';
$string['basicsettingsdesc'] = 'Grundeinstellungen des AI-Managers konfigurieren';
$string['configure_instance'] = 'KI-Tool-Instanzen konfigurieren';
$string['configureaitool'] = 'KI-Tool konfigurieren';
$string['configurepurposes'] = 'Einsatzzwecke konfigurieren';
Expand Down
2 changes: 1 addition & 1 deletion purpose_statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
$tenantfield = get_config('local_ai_manager', 'tenantcolumn');
$recordscountsql = "SELECT COUNT(*) FROM {local_ai_manager_request_log} rl JOIN {user} u ON rl.userid = u.id"
. " WHERE u." . $tenantfield . " = :tenant AND rl.purpose = :purpose";
$recordscountparams = ['tenant' => $tenant->get_identifier(), 'purpose' => $purpose];
$recordscountparams = ['tenant' => $tenant->get_sql_identifier(), 'purpose' => $purpose];
$recordscount = $DB->count_records_sql($recordscountsql, $recordscountparams);

if ($recordscount !== 0) {
Expand Down
1 change: 0 additions & 1 deletion rights_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

use local_ai_manager\local\userinfo;
use local_ai_manager\output\tenantnavbar;
use local_bycsauth\idmgroup;

require_once(dirname(__FILE__) . '/../../config.php');
require_login();
Expand Down
4 changes: 2 additions & 2 deletions user_statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
$recordscount =
$DB->count_records_sql("SELECT COUNT(*) FROM {local_ai_manager_request_log} rl JOIN {user} u ON rl.userid = u.id"
. " WHERE u." . $tenantfield . " = :tenant",
['tenant' => $tenant->get_identifier()]);
['tenant' => $tenant->get_sql_identifier()]);

if ($recordscount !== 0) {
$uniqid = 'statistics-table-users-all-purposes';

$baseurl = new moodle_url('/local/ai_manager/user_statistics.php', ['tenant' => $tenant->get_identifier()]);
$baseurl = new moodle_url('/local/ai_manager/user_statistics.php', ['tenant' => $tenant->get_sql_identifier()]);
$table = new \local_ai_manager\local\userstats_table($uniqid, '', $tenant, $baseurl);
$table->out(5, false);
} else {
Expand Down
Loading