dnj / laravel-aaa
为您的下一个 Laravel 项目提供认证 + 授权 + 账户功能!
Requires
- php: ^8.1
- dnj/laravel-localization: ^1.0.0
- dnj/laravel-user-logger: ^1.1.2
- yeganemehr/laravel-support: ^1.0.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.11
- orchestra/testbench: ^7.0
- phpstan/phpstan: ^1.4.1
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-03 16:01:59 UTC
README

Laravel-AAA,一个可定制且现代化的认证 + 授权 + 账户包,适用于您的laravel项目
讨论 • 文档
Laravel-AAA 是什么?
认证是网络应用最关键和基本的功能之一。像 Laravel 这样的网络框架为用户提供了许多认证方法。
您可以通过 Laravel 实现快速安全的认证功能。然而,如果实现这些认证功能不当,则可能存在风险,因为恶意当事人可能会利用这些功能。
但,我们从头开始构建了一个完全可定制的包来解决这个!
以下是一些 Laravel-AAA 的功能总结:
- 安全!
- 易于使用。
- 完全可定制。
- 应用最佳实践。
- 使用最新版本的 PHP 和 PHPUnit
- 最重要的是,它是免费的开源软件(FOSS)!
Laravel-AAA 目前正在积极开发中。您可以在我们的 讨论页面 提出建议和反馈。
如何开始
首先,您需要一个 Laravel 安装。您可以通过运行以下命令开始一个 Laravel 项目:
composer create-project laravel/laravel my-awesome-laravel-app
或者,如果您已经有了 Laravel,可以通过运行以下命令在项目中安装 laravel-aaa:
composer require dnj/laravel-aaa
然后,使用此命令发布配置:
php artisan vendor:publish --tag=config
这将产生以下结果:
INFO Publishing [config] assets.
Copying file [vendor/dnj/laravel-aaa/config/aaa.php] to [config/aaa.php] .................................................................... DONE
Copying file [vendor/dnj/laravel-user-logger/config/user-logger.php] to [config/user-logger.php] ............................................ DONE
如您所见,有一个 aaa.php 配置文件,我们将在以后使用它。
概念
我们处理一些概念,例如:类型
、类型能力
、类型本地化详细信息
、用户
和 用户名
类型
我们在应用程序中定义了一些 类型
,假设 类型
是用户级别,它定义了用户可以访问应用程序的哪些功能。类型有一个 id、一个可以定义为任何语言的本地化名称、可能有一个父级、可能有子级,以及一些能力。
类型能力
将其视为用户的权限,每个能力都属于一个 类型
。
用户
每个用户都有一个类型,该类型定义了该用户可以访问应用程序的哪些功能。
用户名
每个用户可能有一些用户名,用户名可以是任何东西,例如:电子邮件、手机、Telegram ID 等。
aaa.php
在这个文件中,您可以定义访客类型,如果用户不在您的应用程序中(或未登录),应用程序将使用此类型来检查权限。在 aaa.php
中的 guestType
属性中定义访客类型的 ID。
[ 'guestType' => 2, // The id of guest type ];
策略
Laravel-AAA 提供了对 策略 的支持。您可以通过运行以下命令来定义策略:
php artisan make:policy:aaa
然后,将您的策略添加到 AuthServiceProvider
中。
<?php namespace App\Providers; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use App\Models\Post; use App\Policies\PostPolicy; class AuthServiceProvider extends ServiceProvider { /** * The model to policy mappings for the application. * * @var array<class-string, class-string> */ protected $policies = [ Post::class => PostPolicy::class, ]; /** * Register any authentication / authorization services. * * @return void */ public function boot() { $this->registerPolicies(); /// } }
现在,您可以像这样检查权限:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Gate; class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function getPost(Request $request) { $post = $request->get('post'); $response = Gate::inspect('view_post_permission', $post); if ($response) { // allow } else { // deny } return view('view_post', ['title' => $post->getTitle()]); } }
类型管理
ITypeManager
接口提供创建、更新和删除类型的方法。以下是如何创建新类型的示例:
use dnj\AAA\Contracts\ITypeManager; use dnj\AAA\Contracts\IType; class TypeController extends Controller { private $typeManager; public function __construct(ITypeManager $typeManager) { $this->typeManager = $typeManager; } public function createType() { // the name of this type in diffrent languages $localizedDetails = [ 'en' => ['title' => 'Admin'], 'fr' => ['title' => 'Administratrice'], 'nl' => ['title' => 'beheerder'], ]; // abilities that we want to define for this type (user role) $abilities = [ 'add_post', 'edit_post', 'remove_post', 'publish_post', ] // the subtype IDs, consider this type as parent of other types, that means this type will have permission to another types $childIds = [ // id ]; // if you want to define any other data for this type, use this field $meta = [ 'notes' => 'Some notes about this type', ]; // save log for this action $userActivityLog = true; $type = $this->typeManager->create($localizedDetails, $abilities, $childIds, $meta, $userActivityLog); } public function updateType(IType $type, array $update) { $changes = []; if (isset($update['localizedDetails'])) { $changes['localizedDetails'] = $update['localizedDetails']; } if (isset($update['abilities'])) { $changes['abilities'] = $update['abilities']; } if (isset($update['childIds'])) { $changes['childIds'] = $update['childIds']; } if (isset($update['meta'])) { $changes['meta'] = $update['meta']; } // save log for this action $userActivityLog = true; $type = $this->typeManager->create($type, $changes, $userActivityLog); } public function deleteType(IType $type) { // save log for this action $userActivityLog = true; $this->typeManager->delete($type, $userActivityLog); } }
用户管理
IUserManager
接口提供创建、更新和删除用户的方法。以下是如何创建新用户的示例:
use dnj\AAA\Contracts\IUserManager; use dnj\AAA\Contracts\IUser; use dnj\AAA\Contracts\ITypeManager; use dnj\AAA\Contracts\IType; class UserController extends Controller { private $userManager; public function __construct(IUserManager $userManager) { $this->userManager = $userManager; } public function createUser() { // name of the user $name = 'Hossein Hosni' // username of the user $username = 'hosni'; // or cellphone number, or any unique thing // password of the user $password = 'SomeStrongPassword@123'; // the id of the user, or the Type object $type = 1; // or create user as guest $type = app(ITypeManager::class)->getGuestTypeID(); // if you want to define any other data for this user, use this field $meta = [ 'notes' => 'Some notes about this user', ]; // save log for this action $userActivityLog = true; $user = $this->userManager->create($name, $username, $type, $meta, $userActivityLog); } public function updateUser(IUser $user, array $update) { $changes = []; if (isset($update['type'])) { $changes['type'] = $update['type']; } if (isset($update['usernames'])) { $changes['usernames'] = $update['usernames']; } // save log for this action $userActivityLog = true; $user = $this->userManager->create($user, $changes, $userActivityLog); } public function deleteUser(IUser $user) { // save log for this action $userActivityLog = true; $this->userManager->delete($user, $userActivityLog); } }
错误报告
如果您发现任何错误,请通过在我们的 问题页面 提交问题并进行详细说明。提供一些截图也会非常有帮助。
功能请求
您也可以在我们的问题页面或讨论区提交功能请求,我们会尽快实现。如果您想为这个项目贡献力量,请为这个项目贡献力量。
开发
如果您想在本地系统运行此项目,请按照以下指南操作
-
分支此项目
-
使用以下命令将项目克隆到本地系统
$ git clone https://github.com/<your_github_username>/laravel-aaa.git
- 切换到项目的根目录
$ cd laravel-aaa
- 使用
composer
安装所有依赖
$ composer install
- 以开发模式运行项目。