You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.3 KiB
PHP

<?php
namespace Isekai\AIToolbox\Api\AIToolbox;
use ApiBase;
use Isekai\AIToolbox\Api\ApiAIToolbox;
use Isekai\AIToolbox\AIToolboxUtils;
use MediaWiki\MediaWikiServices;
use Wikimedia\ParamValidator\ParamValidator;
class ApiCheckAccess extends ApiBase {
/** @var MediaWikiServices */
private $services;
/** @var ApiAIToolbox */
private $mParent;
public function __construct(ApiAIToolbox $main, $method) {
parent::__construct($main->getMain(), $method);
$this->mParent = $main;
$this->services = MediaWikiServices::getInstance();
}
public function execute() {
$userAction = strtolower($this->getParameter('ataction'));
$user = $this->getUser();
if (!$user->isRegistered()) {
$this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission');
return false;
}
$result = $this->getResult();
$permissionMap = [
'embeddingpage' => 'ccembeddingpage',
'chatcomplete' => 'chatcomplete',
];
$permissionKey = $permissionMap[$userAction] ?? null;
if ($permissionKey && !$user->isAllowed($permissionKey)) {
$this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission');
$result->addValue(['aitoolbox', $this->getModuleName()], 'available', false);
return false;
}
$noCost = false;
if ($userAction === 'chatcomplete' && $user->isAllowed('aitoolbox-unlimited')) {
$noCost = true;
}
$isAvailable = true;
$result->addValue(['aitoolbox', $this->getModuleName()], 'available', $isAvailable);
$result->addValue(['aitoolbox', $this->getModuleName()], 'nocost', $noCost);
return true;
}
public function getAllowedParams($flags = 0) {
return [
'ataction' => [
ParamValidator::PARAM_TYPE => [
'embeddingpage',
'chatcomplete',
],
ParamValidator::PARAM_DEFAULT => null,
ParamValidator::PARAM_REQUIRED => true,
],
];
}
public function getCacheMode($params) {
return 'private';
}
public function getParent() {
return $this->mParent;
}
}