Module.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/26
  6. * Time: 下午3:32
  7. */
  8. namespace common\modules\user;
  9. use common\behaviors\UserBehaviorBehavior;
  10. use common\modules\user\clients\QqAuth;
  11. use common\modules\user\clients\WeiboAuth;
  12. use common\modules\user\clients\WeixinAuth;
  13. use Yii;
  14. use yii\authclient\clients\GitHub;
  15. use yii\base\BootstrapInterface;
  16. use yii\web\User;
  17. class Module extends \common\modules\Module implements BootstrapInterface
  18. {
  19. public $enableGeneratingPassword = true;
  20. /** @var bool Whether to show flash messages. */
  21. public $enableFlashMessages = true;
  22. /** @var bool Whether to enable registration. */
  23. public $enableRegistration = true;
  24. /** @var bool Whether user has to confirm his account. */
  25. public $enableConfirmation = true;
  26. /** @var bool Whether to allow logging in without confirmation. */
  27. public $enableUnconfirmedLogin = false;
  28. /** @var bool Whether to enable password recovery. */
  29. public $enablePasswordRecovery = true;
  30. /** @var bool Whether user can remove his account */
  31. public $enableAccountDelete = false;
  32. /** @var string rbac默认管理员permission名 */
  33. public $adminPermission = 'admin';
  34. public $admins = [];
  35. public $defaultPassword = '111111';
  36. public $urlPrefix = 'user';
  37. public $urlRules = [];
  38. public function init()
  39. {
  40. parent::init();
  41. if (!isset(Yii::$app->i18n->translations['user'])) {
  42. Yii::$app->i18n->translations['user'] = [
  43. 'class' => 'yii\i18n\PhpMessageSource',
  44. 'basePath' => '@common/modules/user/messages'
  45. ];
  46. }
  47. }
  48. public function bootstrap($app)
  49. {
  50. if ($app->id == 'backend') {
  51. Yii::$app->set('user', [
  52. 'class' => 'yii\web\User',
  53. 'identityClass' => 'common\modules\user\models\User',
  54. // 'loginUrl' => ['/user/default/qrcode-login'],
  55. 'loginUrl' => ['/user/default/login'],
  56. 'enableAutoLogin' => true,
  57. 'on afterLogin' => function($event) {
  58. $event->identity->touch('login_at');
  59. },
  60. 'idParam' => '__idBackend',
  61. 'identityCookie' => ['name' => '_identityBackend', 'httpOnly' => true]
  62. ]);
  63. } else {
  64. Yii::$app->set('user', [
  65. 'class' => 'yii\web\User',
  66. 'identityClass' => 'common\modules\user\models\User',
  67. 'loginUrl' => ['/user/security/login'],
  68. 'enableAutoLogin' => true,
  69. 'on afterLogin' => function($event) {
  70. $event->identity->touch('login_at');
  71. },
  72. 'as userBehavior' => [
  73. 'class' => UserBehaviorBehavior::className(),
  74. 'eventName' => User::EVENT_AFTER_LOGIN,
  75. 'name' => 'login',
  76. 'rule' => [
  77. 'cycle' => 24,
  78. 'max' => 1,
  79. 'counter' => 10,
  80. ],
  81. 'content' => '{user.username}在{extra.time}登录了系统',
  82. 'data' => [
  83. 'extra' => [
  84. 'time' => date('Y-m-d H:i:s')
  85. ]
  86. ]
  87. ]
  88. ]);
  89. $config = $this->params;
  90. $params = [
  91. 'qq' => [
  92. 'class' => QqAuth::className(),
  93. 'clientId' => $config['qq_client_id'],
  94. 'clientSecret' => $config['qq_client_secret']
  95. ],
  96. 'github' => [
  97. 'class' => GitHub::className(),
  98. 'clientId' => $config['github_client_id'],
  99. 'clientSecret' => $config['github_client_secret']
  100. ],
  101. 'weibo' => [
  102. 'class' => WeiboAuth::className(),
  103. 'clientId' => $config['weibo_client_id'],
  104. 'clientSecret' => $config['weibo_client_secret']
  105. ],
  106. 'weixin' => [
  107. 'class' => WeixinAuth::className(),
  108. 'clientId' => $config['weixin_client_id'],
  109. 'clientSecret' => $config['weixin_client_secret']
  110. ],
  111. ];
  112. $openClients = $config['open_client'];
  113. $openParams = [];
  114. if (!empty($openClients)) {
  115. foreach ($openClients as $client) {
  116. $client = strtolower($client);
  117. $openParams[$client] = $params[$client];
  118. }
  119. }
  120. $app->set('authClientCollection', [
  121. 'class' => 'yii\authclient\Collection',
  122. 'clients' => $openParams
  123. ]);
  124. $this->urlRules = [
  125. '<id:\d+>' => 'default/index',
  126. '<action:(login|logout)>' => 'security/<action>',
  127. '<action:(signup)>' => 'registration/<action>',
  128. '<action:(up|article-list|create-article|update-article|notice|favourite)>' => 'default/<action>',
  129. ];
  130. }
  131. $configUrlRule = [
  132. 'prefix' => $this->urlPrefix,
  133. 'rules' => $this->urlRules,
  134. ];
  135. if ($this->urlPrefix != 'user') {
  136. $configUrlRule['routePrefix'] = 'user';
  137. }
  138. $configUrlRule['class'] = 'yii\web\GroupUrlRule';
  139. $rule = Yii::createObject($configUrlRule);
  140. $app->urlManager->addRules([$rule], false);
  141. }
  142. }