更新站点配置文件验证规则

This commit is contained in:
Chenx221 2024-03-30 14:49:29 +08:00
parent 243a0a4b9c
commit 901e7ae77b
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021

View File

@ -30,8 +30,30 @@ class SiteConfig extends Model
public function rules(): array public function rules(): array
{ {
return [ return [
[['siteTitle', 'siteUrl', 'domain', 'verifyProvider', 'recaptchaSiteKey', 'recaptchaSecret', 'hcaptchaSiteKey', 'hcaptchaSecret', 'turnstileSiteKey', 'turnstileSecret', 'ipinfoToken'], 'string'], [['siteTitle', 'domain'], 'required'],
[['siteTitle', 'domain', 'verifyProvider', 'recaptchaSiteKey', 'recaptchaSecret', 'hcaptchaSiteKey', 'hcaptchaSecret', 'turnstileSiteKey', 'turnstileSecret', 'ipinfoToken'], 'string'],
[['registrationEnabled', 'enableIpinfo'], 'boolean'], [['registrationEnabled', 'enableIpinfo'], 'boolean'],
['verifyProvider', 'in', 'range' => ['reCAPTCHA', 'hCaptcha', 'Turnstile', 'None']],
[['recaptchaSiteKey', 'recaptchaSecret'], 'required', 'when' => function ($model) {
return $model->verifyProvider == 'reCAPTCHA';
}, 'whenClient' => "function (attribute, value) {
return $('#siteconfig-verifyprovider').val() == 'reCAPTCHA';
}"],
[['hcaptchaSiteKey', 'hcaptchaSecret'], 'required', 'when' => function ($model) {
return $model->verifyProvider == 'hCaptcha';
}, 'whenClient' => "function (attribute, value) {
return $('#siteconfig-verifyprovider').val() == 'hCaptcha';
}"],
[['turnstileSiteKey', 'turnstileSecret'], 'required', 'when' => function ($model) {
return $model->verifyProvider == 'Turnstile';
}, 'whenClient' => "function (attribute, value) {
return $('#siteconfig-verifyprovider').val() == 'Turnstile';
}"],
['ipinfoToken', 'required', 'when' => function ($model) {
return $model->enableIpinfo;
}, 'whenClient' => "function (attribute, value) {
return $('#siteconfig-enableipinfo').is(':checked');
}"],
]; ];
} }
@ -86,7 +108,7 @@ class SiteConfig extends Model
{ {
try { try {
$env = parse_ini_file(Yii::getAlias('@app/.env')); $env = parse_ini_file(Yii::getAlias('@app/.env'));
if($env === false) { if ($env === false) {
return false; return false;
} }
$env['SITE_TITLE'] = $this->siteTitle; $env['SITE_TITLE'] = $this->siteTitle;
@ -104,7 +126,7 @@ class SiteConfig extends Model
$data = array_map(function ($key, $value) { $data = array_map(function ($key, $value) {
return "$key=$value"; return "$key=$value";
}, array_keys($env), $env); }, array_keys($env), $env);
return !(file_put_contents('.env', implode("\n", $data)) == false); return !(file_put_contents(Yii::getAlias('@app/.env'), implode("\n", $data)) == false);
} catch (Exception) { } catch (Exception) {
return false; return false;
} }