codersvn / usermanagement
Laravel / Lumen 的用户组件
Requires
- dingo/api: 2.0.0-alpha1
- laravel/socialite: ^3.1
- prettus/l5-repository: ^2.6
- tymon/jwt-auth: ^1.0@dev
README
用户组件包提供了一种方便的方式来管理应用程序的用户。
目录
安装
Composer
要将此包包含到您的项目中,请运行以下命令。
composer require codersvn/usermanagement
包安装完成后,下一步取决于您使用的框架。
服务提供者
Laravel
在您的 config/app.php
文件中,将以下服务提供者添加到 providers
数组的末尾
'providers' => [ ... Dingo\Api\Provider\LaravelServiceProvider::class, Tymon\JWTAuth\Providers\LaravelServiceProvider::class, Prettus\Repository\Providers\RepositoryServiceProvider::class, VCComponent\Laravel\User\Providers\UserComponentProvider::class, VCComponent\Laravel\User\Providers\UserComponentRouteProvider::class, VCComponent\Laravel\User\Providers\UserComponentEventProvider::class, ],
Lumen
在您的 bootstrap/app.php
文件中添加以下服务提供者。
$app->register(App\Providers\AuthServiceProvider::class); $app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class); $app->register(Dingo\Api\Provider\LumenServiceProvider::class); $app->register(Prettus\Repository\Providers\LumenRepositoryServiceProvider::class); $app->register(VCComponent\Laravel\User\Providers\LumenUserComponentProvider::class);
您还需要在 bootstrapp/app.php
中定义 route
。
$app->router->group([ ], function ($router) { require __DIR__ . '/../vendor/codersvn/usermanagement/src/routes.php'; });
配置和迁移
Laravel
运行以下命令以发布配置和迁移文件。
php artisan vendor:publish --provider="VCComponent\Laravel\User\Providers\UserComponentProvider"
php artisan vendor:publish --provider="Dingo\Api\Provider\LaravelServiceProvider"
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositoryServiceProvider"
创建表。
php artisan migrate
请删除 Laravel 默认的
users
迁移文件,以避免在运行迁移命令时发生冲突。
在 config/auth.php
中进行更改。
'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => VCComponent\Laravel\User\Entities\User::class, ], ],
Lumen
创建 config/auth.php
文件并添加以下内容。
<?php return [ /* |-------------------------------------------------------------------------- | Authentication Defaults |-------------------------------------------------------------------------- | | This option controls the default authentication "guard" and password | reset options for your application. You may change these defaults | as required, but they're a perfect start for most applications. | */ 'defaults' => [ 'guard' => env('AUTH_GUARD', 'api'), ], /* |-------------------------------------------------------------------------- | Authentication Guards |-------------------------------------------------------------------------- | | Next, you may define every authentication guard for your application. | Of course, a great default configuration has been defined for you | here which uses session storage and the Eloquent user provider. | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | Supported: "session", "token" | */ 'guards' => [ 'api' => [ 'driver' => 'jwt', 'provider' => 'users' ], ], /* |-------------------------------------------------------------------------- | User Providers |-------------------------------------------------------------------------- | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | If you have multiple user tables or models you may configure multiple | sources which represent each model / table. These sources may then | be assigned to any extra authentication guards you have defined. | | Supported: "database", "eloquent" | */ 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => VCComponent\Laravel\User\Entities\User::class, ], ], /* |-------------------------------------------------------------------------- | Resetting Passwords |-------------------------------------------------------------------------- | | Here you may set the options for resetting passwords including the view | that is your password reset e-mail. You may also set the name of the | table that maintains all of the reset tokens for your application. | | You may specify multiple password reset configurations if you have more | than one user table or model in the application and you want to have | separate password reset settings based on the specific user types. | | The expire time is the number of minutes that the reset token should be | considered valid. This security feature keeps tokens short-lived so | they have less time to be guessed. You may change this as needed. | */ 'passwords' => [ 'users' => [ 'provider' => 'users', 'table' => 'password_resets', 'expire' => 60, ], ], ];
创建以下迁移文件。
php artisan make:migration create_password_resets_table
php artisan make:migration create_users_table
php artisan make:migration create_statuses_table
php artisan make:migration create_user_meta_table
将这些内容添加到相应的迁移文件中
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePasswordResetsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('password_resets', function (Blueprint $table) { $table->string('email')->index(); $table->string('token'); $table->timestamp('created_at')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('password_resets'); } }
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('email', 40); $table->string('username', 100); $table->string('first_name', 100); $table->string('last_name', 100); $table->string('password', 100); $table->dateTime('last_login'); $table->boolean('email_verified')->default(0); $table->integer('status'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateStatusesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('statuses', function (Blueprint $table) { $table->increments('id'); $table->string('name', 20); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('statuses'); } }
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUserMetaTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('user_meta', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->string('key'); $table->text('value'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('user_meta'); } }
创建表。
php artisan migrate
环境
在 .env
文件中,我们需要一些配置。
API_PREFIX=api
API_VERSION=v1
API_NAME="Your API Name"
API_DEBUG=false
在 .env
文件中生成 JWT_SECRET
。
php artisan jwt:secret
现在该包已准备好使用。
配置
URL 命名空间
为了避免与应用程序的 API 端点重复,该包为它的路由提供了默认命名空间,即 user-management
。例如
{{url}}/api/user-management/admin/users
您可以通过修改 .env
文件中的 USER_MANAGEMENT_NAMESPACE
变量来将包的 URL 命名空间修改为您想要的任何内容。
USER_MANAGEMENT_NAMESPACE="your-namespace"
用户模型
您可以通过修改 config/auth.php
使用您自己的 User
模型。
'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Entities\User::class, ], ],
您的 User
模型必须包含以下内容。
<?php namespace App\Entities; use Illuminate\Auth\Authenticatable; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; use Illuminate\Database\Eloquent\Model; // use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Hash; use VCComponent\Laravel\User\Contracts\UserManagement; use VCComponent\Laravel\User\Contracts\UserSchema; use VCComponent\Laravel\User\Notifications\MailResetPasswordToken; use VCComponent\Laravel\User\Traits\UserManagementTrait; use VCComponent\Laravel\User\Traits\UserSchemaTrait; use Prettus\Repository\Contracts\Transformable; use Prettus\Repository\Traits\TransformableTrait; use Tymon\JWTAuth\Contracts\JWTSubject; use Tymon\JWTAuth\Facades\JWTAuth; class User extends Model implements AuthenticatableContract, JWTSubject, Transformable, UserManagement, UserSchema, CanResetPasswordContract { use Authenticatable, TransformableTrait, UserManagementTrait, UserSchemaTrait, // Notifiable, CanResetPassword; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'email', 'username', 'first_name', 'last_name', 'avatar', ]; /** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = [ 'password', ]; /** * Get the identifier that will be stored in the subject claim of the JWT. * * @return mixed */ public function getJWTIdentifier() { return $this->getKey(); } /** * Return a key value array, containing any custom claims to be added to the JWT. * * @return array */ public function getJWTCustomClaims() { return []; } public function setPasswordAttribute($value) { $this->attributes['password'] = Hash::make($value); } public function getEmailVerifyToken() { return Hash::make($this->email); } public function sendPasswordResetNotification($token) { // $this->notify(new MailResetPasswordToken($token)); } public function getToken() { return JWTAuth::fromUser($this); } }
用户转换器
Laravel
您可以通过修改 config/user.php
使用您自己的 UserTransformer
类。
'transformers' => [ 'user' => App\Transformers\UserTransformer::class, ],
Lumen
创建 config/user.php
并包含以下内容。
<?php return [ 'namespace' => env('USER_MANAGEMENT_NAMESPACE', 'user-management'), 'transformers' => [ 'user' => VCComponent\Laravel\User\Transformers\UserTransformer::class, ], ];
现在您可以修改 UserTransformer
类。
社交登录
要使用包提供的社交登录 API,您必须在 config/services.php
中添加以下配置。
'facebook' => [ 'client_id' => env('FACEBOOK_CLIENT_ID'), 'client_secret' => env('FACEBOOK_CLIENT_SECRET'), 'redirect' => env('FACEBOOK_CALLBACK_URL'), ], 'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID'), 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 'redirect' => env('GOOGLE_CALLBACK_URL'), ],
用户模型
用户架构
默认情况下,该包在 users
表中提供了一些非常基本的字段。在您的应用程序中,您可能希望在自己的 users
表中拥有自己的字段以满足应用程序的需求。这可以通过包内的 用户元 系统实现,该系统可以存储您包含的任何附加字段数据。
该包提供了一种通过 schema()
方法描述您附加字段的方式。在 schema()
中,您需要定义字段的 name
、字段 type
和字段验证 rule
。
您只需在您的 User
模型中声明 schema()
方法即可。
public static function schema() { return [ 'address' => [ 'type' => 'string', 'rule' => ['required'] ], 'phone_number' => [ 'type' => 'string', 'rule' => ['required', 'regex:/^\d+$/', 'min:9', 'max:15'] ], ]; }
用户管理
在您的应用程序中,您可能想要确定用户是否有权访问用户资源。该包提供了UserManagementTrait
,其中包含授予访问权限的逻辑。UserMangementTrait
包含5个方法:ableToShow()
、ableToCreate()
、ableToUpdate()
、ableToUpdateProfile()
、ableToDelete()
。这些方法将执行检查逻辑,然后返回布尔值。
要使用自己的逻辑覆盖默认逻辑,您只需要在您的 User
模型中声明这些方法。
public function ableToUpdateProfile($id) { if ($this->id == $id || $this->isRole('admin')) { return true; } return false; }
有关这些方法的更多详细信息,请查看
VCComponent\Laravel\User\Traits\UserManagementTrait
。
API 列表
以下是该包提供的API列表。
路由
自定义路由
要使用自己的路由,您需要在 config/app.php
中移除 VCComponent\Laravel\User\Providers\UserComponentRouteProvider
。
现在您可以手动创建并使用自己的路由。
自定义控制器
您可以使用自己的 UserController
来自定义API功能。
为了确保您的更改不会破坏其他功能,您的 UserController
需要扩展 VCComponent\Laravel\User\Http\Controller\ApiController
,并为管理员控制器使用 UserAdminMethods
特性,为前端控制器使用 UserFrontendMethods
特性。
附加配置
该包包含3个其他包,分别是 dingo/api
、tymon/jwt-auth
、prettus/l5-repository
。
有关这些包的其他配置,请遵循它们的文档。