使用SimpleAuth,您能够在不到5分钟的时间内为您的应用程序添加登陆和用户注册!SimpleAuth包含一个controller(SimpleAuthController),一个中间件(SimpleAuthMiddleware),一个库(Simple_auth)以及从Luthier CI Authentication Framework构建的其余元素。php
因为安装是经过 Built-in CLI Tools of Luthier CI, 内置CLI工具的命令完成的,所以请务必在路由文件中定义如下命令 cli .php
:css
<?php
# application/routes/cli.php
Luthier\Cli::maker(); // 'luthier make' command
Luthier\Cli::migrations(); // 'luthier migrate' command
复制代码
此外,还必须在启动以前正确配置与数据库(in application/config/database.php)和迁移(in application/config/migration.php)的链接。web
在应用程序的根文件夹中运行:数据库
php index.php luthier make auth
复制代码
若是一切顺利,您应该拥有如下新文件:数组
application
|- config
| |- auth.php
|
|- controllers
| |- SimpleAuthController.php
|
|- libraries
| |- Simple_Auth.php
|
|- middleware
| |- SimpleAuthMiddleware.php
|
|- migrations
| |- 20180516000000_create_users_table.php
| |- 20180516000001_create_password_resets_table.php
| |- 20180516000002_create_email_verifications_table.php
| |- 20180516000003_create_login_attempts_table.php
| |- 20180516000004_create_user_permissions_categories_table.php
| |- 20180516000005_create_user_permissions_table.php
|
|- security
| |- providers
| |- User.php
| |- UserProvider.php
复制代码
在应用程序的根文件夹中运行:安全
php index.php luthier migrate
复制代码
您应该可以看到如下输出:bash
MIGRATED: 20180516000000_create_users_table.php
MIGRATED: 20180516000001_create_password_resets_table.php
MIGRATED: 20180516000002_create_email_verifications_table.php
MIGRATED: 20180516000003_create_login_attempts_table.php
MIGRATED: 20180516000004_create_user_permissions_categories_table.php
MIGRATED: 20180516000005_create_user_permissions_table.php
复制代码
在您的web.php文件中,添加如下行:cookie
Route::auth();
复制代码
这是定义全部这些路线的快捷方式:session
Route::match(['get', 'post'], 'login', 'SimpleAuthController@login')->name('login');
Route::post('logout', 'SimpleAuthController@logout')->name('logout');
Route::get('email_verification/{token}', 'SimpleAuthController@emailVerification')->name('email_verification');
Route::match(['get', 'post'], 'signup', 'SimpleAuthController@signup')->name('signup');
Route::match(['get', 'post'], 'confirm_password', 'SimpleAuthController@confirmPassword')->name('confirm_password');
Route::group('password-reset', function(){
Route::match(['get','post'], '/', 'SimpleAuthController@passwordReset')->name('password_reset');
Route::match(['get','post'], '{token}', 'SimpleAuthController@passwordResetForm')->name('password_reset_form');
});
复制代码
若是您已正确执行全部步骤,则在访问该URL时,/login您应该会看到新的登陆屏幕:app
SimpleAuth控制器(SimpleAuthController)包含身份验证操做,如登陆,用户注册,密码重置等。它看起来相似于:
<?php
# application/controllers/SimpleAuthController.php
defined('BASEPATH') OR exit('No direct script access allowed');
/* (...) */
class SimpleAuthController extends Luthier\Auth\SimpleAuth\Controller {
/** * Sign up form fields * * (...) */
public function getSignupFields() {
return [ /* (...) */ ];
}
/** * Fillable database user fields * * (...) * @access public */
public function getUserFields() {
return [ /* (...) */ ];
}
}
复制代码
除非您想要自定义SimpleAuth,不然您不须要向此驱动程序添加任何其余内容,由于您扩展的类(Luthier\Auth\SimpleAuth\Controller)已经定义了身份验证逻辑,而且在路由文件中Route::auth()已经定义了应该指向此处的全部路由。
( Customize the user registration form ) 您能够根据本身的喜爱更改注册表单的字段。为此,getSignupFields()SimpleAuth驱动程序的方法必须返回一个定义其结构的数组,语法以下:
public function getSignupFields() {
return [
'Field name 1' => [
'Field type',
'Field label',
[ /* HTML5 attributes array */ ],
[ /* CI Validation rules array */] ,
[ /* CI Validation error essages array (Optional)*/]
],
'Field name 2' => [
'Field type',
'Field label',
[ /* ... */ ],
[ /* ... */ ] ,
],
// ( ... )
'Field name N' => [
'Field type',
'Field label',
[ /* ... */ ],
[ /* ... */ ] ,
]
];
}
复制代码
另外一方面,getUserFields()SimpleAuth驱动程序的方法必须返回一个数组,该数组包含将存储在新用户中的该表单的字段,其中数组的每一个元素都匹配该注册表单的字段和名称数据库中users表的列:
public function getUserFields() {
return [
'first_name',
'last_name',
'username',
'gender',
'email',
'password',
'role',
];
}
复制代码
Laravel用户会注意到这$fillable与EloquentORM模型的属性彻底相同,但应用于SimpleAuth用户注册表单。
SimpleAuth中间件 (SimpleAuthMiddleware
) 是须要用户预身份验证的路由的第一道防线。此中间件自动负责验证用户的当前状态:
您能够根据须要在尽量多的路由和路由组中使用SimpleAuth中间件,甚至能够将其与您本身的中间件结合使用,以添加额外的安全层。
例:
<?php
# application/routes/web.php
// SimpleAuth default routes:
Route::auth();
// Public routes:
Route::get('/', 'FrontendController@homepage')->name('homepage');
Route::get('/about', 'FrontendController@about')->name('about');
Route::match(['get','post'], '/contact', 'FrontendController@contact')->name('contact');
// Protected routes: access here without being authenticated will direct to the
// login screen
Route::group('dashboard', ['middleware' => ['SimpleAuthMiddleware']], function(){
Route::get('/', 'UserArea@dashboard');
});
复制代码
SimpleAuth库是Luthier CI身份验证框架类的包装器,Auth采用本机CodeIgniter库的格式,所以您可使用您应该已知的语法来使用它的全部方法。
要开始使用SimpleAuth库,您必须将其加载到框架中:
$this->load->library('Simple_auth');
复制代码
注意: Luthier\Auth
当您使用SimpleAuth时,并不是全部类的方法都相关,所以咱们仅列出可能有用的方法
要获取在应用程序中进行身份验证的用户,请使用user()返回用户对象的方法,或者NULL若是不存在通过身份验证的用户:
// The current user object:
$userObject = $this->simple_auth->user();
// With the user object you have access to:
// ...the user entity of the database:
$user = $userObject->getEntity();
// ...their roles:
$roles = $userObject->getRoles();
// ...and its permissions:
$permissions = $userObject->getPermissions();
复制代码
若是您使用默认的SimpleAuth用户提供程序,则能够直接访问当前用户的数据,而无需使用该getEntity()方法。如下表达式是等效的:
$this->simple_auth->user()->getEntity()->first_name;
$this->simple_auth->user()->first_name;
复制代码
要快速验证用户是否被邀请,请使用该isGuest()方法,TRUE若是用户还没有登陆,则返回该方法,FALSE不然:
$this->simple_auth->isGuest();
复制代码
要验证用户是否具备特定角色,请使用该方法isRole(role,或者FALSE若是他不拥有该角色,或者没有通过身份验证的用户,则使用该方法:
$this->simple_auth->isRole('ADMIN');
复制代码
要验证用户是否具备特定权限,请使用该方法isGranted($permission),该方法TRUE在用户具备权限时返回permission,或者FALSE若是用户没有该权限,或者没有通过身份验证的用户
例:
$this->simple_auth->isGranted('general.read');
复制代码
可使用替代语法来验证用户是否属于以特定短语/类别开头的角色:
// The following will give TRUE for permits that begin with 'general.'
$this->simple_auth->isGranted('general.*');
复制代码
访问控制列表(ACL)是一种可选的身份验证功能,用于为每一个通过身份验证的用户设置特定权限。所以,用户能够具备角色和若干分配的权限,以保证(或拒绝)访问应用程序的某些资源。
在SimpleAuth中没有用户组或相似的东西,用户权限存储在变量深度权限树中(子权限限制取决于您)。
请考虑如下权限:
ID NAME PARENT_ID
-----------------------------
1 general [null]
2 read 1
3 write 1
4 delete 1
5 local 4
6 global 4
复制代码
这个权限分配:
ID USERNAME PERMISSION_ID
---------------------------------
1 anderson 2
2 anderson 5
3 julio 3
4 julio 6
复制代码
例如,当用户anderson登陆时,您将拥有如下权限:
general.read
general.delete.local
复制代码
当用户julio登陆时,他将拥有如下权限:
general.write
general.delete.global
复制代码
权限树存储在user_permissions_categories表中,而权限分配存储在user_permissions表中,二者都由SimpleAuth中包含的迁移建立。没有自动建立或删除权限的方法,所以您必须手动执行此操做。
这些是SimpleAuth库中可用的ACL函数:
验证$permission访问控制列表(ACL)表中是否存在权限。
例:
$this->simple_auth->permissionExists('general.read');
复制代码
将权限分配username,TRUE若是操做成功FALSE则返回。
// Assigning the 'general.read' permission to the current user
$this->simple_auth->grantPermission('general.read');
复制代码
撤消对username,TRUE若是操做成功或FALSE以其余方式返回。
// Revoking the 'general.read' permission to the current user
$this->simple_auth->revokePermission('general.read');
复制代码
如下功能对于与用户身份验证相关的特殊任务很是有用:
TRUE若是用户彻底经过身份验证,FALSE则返回。彻底经过身份验证的用户是直接登陆而不是经过“记住我”功能登陆的用户。
'confirm_password'
) : [bool]$route若是用户未彻底经过身份验证,则会自动重定向到路径。此功能对于经过“记住我”功能再次请求通过身份验证的用户确认密码很是有用。
返回在标准下找到的用户的对象search,此方法执行三种类型的搜索:
例:
// It will search the user with ID 1
$this->simple_auth->searchUser(1);
// It will search the user with the username/email column equal to 'admin@admin.com'
$this->simple_auth->searchUser('admin@admin.com');
// It will search for the user whose column value 'gender' is 'm' and 'active' is equal to 1
$this->simple_auth->searchUser(['gender' => 'm', 'active' => 1]);
复制代码
更新在search,此方法执行两种不一样类型的更新:
例:
// It will replace the user's data with ID 1
$this->simple_auth->updateUser(1, ['first_name' => 'John']);
// It will replace the user's data with the user name / email column equal to 'admin@admin.com'
$this->simple_auth->searchUser('admin@admin.com', ['gender' => 'f']);
复制代码
使用data数组的每一个索引对应于用户表中的一个列,在simpleauth_users_table配置中定义
例:
$this->simple_auth->createUser(
[
'first_name' => 'Admin',
'last_name' => 'Admin',
'username' => 'admin',
'email' => 'admin@admin.com',
'password' => 'admin',
'gender' => 'm',
'role' => 'admin',
'verified' => 1
]
);
复制代码
若是列的名称与simpleauth_password_col配置中设置的名称匹配,则此函数会自动建立密码哈希
SimpleAuth使您能够在预约的设计(皮肤)之间进行选择或使用您本身的视图。SimpleAuth中包含的设计具备翻译成多种语言的优势。目前,支持的语言以下:
要更改视图中使用的外观,请修改simpleauth_skinSimpleAuth配置文件中的选项:
# application/config/auth.php
$config['simpleauth_skin'] = 'default';
复制代码
外观使用的语言取决于framework()主配置文件的languageoption($config['language'])的值application/config/config.php。若是在SimpleAuth支持的语言中找不到当前语言,english则将使用English()。
您可使用本身的视图,而无需覆盖SimpleAuth驱动程序方法。SimpleAuth总共使用了6个视图:
所以,要使用您本身的视图,只需在文件夹中建立一个文件,其中包含要替换的视图的名称simpleauth(若是它不存在,您必须先建立它)views。例如:
application/views/simpleauth/login.php
application/views/simpleauth/message.php
application/views/simpleauth/password_prompt.php
application/views/simpleauth/password_reset.php
application/views/simpleauth/password_reset_form.php
application/views/simpleauth/signup.php
复制代码
SimpleAuth配置位于application/config/auth.php文件中。接下来,简要说明每一个元素:
auth_login_route_redirect
在用户已通过身份验证的状况下将激活自动重定向到路径的路由。