定制 amnah/yii2-user 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

amnah/yii2-user

Composer 安装命令:

composer require amnah/yii2-user

包简介

Yii 2 user authentication module

README 文档

README

Yii 2 User - User authentication module

New version released 01/31/2016

This release contains a few small updates and bug fixes. Most notably, I've changed LoginForm.username to LoginForm.email and added a timezone field to app\models\Profile (thanks mnglkhn)

If there are any issues, let me know and I'll get to it asap.

Demo

Features

  • Quick setup - works out of the box so you can see what it does
  • Easily extendable
  • Registration using email and/or username
  • Login using email and/or username
  • Login/register via email (enter email > get link in inbox > click link to login/register)
  • Email confirmation (+ resend functionality)
  • Social authentication (facebook, twitter, google, linkedin, reddit, vkontakte)
  • Account page
    • Updates email, username, and password
    • Requires current password
  • Profile page
    • Lists custom fields for users, e.g., full_name
  • Password recovery
  • Admin crud via GridView

Installation

  • Install Yii 2 using your preferred method
  • Install package via composer "amnah/yii2-user": "^5.0"
  • Update config file config/web.php and 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' => 'amnah\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' => 'amnah\yii2\user\Module',
            // set custom module properties here ...
        ],
    ],
];
// app/config/db.php
return [
    'class' => 'yii\db\Connection',
    // set up db info
];
  • Run migration file
    • php yii migrate --migrationPath=@vendor/amnah/yii2-user/migrations
  • Go to your application in your browser
    • http://localhost/pathtoapp/web/user
    • note: go to user/login instead of site/login
  • Log in as admin using neo/neo (change it!)
  • Set up module properties as desired
  • Optional - Update the nav links in your main layout 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']] : // or ['/user/login-email']
        ['label' => 'Logout (' . Yii::$app->user->displayName . ')',
            'url' => ['/user/logout'],
            'linkOptions' => ['data-method' => 'post']],
],

Development Notes

How do I check user permissions?

This package contains a custom permissions system. Every user has a role, and that role has permissions in the form of database columns. It should follow the format: can_{permission name}.

For example, the role table has a column named can_admin by default. To check if the user can perform admin actions:

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
};

Add more database columns for permissions as needed. If you need something more powerful, look into setting up [RBAC] (https://github.com/yiisoft/yii2/blob/master/docs/guide/security-authorization.md#role-based-access-control-rbac).

Note: If you set up an authManager component for RBAC, then Yii::$app->user->can() will use that instead of this module's custom role table.

How do I add captcha to the forms?

Check out this great 3-step guide by dektrium. (Please note that the scenarios for the validation rules will depend on your project requirements.)

How do I add i18n?

// app/config/web.php
return [
    'components' => [
        'i18n' => [
            'translations' => [
                'user' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@app/messages', // example: @app/messages/fr/user.php
                ]
            ],
        ],
    ],
];

How do I extend this package?

You can extend the classes directly. Depending on which ones you need, set the proper config property:

// 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'  => [
            'User' => 'app\models\MyUser', // note: don't forget component user::identityClass above
            'Profile' => 'app\models\MyProfile',
        ],
        'emailViewPath' => '@app/mail/user', // example: @app/mail/user/confirmEmail.php
    ],
],

For view files, you can use the theme component.

// app/config/web.php
'components' => [
    'view' => [
        'theme' => [
            'pathMap' => [
                '@vendor/amnah/yii2-user/views' => '@app/views/user', // example: @app/views/user/default/login.php
            ],
        ],
    ],
],

I need more control. Can I just extend the whole thing?

You can always fork the package and modify it as needed.

Or, if you want, you can integrate the package directly into your app by copying the files. This would make it more difficult to get updates, but it also guarantees that your app won't break after running composer update.

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)
// app/config/console.php
'modules' => [
    'user' => [
        'class' => 'amnah\yii2\user\Module',
    ],
],
  • Use the php yii user/copy command. For a basic app, you can call the default command without any options
php yii user/copy --from=@vendor/amnah/yii2-user --to=@app/modules/user --namespace=app\\modules\\user
  • Update config to point to your new package
// 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 amnah\yii2\user with app\modules\user.

Todo

amnah/yii2-user 适用场景与选型建议

amnah/yii2-user 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 232.03k 次下载、GitHub Stars 达 251, 最近一次更新时间为 2013 年 11 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Authentication」 「user」 「auth」 「yii」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 amnah/yii2-user 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 amnah/yii2-user 我们能提供哪些服务?
定制开发 / 二次开发

基于 amnah/yii2-user 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 232.03k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 257
  • 点击次数: 23
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 251
  • Watchers: 37
  • Forks: 101
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-11-20