Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

107 lines
3.1KB

  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. * @inheritdoc
  74. */
  75. public function init()
  76. {
  77. parent::init();
  78. if (empty($this->attributes)) {
  79. $this->attributes = [
  80. BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->createdByAttribute, $this->updatedByAttribute],
  81. BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedByAttribute,
  82. ];
  83. }
  84. }
  85. /**
  86. * @inheritdoc
  87. *
  88. * In case, when the [[value]] property is `null`, the value of `Yii::$app->user->id` will be used as the value.
  89. */
  90. protected function getValue($event)
  91. {
  92. if ($this->value === null) {
  93. $user = Yii::$app->get('user', false);
  94. return $user && !$user->isGuest ? $user->id : null;
  95. }
  96. return parent::getValue($event);
  97. }
  98. }