zakharov-andrew/yii2-user
Composer 安装命令:
composer require zakharov-andrew/yii2-user
包简介
Yii2 User
README 文档
README
Yii2 user authentication module for management users and their rights.
- Registration, authorization, password recovery, change email and so on
- User administration interface
- Supports role creation
- Multiple user roles are supported
- Happy Birthday widgets
- Birthday Calendar widget
- User Deputies Management
- logging of failed authorization attempts and blocking access via IP
- Supports languages: English, Russian
🚀 Installation
The preferred way to install this extension is through composer.
Either run
$ composer require zakharov-andrew/yii2-user
or add
"zakharov-andrew/yii2-user": "*"
to the require section of your composer.json file.
Subsequently, run
./yii migrate/up --migrationPath=@vendor/zakharov-andrew/yii2-user/migrations
in order to create the settings table in your database.
Or add to console config
return [ // ... 'controllerMap' => [ // ... 'migrate' => [ 'class' => 'yii\console\controllers\MigrateController', 'migrationPath' => [ '@console/migrations', // Default migration folder '@vendor/zakharov-andrew/yii2-user/src/migrations' ] ] // ... ] // ... ];
🛠 Usage
Add this to your main configuration's modules array
'modules' => [ 'user' => [ 'class' => 'ZakharovAndrew\user\Module', 'bootstrapVersion' => 5, // if use bootstrap 5 'showTitle' => true, // display H1 headings (default - true) 'enableUserSignup' => false, //Toggles user registration functionality (default - false) 'telegramToken' => '', // necessary for the bot to work 'telegramBotLink' => 'https://t.me/YOUR_BOT_NAME_FOR_USER_LINK', //change! // use for menu and access 'controllersAccessList' => [ 1001 => [ 'Users' => [ '/user/user/index' => 'users', '/user/user/create' => 'create user', ], ], 1002 => ['/user/roles/index' => 'Roles'] ], 'wallpapers' => [ [ 'url' => 'path/to/wallpaper1.jpg', 'roles' => ['user', 'admin'], // available to which roles ], [ 'url' => 'path/to/wallpaper2.jpg', 'roles' => ['admin'], // only for admin ], // ... ], ], // ... ], // ... 'components' => [ 'telegram' => [ 'class' => \ZakharovAndrew\user\components\Telegram::class, 'token' => env('TELEGRAM_BOT_TOKEN'), // Use environment variable 'apiUrl' => 'https://api.telegram.org/bot', // Can be changed 'timeout' => 30, 'maxRetries' => 3, 'debug' => YII_DEBUG, 'cache' => Yii::$app->cache ], 'birthdayNotifier' => [ 'class' => 'ZakharovAndrew\user\components\BirthdayNotifier', 'showAge' => true, 'showButtons' => true, 'ageTemplate' => "🎂 Age: {age} {years}\n", 'ageUnits' => 'years', ], // ... ],
Add this to your config\params.php
return [ // ... 'supportEmail' => 'change-this-email@test.com', // lifetime of the password reset token 'userResetPasswordTokenExpire' => 3600 // ... ];
If a pretty URL is enabled:
Add this to your main configuration's urlManager array
'urlManager' => [ //... 'rules' => [ 'login' => 'user/user/login', 'logout' => 'user/user/logout', 'profile' => 'user/user/profile', 'signup' => 'user/user/signup', 'wallpapers' => 'user/wallpaper/index', //... ], //... ],
📊 Controller Action Logging
The module provides comprehensive controller action logging for auditing and debugging purposes. When enabled, it automatically logs all controller actions with detailed information.
Features:
- ✅ User tracking - Logs user ID for authenticated users
- ✅ Action details - Records controller, action, and HTTP method
- ✅ Request data - Captures request parameters (sensitive data filtered)
- ✅ Performance metrics - Measures execution time
- ✅ Security information - Logs IP address and user agent
- ✅ Response codes - Records HTTP response status codes
Configuration
Enable logging in module configuration:
'modules' => [ 'user' => [ 'class' => 'ZakharovAndrew\user\Module', 'enableControllerLogging' => true, // Enable logging // ... other configurations ], ],
What Gets Logged
| Field | Description |
|---|---|
user_id | User ID (if authenticated) |
controller | Controller name |
action | Action name |
method | HTTP method (GET, POST, etc.) |
url | Request URL |
request_params | Request parameters (passwords/tokens filtered) |
response_code | HTTP response status code |
execution_time | Action execution time in seconds |
ip_address | Client IP address |
user_agent | Client browser/user agent |
Sensitive Data Protection
The logging system automatically filters sensitive information:
- Passwords (password, password_hash, etc.)
- Authentication tokens (auth_key, access_token, etc.)
- Security codes (cvv, security_code, etc.)
- Sensitive fields are replaced with HIDDEN in the logs.
🎉 Happy Birthday widget
You can use the birthday greeting widget by customizing both the header and the message indicating that there are no birthdays today:
<?= \ZakharovAndrew\user\components\BirthdayWidget::widget([ 'headerMessage' => 'Today’s birthdays:', 'noBirthdaysMessage' => 'Today, no one is celebrating a birthday.', // or empty 'useAvatars' => true ]); ?>
Widget for congratulating the user on his birthday:
<?= \ZakharovAndrew\user\components\BirthdayGreetingWidget::widget([ 'message' => '<h1>Happy Birthday, {username}!</h1>' ]) ?>
📅 Birthday Calendar Widget
Display a calendar of birthdays for the current week and next month:
<?= \ZakharovAndrew\user\widgets\BirthdayCalendarWidget::widget([ 'title' => 'Upcoming Birthdays', 'showAge' => true, 'maxUsersPerDay' => 3 ]); ?>
Options:
- title (string) - Calendar title
- showAge (bool) - Show user age (default: true)
- maxUsersPerDay (int) - Maximum number of users to show per day (default: 5)
Features:
- Shows birthdays from current week to next month
- 📆 Weekly grouping with week numbers
- 🎯 Current day highlighting
- 👥 Clickable user names linking to profiles
- 🔢 Age display with proper pluralization
- 📱 Responsive design for mobile devices
- 🌍 Multiple language support (English/Russian)
Advanced usage with custom parameters:
<?= \ZakharovAndrew\user\widgets\BirthdayCalendarWidget::widget([ 'title' => \ZakharovAndrew\user\Module::t('Upcoming Birthdays'), 'showAge' => true, 'maxUsersPerDay' => 5, 'view' => 'custom-calendar-view' // custom view file ]); ?>
👤 User Menu Widget
Add a user menu to your navigation bar with avatar, name and dropdown options:
Basic Usage
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Your App</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav me-auto">
<!-- Your menu items -->
</ul>
<ul class="navbar-nav">
<?= \ZakharovAndrew\user\widgets\UserMenuWidget::widget() ?>
</ul>
</div>
</div>
</nav>
Custom Guest Content
Customize the display for non-authenticated users:
<?= \ZakharovAndrew\user\widgets\UserMenuWidget::widget([ 'guestContent' => ' <div class="d-flex gap-2"> <a href="' . \yii\helpers\Url::to(['/user/auth/login']) . '" class="btn btn-outline-light btn-sm"> ' . \ZakharovAndrew\user\Module::t('Login') . ' </a> <a href="' . \yii\helpers\Url::to(['/user/auth/signup']) . '" class="btn btn-primary btn-sm"> ' . \ZakharovAndrew\user\Module::t('Signup') . ' </a> </div> ', ]) ?>
Advanced Configuration
<?= \ZakharovAndrew\user\widgets\UserMenuWidget::widget([ 'guestView' => 'custom-guest-view', // custom view file 'guestContent' => [ 'loginUrl' => ['/user/auth/login'], 'signupUrl' => ['/user/auth/signup'], ], ]) ?>
👥 User Deputies Management
The module now supports user deputies functionality, allowing users to have multiple deputies with date tracking and assignment history.
Basic Usage
// Get current user $user = User::findOne(1); // Add a deputy with validity period $user->addDeputy(2, '2024-01-01', '2024-12-31'); // Get all active deputies for a user $deputies = $user->getCurrentDeputies(); // Check if user has specific deputy $hasDeputy = $user->hasDeputy(2); // Check if user is deputy for another user $isDeputy = $user->isDeputyFor(1); // Remove deputy $user->removeDeputy(2); // Get users for whom current user is a deputy $deputyForUsers = $user->getCurrentDeputyForUsers();
Advanced Usage
// Add deputy with custom created by user $user->addDeputy(2, '2024-01-01', null, Yii::$app->user->id); // Get deputies list for dropdown $deputiesList = $user->getDeputiesList(); // Get available users for deputy assignment $availableUsers = User::getAvailableUsersForDeputy($currentUserId); // Check if deputy relation is currently active $deputy = UserDeputy::findOne(1); $isActive = $deputy->isCurrentlyActive();
Features
- ✅ Multiple deputies - Users can have multiple deputies
- ✅ Date tracking - Track assignment dates and validity periods
- ✅ Assignment history - Record who assigned each deputy
- ✅ Active/inactive status - Manage deputy status without deletion
- ✅ Date validation - Automatic validation of validity periods
- ✅ Relationship management - Easy methods for managing deputies
Telegram Usage
// In controller, widget, or any class Yii::$app->telegram->sendMessage($chatId, 'Hello!'); // Change token dynamically if needed Yii::$app->telegram->setToken('new_token'); // Change API URL (e.g., for local testing) Yii::$app->telegram->setApiUrl('https://test.telegram.org/bot');
👥 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📄 License
yii2-user it is available under a MIT License. Detailed information can be found in the LICENSE.md.
zakharov-andrew/yii2-user 适用场景与选型建议
zakharov-andrew/yii2-user 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 422 次下载、GitHub Stars 达 5, 最近一次更新时间为 2023 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「admin」 「module」 「extension」 「access」 「Users」 「profile」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zakharov-andrew/yii2-user 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zakharov-andrew/yii2-user 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zakharov-andrew/yii2-user 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
2lenet/EasyAdminPlusBundle
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
Adds more BBCode
统计信息
- 总下载量: 422
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 23
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-25
