Assignment.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace common\modules\rbac\models;
  3. use common\modules\rbac\components\DbManager;
  4. use common\modules\rbac\validators\RbacValidator;
  5. use Yii;
  6. use yii\base\InvalidConfigException;
  7. use yii\base\Model;
  8. use yii\helpers\ArrayHelper;
  9. use yii\rbac\Item;
  10. class Assignment extends Model
  11. {
  12. /** @var array */
  13. public $items = [];
  14. /** @var integer */
  15. public $user_id;
  16. /** @var boolean */
  17. public $updated = false;
  18. /** @var DbManager */
  19. protected $manager;
  20. /**
  21. * @inheritdoc
  22. */
  23. public function init()
  24. {
  25. parent::init();
  26. $this->manager = Yii::$app->authManager;
  27. if ($this->user_id === null) {
  28. throw new InvalidConfigException('user_id must be set');
  29. }
  30. $this->items = array_keys($this->manager->getItemsByUser($this->user_id));
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public function attributeLabels()
  36. {
  37. return [
  38. 'items' => '角色'
  39. ];
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [
  48. 'user_id',
  49. 'required'
  50. ],
  51. [
  52. 'items',
  53. RbacValidator::className()
  54. ],
  55. [
  56. 'user_id',
  57. 'integer'
  58. ]
  59. ];
  60. }
  61. /**
  62. * Updates auth assignments for user.
  63. *
  64. * @return boolean
  65. */
  66. public function updateAssignments()
  67. {
  68. if (! $this->validate()) {
  69. return false;
  70. }
  71. if (! is_array($this->items)) {
  72. $this->items = [];
  73. }
  74. $assignedItems = $this->manager->getItemsByUser($this->user_id);
  75. $assignedItemsNames = array_keys($assignedItems);
  76. foreach (array_diff($assignedItemsNames, $this->items) as $item) {
  77. $this->manager->revoke($assignedItems[$item], $this->user_id);
  78. }
  79. foreach (array_diff($this->items, $assignedItemsNames) as $item) {
  80. $this->manager->assign($this->manager->getItem($item), $this->user_id);
  81. }
  82. $this->updated = true;
  83. return true;
  84. }
  85. /**
  86. * Returns all available auth items to be attached to user.
  87. *
  88. * @return array
  89. */
  90. public function getAvailableItems($type)
  91. {
  92. if ($type == Item::TYPE_ROLE) {
  93. $items = $this->manager->getRoles();
  94. } else {
  95. $items = $this->manager->getPermissions();
  96. }
  97. return ArrayHelper::map($items, 'name', function ($item) {
  98. return empty($item->description) ? $item->name : $item->name . ' (' . $item->description . ')';
  99. });
  100. }
  101. public function getAvailableRoles()
  102. {
  103. return $this->getAvailableItems(Item::TYPE_ROLE);
  104. }
  105. }