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

refactor(http): optimize request authorization and validation #532

Merged
merged 7 commits into from
Jan 15, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ jobs:
uses: codecov/codecov-action@v4-beta
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: "./tests/coverage/index.xml"
file: "tests/coverage/index.xml"
95 changes: 0 additions & 95 deletions .github/workflows/codeql.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ vendor/
.vscode/
tests/cover
tests/coverage.xml
tests/coverage
tests/coding_standard.xml
tests/junit.xml
public
Expand Down
7 changes: 2 additions & 5 deletions app/Http/Admin/Request/PassportLoginRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace App\Http\Admin\Request;

use App\Http\Common\Request\Traits\NoAuthorizeTrait;
use Hyperf\Collection\Arr;
use Hyperf\Swagger\Annotation\Property;
use Hyperf\Swagger\Annotation\Schema;
Expand All @@ -27,11 +28,7 @@ class PassportLoginRequest extends FormRequest
{
use ClientIpRequestTrait;
use ClientOsTrait;

public function authorize(): bool
{
return true;
}
use NoAuthorizeTrait;

public function rules(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace App\Http\Admin\Request\Permission;

use App\Http\Common\Request\Traits\NoAuthorizeTrait;
use Hyperf\Swagger\Annotation\Property;
use Hyperf\Swagger\Annotation\Schema;
use Hyperf\Validation\Request\FormRequest;
Expand All @@ -24,10 +25,7 @@
)]
class BatchGrantPermissionsForRoleRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
use NoAuthorizeTrait;

public function rules(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace App\Http\Admin\Request\Permission;

use App\Http\Common\Request\Traits\NoAuthorizeTrait;
use Hyperf\Swagger\Annotation\Property;
use Hyperf\Swagger\Annotation\Schema;
use Hyperf\Validation\Request\FormRequest;
Expand All @@ -24,10 +25,7 @@
)]
class BatchGrantRolesForUserRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
use NoAuthorizeTrait;

public function rules(): array
{
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Admin/Request/Permission/MenuRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace App\Http\Admin\Request\Permission;

use App\Http\Common\Request\Traits\NoAuthorizeTrait;
use App\Schema\MenuSchema;
use Hyperf\Validation\Request\FormRequest;

Expand All @@ -24,10 +25,7 @@
)]
class MenuRequest extends FormRequest
{
public function authorize()
{
return true;
}
use NoAuthorizeTrait;

public function rules(): array
{
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Admin/Request/Permission/PermissionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace App\Http\Admin\Request\Permission;

use App\Http\Common\Request\Traits\NoAuthorizeTrait;
use App\Schema\UserSchema;
use Hyperf\Validation\Request\FormRequest;

Expand All @@ -23,10 +24,7 @@
)]
class PermissionRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
use NoAuthorizeTrait;

public function rules(): array
{
Expand Down
24 changes: 18 additions & 6 deletions app/Http/Admin/Request/Permission/RoleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace App\Http\Admin\Request\Permission;

use App\Http\Common\Request\Traits\HttpMethodTrait;
use App\Http\Common\Request\Traits\NoAuthorizeTrait;
use App\Schema\RoleSchema;
use Hyperf\Validation\Request\FormRequest;

Expand All @@ -23,20 +25,30 @@
)]
class RoleRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
use HttpMethodTrait;
use NoAuthorizeTrait;

public function rules(): array
{
return [
$rules = [
'name' => 'required|string|max:60',
'code' => 'required|string|max:60',
'code' => [
'required',
'string',
'max:60',
'regex:/^[a-zA-Z0-9_]+$/',
],
'status' => 'sometimes|integer|in:1,2',
'sort' => 'required|integer',
'remark' => 'nullable|string|max:255',
];
if ($this->isCreate()) {
$rules['code'][] = 'unique:role,code';
}
if ($this->isUpdate()) {
$rules['code'][] = 'unique:role,code,' . $this->route('id');
}
return $rules;
}

public function attributes(): array
Expand Down
10 changes: 4 additions & 6 deletions app/Http/Admin/Request/Permission/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace App\Http\Admin\Request\Permission;

use App\Http\Common\Request\Traits\NoAuthorizeTrait;
use App\Schema\UserSchema;
use Hyperf\Validation\Request\FormRequest;
use Mine\Swagger\Attributes\FormRequest as FormRequestAnnotation;
Expand Down Expand Up @@ -46,10 +47,7 @@
)]
class UserRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
use NoAuthorizeTrait;

public function rules(): array
{
Expand All @@ -58,8 +56,8 @@ public function rules(): array
'user_type' => 'required|integer',
'nickname' => ['required', 'string', 'max:60', 'regex:/^[^\s]+$/'],
'phone' => 'sometimes|string|max:12',
'email' => 'sometimes|string|max:60',
'avatar' => 'sometimes|string|max:255',
'email' => 'sometimes|string|max:60|email:rfc,dns',
'avatar' => 'sometimes|string|max:255|url',
'signed' => 'sometimes|string|max:255',
'status' => 'sometimes|integer',
'backend_setting' => 'sometimes|array|max:255',
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Admin/Request/UploadRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace App\Http\Admin\Request;

use App\Http\Common\Request\Traits\NoAuthorizeTrait;
use Hyperf\Swagger\Annotation\Property;
use Hyperf\Swagger\Annotation\Schema;
use Hyperf\Validation\Request\FormRequest;
Expand All @@ -24,10 +25,7 @@
)]
class UploadRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
use NoAuthorizeTrait;

public function rules(): array
{
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Api/Request/V1/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace App\Http\Api\Request\V1;

use App\Http\Common\Request\Traits\NoAuthorizeTrait;
use App\Schema\UserSchema;
use Hyperf\Validation\Request\FormRequest;

Expand All @@ -23,10 +24,7 @@
)]
class UserRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
use NoAuthorizeTrait;

public function rules(): array
{
Expand Down
41 changes: 41 additions & 0 deletions app/Http/Common/Request/Traits/HttpMethodTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact [email protected]
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/

namespace App\Http\Common\Request\Traits;

use Hyperf\Validation\Request\FormRequest;

/**
* @mixin FormRequest
*/
trait HttpMethodTrait
{
public function isCreate(): bool
{
return $this->isMethod('POST');
}

public function isUpdate(): bool
{
return $this->isMethod('PUT') || $this->isMethod('PATCH');
}

public function isDelete(): bool
{
return $this->isMethod('DELETE');
}

public function isSearch(): bool
{
return $this->isMethod('GET');
}
}
21 changes: 21 additions & 0 deletions app/Http/Common/Request/Traits/NoAuthorizeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact [email protected]
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/

namespace App\Http\Common\Request\Traits;

trait NoAuthorizeTrait
{
public function authorize(): bool
{
return true;
}
}
Loading
Loading