kongoon / yii2-user
Yii 2 用户认证模块
dev-master
2015-04-25 05:41 UTC
Requires
This package is not auto-updated.
Last update: 2024-09-18 08:07:22 UTC
README
Yii 2 用户 - 用户认证模块
特性
- 快速设置 - 直接使用即可了解其功能
- 易于扩展
- 使用邮箱和/或用户名进行注册
- 使用邮箱和/或用户名进行登录
- 邮箱确认(+重发功能)
- 社交认证(Facebook、Twitter、Google、LinkedIn、Reddit、Vkontakte)
- 账户页面
- 更新邮箱、用户名和密码
- 需要当前密码
- 个人资料页面
- 列出用户自定义字段,例如 full_name
- 密码恢复
- 通过 GridView 进行管理员 CRUD
安装
- 使用您喜欢的方案安装 Yii 2
- 使用 composer 安装包
`
"kongoon/yii2-user": "dev-master"`
- 更新配置文件 config/web.php 和 config/db.php
// app/config/web.php
return [
'components' => [
// NOTE: in the yii2-advanced-app, the user component should be updated in
// 'frontend/config/main.php' and/or 'backend/config/main.php' (OR you can add it
// to 'common/config' if you remove it from frontend/backend)
'user' => [
'class' => 'kongoon\yii2\user\components\User',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => true,
'messageConfig' => [
'from' => ['admin@website.com' => 'Admin'], // this is needed for sending emails
'charset' => 'UTF-8',
]
],
],
'modules' => [
'user' => [
'class' => 'kongoon\yii2\user\Module',
// set custom module properties here ...
],
],
];
// app/config/db.php
return [
'class' => 'yii\db\Connection',
// set up db info
];
- 运行迁移文件
- 在浏览器中访问您的应用程序
- 使用
`
neo/neo`
(请更改它!)作为管理员登录 - 根据需要设置 模块属性
- 可选 - 更新主布局中的导航链接 app/views/layouts/main.php
// app/views/layouts/main.php
<?php
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
['label' => 'User', 'url' => ['/user']],
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/user/login']] :
['label' => 'Logout (' . Yii::$app->user->displayName . ')',
'url' => ['/user/logout'],
'linkOptions' => ['data-method' => 'post']],
],
发行说明
- 2014/11/28 - 发行版 2.1.0
- 2014/10/13 - 发行版 2.1.0-RC
- 2014/8/1 - 发行版 2.1.0-alpha4
- 2014/6/29 - 发行版 2.1.0-alpha3
- 2014/6/12 - 发行版 2.1.0-alpha2
- 2014/5/19 - 发行版 2.1.0-alpha
- 2014/4/28 - 发行版 2.0.0-alpha(《升级说明》)
- 2014/4/17 - 发行版 1.0.0-beta
开发说明
如何检查用户权限?
此包包含自定义权限系统。每个用户都有一个角色,该角色具有数据库列形式的权限。应遵循以下格式:`
can_{permission name}`
。
例如,默认情况下,`
role表有一个名为
can_admin`
的列。要检查用户是否可以执行管理员操作
if (!Yii::$app->user->can("admin")) {
throw new HttpException(403, 'You are not allowed to perform this action.');
}
// --- or ----
$user = User::findOne(1);
if ($user->can("admin")) {
// do something
};
根据需要添加更多数据库列以设置权限。如果您需要更强大的功能,请考虑设置 [RBAC](《https://github.com/yiisoft/yii2/blob/master/docs/guide/security-authorization.md#role-based-access-control-rbac》)
注意:如果您设置了 `
authManager组件以设置 RBAC,则
Yii::$app->user->can()将使用该组件而不是此模块的自定义
role表。
如何将验证码添加到表单中?
请参阅 dektrium 编写的这个优秀的 3 步 指南。(请注意,验证规则的场景将取决于您的项目要求。)
如何添加 i18n?
// app/config/web.php
return [
'components' => [
'i18n' => [
'translations' => [
'user' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@app/messages', // example: @app/messages/fr/user.php
]
],
],
],
];
如何扩展此包?
您可以直接扩展类。根据您的需要,设置适当的配置属性
// app/config/web.php
'components' => [
'user' => [
'class' => 'app\components\MyUser',
'identityClass' => 'app\models\MyUser',
],
],
'modules' => [
'user' => [
'class' => 'app\modules\MyModule',
'controllerMap' => [
'default' => 'app\controllers\MyDefaultController',
],
'modelClasses' => [
'Profile' => 'app\models\MyProfile',
],
'emailViewPath' => '@app/mail/user', // example: @app/mail/user/confirmEmail.php
],
],
对于视图文件,您可以使用 `
theme`
组件。
// app/config/web.php
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'@vendor/kongoon/yii2-user/views' => '@app/views/user', // example: @app/views/user/default/login.php
],
],
],
],
我需要更多控制。我能否只扩展整个包?
您始终可以将其分叉并按需修改。
或者,如果您愿意,您可以将包直接集成到您的应用程序中,通过复制文件。这将使更新更困难,但同时也保证了在运行后您的应用程序不会损坏
To do so, you can use the helper command ```CopyController```.
* Add the module to your *config/console.php* to gain access to the command (**Note: this is CONSOLE config**)
```php
// app/config/console.php
'modules' => [
'user' => [
'class' => 'kongoon\yii2\user\Module',
],
],
```
* Use the ```php yii user/copy``` command. For a [basic app]
(https://github.com/yiisoft/yii2-app-basic), you can call the default command without any options
```
php yii user/copy --from=@vendor/kongoon/yii2-user --to=@app/modules/user --namespace=app\\modules\\user
```
* Update config to point to your new package
```php
// app/config/web.php + app/config/console.php
'modules' => [
'user' => [
'class' => 'app\modules\user\Module',
],
],
```
**Alternatively,** you can do this manually. Just copy/paste the files wherever you'd like and
change the namespaces in the files. Replace ```kongoon\yii2\user``` with ```app\modules\user```.
## Todo
* Tests
* Issues/requests? Submit a [github issue](https://github.com/kongoon/yii2-user/issues)