Browse Source

Supports custom configuration saving for group and field validation

moell 1 year ago
parent
commit
e8eda9ff32

+ 21 - 2
app/Http/Controllers/API/ConfigController.php

@@ -3,7 +3,7 @@
 namespace App\Http\Controllers\API;
 
 use App\Http\Controllers\Controller;
-use App\Http\Requests\API\Config\SettingRequest;
+use App\Http\Requests\API\Config\AllowSettingConfig;
 use App\Models\Config;
 use Illuminate\Http\Request;
 
@@ -26,8 +26,27 @@ class ConfigController extends Controller
         ]);
     }
 
-    public function setting(SettingRequest $request)
+    public function setting(Request $request)
     {
+        $allowSettingConfig = new AllowSettingConfig();
+
+        foreach ($request->all() as $item) {
+            try {
+                $allowSettingConfig->check($item['group'], $item['key'], $item['value']);
+            } catch (\Exception $exception) {
+                return $this->forbidden($exception->getMessage());
+            }
+        }
+
+        foreach ($request->all() as $item) {
+            Config::query()->updateOrCreate([
+                'group' => $item['group'],
+                'key' => $item['key'],
+            ], [
+                'value' => $item['value']
+            ]);
+        }
 
+        return $this->noContent();
     }
 }

+ 48 - 0
app/Http/Requests/API/Config/AllowSettingConfig.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace App\Http\Requests\API\Config;
+
+class AllowSettingConfig
+{
+    public function check(string $group, string $key, mixed $value)
+    {
+        if (! method_exists($this, $group)) {
+            throw new \Exception(sprintf("%s configuration group is not supported", $group));
+        }
+
+        $groupRules = $this->$group();
+
+        if (! isset($groupRules[$key])) {
+            throw new \Exception(sprintf("The %s configuration field under the %s group is not supported", $key, $group));
+        }
+
+        $result = validator([$key => $value], [$key => $groupRules[$key]]);
+        if ($result->fails()) {
+            throw new \Exception($result->errors()->first());
+        }
+    }
+
+    /**
+     * email 字段验证
+     *
+     * @return string[]
+     */
+    private function email(): array
+    {
+        return [
+            "email_notification" => "in:on,off",
+            "async_sender" => "in:yes,no",
+            "sender_email" => "nullable|email",
+            "sender" => "nullable|min:1",
+            "domain" => "nullable|url",
+            "smtp_server" => "nullable|min:1",
+            "smtp_account" => "nullable|min:1",
+            "smtp_validation" => "in:yes,no",
+            "smtp_port" => "nullable|numeric",
+            "encryption" => "in:ssl,plain,tls",
+            "smtp_password" => "nullable|min:6",
+            "debug" => "in:off,normal,high",
+            "charset" => "in:utf8,gbk"
+        ];
+    }
+}

+ 0 - 29
app/Http/Requests/API/Config/SettingRequest.php

@@ -1,29 +0,0 @@
-<?php
-
-namespace App\Http\Requests\API\Config;
-
-use Illuminate\Foundation\Http\FormRequest;
-
-class SettingRequest extends FormRequest
-{
-    /**
-     * Determine if the user is authorized to make this request.
-     */
-    public function authorize(): bool
-    {
-        return true;
-    }
-
-    /**
-     * Get the validation rules that apply to the request.
-     *
-     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
-     */
-    public function rules(): array
-    {
-        return [
-            "group" => "required",
-            ""
-        ];
-    }
-}

+ 2 - 0
app/Models/Config.php

@@ -8,4 +8,6 @@ use Illuminate\Database\Eloquent\Model;
 class Config extends Model
 {
     use HasFactory;
+
+    protected $guarded = ['id'];
 }