AdminUserController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-03-31 15:01
  7. */
  8. namespace backend\controllers;
  9. use Yii;
  10. use backend\actions\CreateAction;
  11. use backend\actions\UpdateAction;
  12. use common\services\RBACServiceInterface;
  13. use common\models\AdminUser;
  14. use common\services\AdminUserServiceInterface;
  15. use backend\models\form\PasswordResetRequestForm;
  16. use backend\actions\IndexAction;
  17. use backend\actions\DeleteAction;
  18. use backend\actions\SortAction;
  19. use yii\base\InvalidParamException;
  20. use yii\web\BadRequestHttpException;
  21. use backend\actions\ViewAction;
  22. /**
  23. * AdminUser management
  24. * - data:
  25. * table admin_user
  26. *
  27. * Class AdminUserController
  28. * @package backend\controllers
  29. */
  30. class AdminUserController extends \yii\web\Controller
  31. {
  32. /**
  33. * @auth
  34. * - item group=权限 category=管理员 description-get=列表 sort=520 method=get
  35. * - item group=权限 category=管理员 description-get=查看 sort=521 method=get


  36. * - item group=权限 category=管理员 description-post=删除 sort=522 method=post


  37. * - item group=权限 category=管理员 description-post=排序 sort=523 method=post

  38. * - item group=权限 category=管理员 description=创建 sort-get=524 sort-post=525 method=get,post
  39. * - item group=权限 category=管理员 description=修改 sort-get=526 sort-post=527 method=get,post
  40. * - item rbac=false
  41. * - item rbac=false
  42. * - item rbac=false
  43. * 

  44. * @return array
  45. * @throws \yii\base\InvalidConfigException
  46. */
  47. public function actions()
  48. {
  49. /** @var AdminUserServiceInterface $service */
  50. $service = Yii::$app->get(AdminUserServiceInterface::ServiceName);
  51. /** @var RBACServiceInterface $rbacService */
  52. $rbacService = Yii::$app->get(RBACServiceInterface::ServiceName);
  53. return [
  54. 'index' => [
  55. 'class' => IndexAction::className(),
  56. 'data' => function($query)use($service){
  57. $result = $service->getList($query);
  58. return [
  59. 'dataProvider' => $result['dataProvider'],
  60. 'searchModel' => $result['searchModel'],
  61. ];
  62. }
  63. ],
  64. 'view-layer' => [
  65. 'class' => ViewAction::className(),
  66. 'data' => function($id)use($service){
  67. return [
  68. 'model' => $service->getDetail($id),
  69. ];
  70. },
  71. ],
  72. 'delete' => [
  73. 'class' => DeleteAction::className(),
  74. 'doDelete' => function($id) use($service){
  75. return $service->delete($id);
  76. },
  77. ],
  78. 'sort' => [
  79. 'class' => SortAction::className(),
  80. 'doSort' => function($id, $sort) use($service){
  81. return $service->sort($id, $sort);
  82. },
  83. ],
  84. 'create' => [
  85. 'class' => CreateAction::className(),
  86. 'doCreate' => function($postData) use($service, $rbacService){
  87. /** @var AdminUser $model */
  88. return $service->create($postData);
  89. },
  90. 'data' => function()use($service, $rbacService){
  91. return [
  92. 'model' => $service->newModel(['scenario' => AdminUserServiceInterface::scenarioCreate]),
  93. 'assignModel' => $rbacService->newAssignPermissionModel(),
  94. 'permissions' => $rbacService->getPermissionsGroups(),
  95. 'roles' => $rbacService->getRoles(),
  96. ];
  97. }
  98. ],
  99. 'update' => [
  100. 'class' => UpdateAction::className(),
  101. 'doUpdate' => function($id, $postData) use($service){
  102. return $service->update($id, $postData, ['scenario' => AdminUserServiceInterface::scenarioUpdate]);
  103. },
  104. 'data' => function($id, $updateResultModel)use($service, $rbacService){
  105. return [
  106. 'model' => $updateResultModel === null ? $service->getDetail($id, ['scenario' => AdminUserServiceInterface::scenarioUpdate]) : $updateResultModel,
  107. 'assignModel' => $rbacService->getAssignPermissionDetail($id),
  108. 'permissions' => $rbacService->getPermissionsGroups(),
  109. 'roles' => $rbacService->getRoles(),
  110. ];
  111. }
  112. ],
  113. 'self-update' => [
  114. 'class' => UpdateAction::className(),
  115. 'primaryKeyIdentity' => null,
  116. 'doUpdate' => function($postData) use($service){
  117. return $service->selfUpdate(Yii::$app->getUser()->getId(), $postData, ['scenario' => AdminUserServiceInterface::scenarioSelfUpdate]);
  118. },
  119. 'data' => function($updateResultModel) use($service){
  120. return [
  121. 'model' => $updateResultModel === null ? $service->getDetail(Yii::$app->getUser()->getId(), ['scenario' => AdminUserServiceInterface::scenarioSelfUpdate]) : $updateResultModel,
  122. ];
  123. },
  124. 'viewFile' => 'update',
  125. ],
  126. 'request-password-reset' => [
  127. 'class' => UpdateAction::className(),
  128. 'primaryKeyIdentity' => null,
  129. 'successTipsMessage' => Yii::t("app", "Check your email for further instructions."),
  130. 'doUpdate' => function($postData) use($service){
  131. $result = $service->sendResetPasswordLink($postData);
  132. if( $result === false ){
  133. return 'Sorry, we are unable to reset password for email provided.';
  134. }
  135. return $result;
  136. },
  137. 'data' => function($updateResultModel){
  138. return [
  139. "model" => $updateResultModel === null ? new PasswordResetRequestForm() : $updateResultModel,
  140. ];
  141. },
  142. 'viewFile' => 'requestPasswordResetToken',
  143. ],
  144. 'reset-password' => [
  145. 'class' => UpdateAction::className(),
  146. 'primaryKeyIdentity' => 'token',
  147. 'successTipsMessage' => Yii::t("app", 'New password was saved.'),
  148. 'doUpdate' => function($token, $postData) use($service) {
  149. return $service->resetPassword($token, $postData);
  150. },
  151. 'data' => function($token, $updateResultModel) use($service) {
  152. if( $updateResultModel === null ){
  153. try {
  154. $model = $service->newResetPasswordForm($token);
  155. }catch (InvalidParamException $e) {
  156. throw new BadRequestHttpException($e->getMessage());
  157. }
  158. }else{
  159. $model = $updateResultModel;
  160. }
  161. return [
  162. 'model' => $model,
  163. ];
  164. },
  165. 'successRedirect' => $this->getHomeUrl(),
  166. 'viewFile' => 'resetPassword'
  167. ]
  168. ];
  169. }
  170. private function getHomeUrl()
  171. {
  172. if( Yii::$app->getRequest()->getIsConsoleRequest() ){//when execute ./yii feehi/permission
  173. return "/";
  174. }
  175. return Yii::$app->getHomeUrl();
  176. }
  177. }