User.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\debug\models\search;
  8. use yii\base\Model;
  9. use yii\data\ActiveDataProvider;
  10. use yii\db\ActiveRecord;
  11. /**
  12. * Search model for implementation of IdentityInterface
  13. *
  14. * @author Semen Dubina <yii2debug@sam002.net>
  15. * @since 2.0.10
  16. */
  17. class User extends Model
  18. {
  19. /**
  20. * @var Model implementation of IdentityInterface
  21. */
  22. public $identityImplement = null;
  23. /**
  24. * @inheritdoc
  25. */
  26. public function init()
  27. {
  28. if (\Yii::$app->user && \Yii::$app->user->identityClass) {
  29. $identityImplementation = new \Yii::$app->user->identityClass();
  30. if ($identityImplementation instanceof Model) {
  31. $this->identityImplement = $identityImplementation;
  32. }
  33. }
  34. parent::init();
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function __get($name)
  40. {
  41. return $this->identityImplement->__get($name);
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function __set($name, $value)
  47. {
  48. return $this->identityImplement->__set($name, $value);
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function rules()
  54. {
  55. return [[array_keys($this->identityImplement->getAttributes()), 'safe']];
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. public function attributes()
  61. {
  62. return $this->identityImplement->attributes();
  63. }
  64. /**
  65. * @inheritdoc
  66. */
  67. public function search($params)
  68. {
  69. if ($this->identityImplement instanceof ActiveRecord) {
  70. return $this->serachActiveDataProvider($params);
  71. }
  72. return null;
  73. }
  74. /**
  75. * Search method for ActiveRecord
  76. * @param array $params the data array to load model.
  77. * @return ActiveDataProvider
  78. */
  79. private function serachActiveDataProvider($params)
  80. {
  81. /** @var ActiveRecord $model */
  82. $model = $this->identityImplement;
  83. $query = $model::find();
  84. $dataProvider = new ActiveDataProvider([
  85. 'query' => $query,
  86. ]);
  87. if (!($this->load($params) && $this->validate())) {
  88. return $dataProvider;
  89. }
  90. foreach ($model::getTableSchema()->columns as $attribute => $column) {
  91. if ($column->phpType === 'string') {
  92. $query->andFilterWhere(['like', $attribute, $model->getAttribute($attribute)]);
  93. } else {
  94. $query->andFilterWhere([$attribute => $model->getAttribute($attribute)]);
  95. }
  96. }
  97. return $dataProvider;
  98. }
  99. }