CUser.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecadmin\helpers;
  10. use Yii;
  11. use fecadmin\models\AdminUserRole;
  12. use fecadmin\models\AdminRole;
  13. /**
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class CUser
  18. {
  19. # 判断当前的用户是否存在某个权限组。
  20. public static function ifCurrentUserIsSpecificRole($role_name){
  21. if(!$role_name)
  22. return false;
  23. $user = Yii::$app->user->identity;
  24. $user_id = $user['id'];
  25. $roles = AdminUserRole::find()->asArray()->where([
  26. 'user_id' => $user_id
  27. ])->all();
  28. $role_ids = [];
  29. if(!empty($roles)){
  30. foreach($roles as $role){
  31. $role_ids[] = $role['role_id'];
  32. }
  33. }
  34. $user_role_names = [];
  35. $user_roles = AdminRole::find()->asArray()->where([
  36. 'in','role_id',$role_ids
  37. ])->all();
  38. if(!empty($user_roles)){
  39. foreach($user_roles as $one){
  40. $user_role_names[] = $one['role_name'];
  41. }
  42. }
  43. if(in_array($role_name,$user_role_names)){
  44. return true;
  45. }
  46. return false;
  47. }
  48. }