BlameableBehavior.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\behaviors;
  8. use Yii;
  9. use yii\db\BaseActiveRecord;
  10. /**
  11. * BlameableBehavior automatically fills the specified attributes with the current user ID.
  12. *
  13. * To use BlameableBehavior, insert the following code to your ActiveRecord class:
  14. *
  15. * ```php
  16. * use yii\behaviors\BlameableBehavior;
  17. *
  18. * public function behaviors()
  19. * {
  20. * return [
  21. * BlameableBehavior::className(),
  22. * ];
  23. * }
  24. * ```
  25. *
  26. * By default, BlameableBehavior will fill the `created_by` and `updated_by` attributes with the current user ID
  27. * when the associated AR object is being inserted; it will fill the `updated_by` attribute
  28. * with the current user ID when the AR object is being updated.
  29. *
  30. * Because attribute values will be set automatically by this behavior, they are usually not user input and should therefore
  31. * not be validated, i.e. `created_by` and `updated_by` should not appear in the [[\yii\base\Model::rules()|rules()]] method of the model.
  32. *
  33. * If your attribute names are different, you may configure the [[createdByAttribute]] and [[updatedByAttribute]]
  34. * properties like the following:
  35. *
  36. * ```php
  37. * public function behaviors()
  38. * {
  39. * return [
  40. * [
  41. * 'class' => BlameableBehavior::className(),
  42. * 'createdByAttribute' => 'author_id',
  43. * 'updatedByAttribute' => 'updater_id',
  44. * ],
  45. * ];
  46. * }
  47. * ```
  48. *
  49. * @author Luciano Baraglia <luciano.baraglia@gmail.com>
  50. * @author Qiang Xue <qiang.xue@gmail.com>
  51. * @author Alexander Kochetov <creocoder@gmail.com>
  52. * @since 2.0
  53. */
  54. class BlameableBehavior extends AttributeBehavior
  55. {
  56. /**
  57. * @var string the attribute that will receive current user ID value
  58. * Set this property to false if you do not want to record the creator ID.
  59. */
  60. public $createdByAttribute = 'created_by';
  61. /**
  62. * @var string the attribute that will receive current user ID value
  63. * Set this property to false if you do not want to record the updater ID.
  64. */
  65. public $updatedByAttribute = 'updated_by';
  66. /**
  67. * {@inheritdoc}
  68. *
  69. * In case, when the property is `null`, the value of `Yii::$app->user->id` will be used as the value.
  70. */
  71. public $value;
  72. /**
  73. * @var mixed Default value for cases when the user is guest
  74. * @since 2.0.14
  75. */
  76. public $defaultValue;
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function init()
  81. {
  82. parent::init();
  83. if (empty($this->attributes)) {
  84. $this->attributes = [
  85. BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->createdByAttribute, $this->updatedByAttribute],
  86. BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedByAttribute,
  87. ];
  88. }
  89. }
  90. /**
  91. * {@inheritdoc}
  92. *
  93. * In case, when the [[value]] property is `null`, the value of [[defaultValue]] will be used as the value.
  94. */
  95. protected function getValue($event)
  96. {
  97. if ($this->value === null && Yii::$app->has('user')) {
  98. $userId = Yii::$app->get('user')->id;
  99. if ($userId === null) {
  100. return $this->getDefaultValue($event);
  101. }
  102. return $userId;
  103. } elseif ($this->value === null) {
  104. return $this->getDefaultValue($event);
  105. }
  106. return parent::getValue($event);
  107. }
  108. /**
  109. * Get default value
  110. * @param \yii\base\Event $event
  111. * @return array|mixed
  112. * @since 2.0.14
  113. */
  114. protected function getDefaultValue($event)
  115. {
  116. if ($this->defaultValue instanceof \Closure || (is_array($this->defaultValue) && is_callable($this->defaultValue))) {
  117. return call_user_func($this->defaultValue, $event);
  118. }
  119. return $this->defaultValue;
  120. }
  121. }