Skip to content

Commit 74d2337

Browse files
committed
feat: add permissions check to tools
1 parent 489ee77 commit 74d2337

13 files changed

+129
-7
lines changed

core/components/modai/src/Model/Tool.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ public static function getAvailableTools(modX $modx, ?int $agentId = null): arra
5555
$tools = $modx->getIterator(self::class, $c);
5656

5757
foreach ($tools as $tool) {
58+
$className = $tool->get('class');
59+
if (!class_exists($className)) {
60+
continue;
61+
}
62+
63+
if (!is_subclass_of($className, ToolInterface::class, true)) {
64+
continue;
65+
}
66+
67+
$hasPermissions = $className::checkPermissions($modx);
68+
if (!$hasPermissions) {
69+
continue;
70+
}
71+
5872
$output[$tool->get('name')] = $tool;
5973
}
6074

core/components/modai/src/Tools/CreateCategory.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ public function __construct(modX $modx, array $config)
7070
*/
7171
public function runTool($parameters): string
7272
{
73+
if (!self::checkPermissions($this->modx)) {
74+
return json_encode(['success' => false, "message" => "You do not have permission to use this tool."]);
75+
}
76+
7377
if (empty($parameters)) {
74-
throw new \Exception('Parameters are required.');
78+
return json_encode(['success' => false, 'message' => 'Parameters are required.']);
7579
}
7680

7781
$output = [];
@@ -106,4 +110,9 @@ public function runTool($parameters): string
106110

107111
return json_encode($output);
108112
}
113+
114+
public static function checkPermissions(modX $modx): bool
115+
{
116+
return $modx->hasPermission('save_category');
117+
}
109118
}

core/components/modai/src/Tools/CreateChunk.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ public function __construct(modX $modx, array $config)
7171
*/
7272
public function runTool($parameters): string
7373
{
74+
if (!self::checkPermissions($this->modx)) {
75+
return json_encode(['success' => false, "message" => "You do not have permission to use this tool."]);
76+
}
77+
7478
if (empty($parameters)) {
75-
throw new \Exception('Parameters are required.');
79+
return json_encode(['success' => false, 'message' => 'Parameters are required.']);
7680
}
7781

7882
$output = [];
@@ -94,4 +98,9 @@ public function runTool($parameters): string
9498

9599
return json_encode($output);
96100
}
101+
102+
public static function checkPermissions(modX $modx): bool
103+
{
104+
return $modx->hasPermission('save_chunk');
105+
}
97106
}

core/components/modai/src/Tools/CreateResource.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ public function __construct(modX $modx, array $config)
7171
*/
7272
public function runTool($parameters): string
7373
{
74+
if (!self::checkPermissions($this->modx)) {
75+
return json_encode(['success' => false, "message" => "You do not have permission to use this tool."]);
76+
}
77+
7478
if (empty($parameters)) {
75-
throw new \Exception('Parameters are required.');
79+
return json_encode(['success' => false, 'message' => 'Parameters are required.']);
7680
}
7781

7882
$output = [];
@@ -119,4 +123,9 @@ public function runTool($parameters): string
119123

120124
return json_encode($output);
121125
}
126+
127+
public static function checkPermissions(modX $modx): bool
128+
{
129+
return $modx->hasPermission('save_document');
130+
}
122131
}

core/components/modai/src/Tools/CreateTemplate.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ public function __construct(modX $modx, array $config)
7171
*/
7272
public function runTool($parameters): string
7373
{
74+
if (!self::checkPermissions($this->modx)) {
75+
return json_encode(['success' => false, "message" => "You do not have permission to use this tool."]);
76+
}
77+
7478
if (empty($parameters)) {
75-
throw new \Exception('Parameters are required.');
79+
return json_encode(['success' => false, 'message' => 'Parameters are required.']);
7680
}
7781

7882
$output = [];
@@ -94,4 +98,9 @@ public function runTool($parameters): string
9498

9599
return json_encode($output);
96100
}
101+
102+
public static function checkPermissions(modX $modx): bool
103+
{
104+
return $modx->hasPermission('save_template');
105+
}
97106
}

core/components/modai/src/Tools/DeleteChunks.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,21 @@ public function __construct(modX $modx, array $config)
5252
*/
5353
public function runTool($parameters): string
5454
{
55+
if (!self::checkPermissions($this->modx)) {
56+
return json_encode(['success' => false, "message" => "You do not have permission to use this tool."]);
57+
}
58+
5559
if (empty($parameters) || empty($parameters['ids'])) {
56-
throw new \Exception("Missing parameters");
60+
return json_encode(['success' => false, 'message' => 'Parameters are required.']);
5761
}
5862

5963
$this->modx->removeCollection(modChunk::class, ['id:IN' => $parameters['ids']]);
6064

6165
return json_encode(["success" => true]);
6266
}
67+
68+
public static function checkPermissions(modX $modx): bool
69+
{
70+
return $modx->hasPermission('delete_chunks');
71+
}
6372
}

core/components/modai/src/Tools/GetCategories.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function __construct(modX $modx, array $config)
4040
*/
4141
public function runTool($parameters): string
4242
{
43+
if (!self::checkPermissions($this->modx)) {
44+
return json_encode(['success' => false, "message" => "You do not have permission to use this tool."]);
45+
}
46+
4347
/** @var modCategory[] $chunks */
4448
$categories = $this->modx->getIterator(modCategory::class);
4549
foreach ($categories as $category) {
@@ -53,4 +57,9 @@ public function runTool($parameters): string
5357

5458
return json_encode($output);
5559
}
60+
61+
public static function checkPermissions(modX $modx): bool
62+
{
63+
return true;
64+
}
5665
}

core/components/modai/src/Tools/GetChunks.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public function __construct(modX $modx, array $config)
7272
*/
7373
public function runTool($parameters): string
7474
{
75+
if (!self::checkPermissions($this->modx)) {
76+
return json_encode(['success' => false, "message" => "You do not have permission to use this tool."]);
77+
}
78+
7579
$where = [];
7680

7781
if (is_array($parameters)) {
@@ -127,4 +131,9 @@ public function runTool($parameters): string
127131

128132
return json_encode($output);
129133
}
134+
135+
public static function checkPermissions(modX $modx): bool
136+
{
137+
return true;
138+
}
130139
}

core/components/modai/src/Tools/GetResourceDetail.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function __construct(modX $modx, array $config)
5454
*/
5555
public function runTool($parameters): string
5656
{
57+
if (!self::checkPermissions($this->modx)) {
58+
return json_encode(['success' => false, "message" => "You do not have permission to use this tool."]);
59+
}
60+
5761
if (empty($parameters)) {
5862
return json_encode(['success' => false, 'message' => 'Parameters are required.']);
5963
}
@@ -87,4 +91,9 @@ public function runTool($parameters): string
8791

8892
return json_encode($output);
8993
}
94+
95+
public static function checkPermissions(modX $modx): bool
96+
{
97+
return true;
98+
}
9099
}

core/components/modai/src/Tools/GetResources.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public function __construct(modX $modx, array $config)
5858
*/
5959
public function runTool($parameters): string
6060
{
61+
if (!self::checkPermissions($this->modx)) {
62+
return json_encode(['success' => false, "message" => "You do not have permission to use this tool."]);
63+
}
64+
6165
$where = [];
6266

6367
if (is_array($parameters)) {
@@ -100,4 +104,9 @@ public function runTool($parameters): string
100104

101105
return json_encode($output);
102106
}
107+
108+
public static function checkPermissions(modX $modx): bool
109+
{
110+
return true;
111+
}
103112
}

core/components/modai/src/Tools/GetTemplates.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public function __construct(modX $modx, array $config)
7272
*/
7373
public function runTool($parameters): string
7474
{
75+
if (!self::checkPermissions($this->modx)) {
76+
return json_encode(['success' => false, "message" => "You do not have permission to use this tool."]);
77+
}
78+
7579
$where = [];
7680

7781
if (is_array($parameters)) {
@@ -127,4 +131,9 @@ public function runTool($parameters): string
127131

128132
return json_encode($output);
129133
}
134+
135+
public static function checkPermissions(modX $modx): bool
136+
{
137+
return true;
138+
}
130139
}

core/components/modai/src/Tools/GetWeather.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class GetWeather implements ToolInterface
88
{
9+
private $modx;
10+
911
public static function getSuggestedName(): string
1012
{
1113
return 'get_weather';
@@ -39,6 +41,7 @@ public static function getConfig(): array
3941

4042
public function __construct(modX $modx, array $config)
4143
{
44+
$this->modx = $modx;
4245
}
4346

4447
/**
@@ -47,6 +50,10 @@ public function __construct(modX $modx, array $config)
4750
*/
4851
public function runTool($parameters): string
4952
{
53+
if (!self::checkPermissions($this->modx)) {
54+
return json_encode(['success' => false, "message" => "You do not have permission to use this tool."]);
55+
}
56+
5057
try {
5158
$res = file_get_contents(
5259
"https://api.open-meteo.com/v1/forecast?latitude={$parameters['latitude']}&longitude={$parameters['longitude']}&current=temperature_2m,relative_humidity_2m,apparent_temperature,is_day,precipitation,rain,showers,snowfall,cloud_cover,pressure_msl,surface_pressure,wind_speed_10m,wind_direction_10m,wind_gusts_10m&timezone=auto&forecast_days=1"
@@ -63,7 +70,12 @@ public function runTool($parameters): string
6370

6471
return json_encode($output, JSON_THROW_ON_ERROR);
6572
} catch (\Throwable $e) {
66-
return "Received an error looking up the weather: {$e->getMessage()}";
73+
return json_encode(['success' => false, "message" => "Received an error looking up the weather: {$e->getMessage()}"]);
6774
}
6875
}
76+
77+
public static function checkPermissions(modX $modx): bool
78+
{
79+
return true;
80+
}
6981
}

core/components/modai/src/Tools/ToolInterface.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ public static function getSuggestedName(): string;
3535
*/
3636
public static function getDescription(): string;
3737

38-
public static function checkPermissions(): bool;
38+
/**
39+
* Checks if user has permissions to run this tool.
40+
*
41+
* @param modX $modx
42+
* @return bool
43+
*/
44+
public static function checkPermissions(modX $modx): bool;
3945

4046
/**
4147
* Set the parameters that the LLM should or must provide when calling your function. Has to return valid JSON-schema.

0 commit comments

Comments
 (0)