You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 line
3.3KB

  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\base\Event;
  10. use yii\db\BaseActiveRecord;
  11. /**
  12. * BlameableBehavior automatically fills the specified attributes with the current user ID.
  13. *
  14. * To use BlameableBehavior, insert the following code to your ActiveRecord class:
  15. *
  16. * ```php
  17. * use yii\behaviors\BlameableBehavior;
  18. *
  19. * public function behaviors()
  20. * {
  21. * return [
  22. * BlameableBehavior::className(),
  23. * ];
  24. * }
  25. * ```
  26. *
  27. * By default, BlameableBehavior will fill the `created_by` and `updated_by` attributes with the current user ID
  28. * when the associated AR object is being inserted; it will fill the `updated_by` attribute
  29. * with the current user ID when the AR object is being updated. If your attribute names are different, you may configure
  30. * the [[createdByAttribute]] and [[updatedByAttribute]] properties like the following:
  31. *
  32. * ```php
  33. * public function behaviors()
  34. * {
  35. * return [
  36. * [
  37. * 'class' => BlameableBehavior::className(),
  38. * 'createdByAttribute' => 'author_id',
  39. * 'updatedByAttribute' => 'updater_id',
  40. * ],
  41. * ];
  42. * }
  43. * ```
  44. *
  45. * @author Luciano Baraglia <luciano.baraglia@gmail.com>
  46. * @author Qiang Xue <qiang.xue@gmail.com>
  47. * @author Alexander Kochetov <creocoder@gmail.com>
  48. * @since 2.0
  49. */
  50. class BlameableBehavior extends AttributeBehavior
  51. {
  52. /**
  53. * @var string the attribute that will receive current user ID value
  54. * Set this property to false if you do not want to record the creator ID.
  55. */
  56. public $createdByAttribute = 'created_by';
  57. /**
  58. * @var string the attribute that will receive current user ID value
  59. * Set this property to false if you do not want to record the updater ID.
  60. */
  61. public $updatedByAttribute = 'updated_by';
  62. /**
  63. * @var callable the value that will be assigned to the attributes. This should be a valid
  64. * PHP callable whose return value will be assigned to the current attribute(s).
  65. * The signature of the callable should be:
  66. *
  67. * ```php
  68. * function ($event) {
  69. * // return value will be assigned to the attribute(s)
  70. * }
  71. * ```
  72. *
  73. * If this property is not set, the value of `Yii::$app->user->id` will be assigned to the attribute(s).
  74. */
  75. public $value;
  76. /**
  77. * @inheritdoc
  78. */
  79. public function init()
  80. {
  81. parent::init();
  82. if (empty($this->attributes)) {
  83. $this->attributes = [
  84. BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->createdByAttribute, $this->updatedByAttribute],
  85. BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedByAttribute,
  86. ];
  87. }
  88. }
  89. /**
  90. * Evaluates the value of the user.
  91. * The return result of this method will be assigned to the current attribute(s).
  92. * @param Event $event
  93. * @return mixed the value of the user.
  94. */
  95. protected function getValue($event)
  96. {
  97. if ($this->value === null) {
  98. $user = Yii::$app->get('user', false);
  99. return $user && !$user->isGuest ? $user->id : null;
  100. } else {
  101. return call_user_func($this->value, $event);
  102. }
  103. }
  104. }