linkbee /
使用此包,我们可以管理项目中所有的用户。
v0.8.2
2020-10-11 13:09 UTC
Requires
- php: ^7.2.5
- laravel/framework: ^5.5|^6.0|^7.0|^8.0
- spatie/laravel-permission: ^2.37|^3.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.5|^8.0
Suggests
- Laravel User Management Routes: Because of changing the route file please replace user_management.php if you want to update version 0.6.5 or less than 0.6.5
- Vue js master file: Add vue folder in views resource if you want to update version 0.6.5 or less than 0.6.5
README
关于 Laravel User Management
管理所有用户,我们使用 "spatie/laravel-permission" 包来管理所有用户。当您安装此包时,"spatie/laravel-permission" 包及其依赖项将自动安装。在 "LaravelUserManagement" 包中,我们创建了所有数据库表、实体、种子、视图,用于管理用户、角色、权限和部门。
安装
- 通过 composer 安装包
composer require mekaeil/laravel-user-management
- 在您的 config/app.php 文件中添加服务提供者
'providers' => [
// ...
\Mekaeil\LaravelUserManagement\LaravelUserManagementProvider::class,
];
- 运行此命令以发布供应商
php artisan vendor:publish --provider="Mekaeil\LaravelUserManagement\LaravelUserManagementProvider"
- 发布供应商后,将以下代码添加到 database/DatabaseSeeder.php 文件的 "run" 方法中
public function run()
{
/*
|--------------------------------------------------------------------------
| SEEDERS FOR LARAVEL USER MANAGEMENT
|--------------------------------------------------------------------------
|
*/
$this->call(RoleTableSeeder::class);
$this->call(PermissionTableSeeder::class);
$this->call(DepartmentTableSeeder::class);
}
- 现在,如果您想更改配置(laravel_user_management):(您可以跳过此步骤)
/*
|--------------------------------------------------------------------------
| LARAVEL USER MANAGEMENT CONFIG
|--------------------------------------------------------------------------
|
|
*/
// laravel_user_management.users_table
'users_table' => 'users',
// laravel_user_management.user_department_table
'user_department_table' => 'user_departments',
/**
* THIS TABLE IS NAME OF THE MANY TO MANY RELATIONAL TABLE
* BETWEEN USERS TABLE & USER DEPARTMENTS TABLE
* **/
// laravel_user_management.user_department_user_table
'user_department_user_table' => 'user_departments_users',
// laravel_user_management.password_resets_table
'password_resets_table' => 'user_password_resets',
// laravel_user_management.user_model
'user_model' => App\Entities\User::class,
// laravel_user_management.row_list_per_page
'row_list_per_page' => 15,
// laravel_user_management.admin_url
'admin_url' => env('APP_URL').'/admin',
// laravel_user_management.logo_url
'logo_url'=> env('APP_URL'). "/mekaeils-package/images/logo-user-management.jpg",
'auth' => [
// laravel_user_management.auth.enable
'enable' => true,
// laravel_user_management.auth.login_url
'login_url' => 'user/login',
// laravel_user_management.auth.register_url
'register_url' => 'user/register',
// laravel_user_management.auth.logout_url
'logout_url' => 'user/logout',
// laravel_user_management.auth.username
'username' => 'email', // email OR mobile
/**
* DEFAULT ROLE FOR USERS WANT TO REGISTER ON WEBSITE
* YOU SHOULD DEFINE THIS ROLE IN SEEDER OR CREATE IT IN ADMIN PANEL
* **/
// laravel_user_management.auth.user_default_role
'user_default_role' => 'User',
/**
* DEFAULT STATUS FOR USERS WANT TO REGISTER ON WEBSITE
* IF IT'S SET ON 'PENDING' USER CAN NOT LOGIN IN WEBSITE
* AND NEED TO ACCEPT BY ADMINISTRATOR
* **/
// laravel_user_management.auth.default_user_status
'default_user_status' =>'accepted', /// 'pending','accepted','blocked'
// laravel_user_management.auth.dashboard_route_name_user_redirection
'dashboard_route_name_user_redirection' => 'home' /// ** ROUTE NAME **
],
- 如果您想设置权限表以进行自定义:(您可以跳过此步骤)
'models' => [
/*
* When using the "HasPermissions" trait from this package, we need to know which
* Eloquent model should be used to retrieve your permissions. Of course, it
* is often just the "Permission" model but you may use whatever you like.
*
* The model you want to use as a Permission model needs to implement the
* `Spatie\Permission\Contracts\Permission` contract.
*/
// 'permission' => Spatie\Permission\Models\Permission::class,
'permission' => Spatie\Permission\Models\Permission::class,
/*
* When using the "HasRoles" trait from this package, we need to know which
* Eloquent model should be used to retrieve your roles. Of course, it
* is often just the "Role" model but you may use whatever you like.
*
* The model you want to use as a Role model needs to implement the
* `Spatie\Permission\Contracts\Role` contract.
*/
// 'role' => Spatie\Permission\Models\Role::class,
'role' => Spatie\Permission\Models\Role::class,
],
'table_names' => [
/*
* When using the "HasRoles" trait from this package, we need to know which
* table should be used to retrieve your roles. We have chosen a basic
* default value but you may easily change it to any table you like.
*/
'roles' => 'roles',
/*
* When using the "HasPermissions" trait from this package, we need to know which
* table should be used to retrieve your permissions. We have chosen a basic
* default value but you may easily change it to any table you like.
*/
'permissions' => 'permissions',
/*
* When using the "HasPermissions" trait from this package, we need to know which
* table should be used to retrieve your models permissions. We have chosen a
* basic default value but you may easily change it to any table you like.
*/
'model_has_permissions' => 'model_has_permissions',
/*
* When using the "HasRoles" trait from this package, we need to know which
* table should be used to retrieve your models roles. We have chosen a
* basic default value but you may easily change it to any table you like.
*/
'model_has_roles' => 'model_has_roles',
/*
* When using the "HasRoles" trait from this package, we need to know which
* table should be used to retrieve your roles permissions. We have chosen a
* basic default value but you may easily change it to any table you like.
*/
'role_has_permissions' => 'role_has_permissions',
],
'column_names' => [
/*
* Change this if you want to name the related model primary key other than
* `model_id`.
*
* For example, this would be nice if your primary keys are all UUIDs. In
* that case, name this `model_uuid`.
*/
'model_morph_key' => 'model_id',
],
/*
* When set to true, the required permission/role names are added to the exception
* message. This could be considered an information leak in some contexts, so
* the default setting is false here for optimum safety.
*/
'display_permission_in_exception' => false,
'cache' => [
/*
* By default all permissions are cached for 24 hours to speed up performance.
* When permissions or roles are updated the cache is flushed automatically.
*/
'expiration_time' => \DateInterval::createFromDateString('24 hours'),
/*
* The cache key used to store all permissions.
*/
'key' => 'spatie.permission.cache',
/*
* When checking for a permission against a model by passing a Permission
* instance to the check, this key determines what attribute on the
* Permissions model is used to cache against.
*
* Ideally, this should match your preferred way of checking permissions, eg:
* `$user->can('view-posts')` would be 'name'.
*/
'model_key' => 'name',
/*
* You may optionally indicate a specific cache driver to use for permission and
* role caching using any of the `store` drivers listed in the cache.php config
* file. Using 'default' here means to use the `default` set in cache.php.
*/
'store' => 'default',
],
- 更新您的 config/auth.php 文件
use App\Entities\User;
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' => 'web',
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| 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' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
/*
|--------------------------------------------------------------------------
| 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' => User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| 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' => config('laravel_user_management.password_resets_table'),
'expire' => 60,
],
],
];
- 完成所有步骤后,运行以下命令
5.1 php artisan migrate
5.2 php artisan db:seed
- 如果您想使用 Vue.js,更改 "laravel_user_management" 配置文件
/**
* IN THIS PACKAGE WE USE THE VUE.JS FOR PAGES IF YOU
* WANT TO USE IT, ENABLE IT AND FOLLOW INSTALLATION STEPS IN README FILE.
* **/
'vue_theme' => true,
然后再次运行此命令
php artisan vendor:publish --provider="Mekaeil\LaravelUserManagement\LaravelUserManagementProvider"
现在,请按照页面底部的 USE VUE.JS FOR YOUR PROJECT 部分进行操作。
重要
After vendor:publish files you should change user migration file, because we set
mobile and email to nullable, one of them you want to set to username should not nullable in Database.
```
$table->string('email')->nullable()->unique();
$table->string('mobile')->nullable()->unique();
```
路由
安装包后,您可以在您的管理员面板中设置以下路由
- 用户管理
[method type: GET, url: domain.com/admin/user-management/user ]
admin.user_management.user.index
[method type: GET, url: domain.com/admin/user-management/user/create ]
admin.user_management.user.create
[method type: POST, url: domain.com/admin/user-management/user/store ]
admin.user_management.user.store
[method type: GET, url: domain.com/admin/user-management/user/edit/{ID} ]
admin.user_management.user.edit
[method type: PUT, url: domain.com/admin/user-management/user/update/{ID} ]
admin.user_management.user.update
[method type: DELETE, url: domain.com/admin/user-management/user/delete/{ID} ]
admin.user_management.user.delete
[method type: PUT, url: domain.com/admin/user-management/user/restore/{ID} ]
admin.user_management.user.restore
- 角色管理
[method type: GET, url: domain.com/admin/user-management/role ]
admin.user_management.role.index
[method type: GET, url: domain.com/admin/user-management/role/create ]
admin.user_management.role.create
[method type: POST, url: domain.com/admin/user-management/role/store ]
admin.user_management.role.store
[method type: GET, url: domain.com/admin/user-management/role/edit/{ID} ]
admin.user_management.role.edit
[method type: PUT, url: domain.com/admin/user-management/role/update/{ID} ]
admin.user_management.role.update
[method type: DELETE, url: domain.com/admin/user-management/role/delete/{ID} ]
admin.user_management.role.delete
- 权限管理
[method type: GET, url: domain.com/admin/user-management/permission ]
admin.user_management.permission.index
[method type: GET, url: domain.com/admin/user-management/permission/create ]
admin.user_management.permission.create
[method type: POST, url: domain.com/admin/user-management/permission/store ]
admin.user_management.permission.store
[method type: GET, url: domain.com/admin/user-management/permission/edit/{ID} ]
admin.user_management.permission.edit
[method type: PUT, url: domain.com/admin/user-management/permission/update/{ID} ]
admin.user_management.permission.update
[method type:DELETE, url:domain.com/admin/user-management/permission/delete/{ID} ]
admin.user_management.permission.delete
- 部门管理
[method type: GET, url: domain.com/admin/user-management/department ]
admin.user_management.department.index
[method type: GET, url: domain.com/admin/user-management/department/create ]
admin.user_management.department.create
[method type: POST, url: domain.com/admin/user-management/department/store ]
admin.user_management.department.store
[method type: GET, url: domain.com/admin/user-management/department/edit/{ID} ]
admin.user_management.department.edit
[method type: PUT, url: domain.com/admin/user-management/department/update/{ID} ]
admin.user_management.department.update
[method type:DELETE, url:domain.com/admin/user-management/department/delete/{ID} ]
admin.user_management.department.delete
- 认证
****
* IMPORTANT: THESE URL CAN BE CHANGE IN CONFIG FILE.
* THESE URLS ARE DEFAULT.
****
[method type: GET, url: domain.com/user/login ]
auth.user.login
[method type: POST, url: domain.com/user/login ]
auth.user.login
[method type: GET, url: domain.com/user/register ]
auth.user.register
[method type: POST, url: domain.com/user/register ]
auth.user.register
[method type: GET, url: domain.com/user/logout ]
auth.user.logout
演示
- 登录和注册
- 管理员面板和创建用户
USE VUE.JS FOR YOUR PROJECT
如果您想为项目使用 Vue.js,可以使用以下安装代替 bootstrap 主题。安装包后,请按照以下步骤操作
1. npm install vue
2. Add this section to your package.json file:
"dependencies": {
"v-tooltip": "^2.0.2",
"vue-carousel": "^0.18.0",
"vue-clickaway": "^2.2.2",
"vue-lazyload": "^1.3.3",
"vue-material": "^1.0.0-beta-11",
"vue-router": "^3.1.3"
}
在 webpack 文件中添加此命令
mix.js('resources/js/mekaeils-package/main.js', 'public/mekaeils-package');
编辑您的配置文件
/**
* IN THIS PACKAGE WE USE THE VUE.JS FOR PAGES IF YOU
* WANT TO USE IT, ENABLE IT AND FOLLOW INSTALLATION STEPS IN README FILE.
* **/
'vue_theme' => true, // true, false | default: false
VUE JS DEMO
- App Vue
- 登录
- 注册
- Material Kit 主题
更新
-
更新包以适应 Laravel 的新版本 => Laravel 6
-
VUE.JS FOR AUTH AND OTHER PAGES.(仅包含 vuejs 主题,没有功能如认证等)
进行中
- 在 Vuejs 主题中添加功能认证。
- 编辑结构方法以处理 API 响应。
测试
使用此命令可以运行测试。
./vendor/bin/phpunit
许可证
-
LaravelUserManagement 是开源软件,受 MIT 许可证 许可。
-
管理员模板(PurpleAdmin-Free-Admin-Template)由 Bootstrap Dash 提供
-
Vue Material Kit 由 Creative Tim 提供