AuthItem.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * AuthItem class file.
  4. *
  5. * @author Spyros Soldatos <spyros@valor.gr>
  6. * @link http://code.google.com/p/srbac/
  7. */
  8. /**
  9. * AuthItem is the models for authManager items (operations, tasks and roles)
  10. *
  11. * @author Spyros Soldatos <spyros@valor.gr>
  12. * @package srbac.models
  13. * @since 1.0.0
  14. */
  15. class AuthItem extends CActiveRecord {
  16. /**
  17. * The followings are the available columns in table 'authitem':
  18. * @var string $name
  19. * @var integer $type
  20. * @var string $description
  21. * @var string $bizrule
  22. * @var string $data
  23. *
  24. */
  25. public static $TYPES = array('Operation','Task','Role');
  26. public $oldName;
  27. public function getDbConnection() {
  28. return Yii::app()->authManager->db;
  29. }
  30. /**
  31. * Returns the static model of the specified AR class.
  32. * @return CActiveRecord the static model class
  33. */
  34. public static function model($className=__CLASS__) {
  35. return parent::model($className);
  36. }
  37. /**
  38. * @return string the associated database table name
  39. */
  40. public function tableName() {
  41. return Yii::app()->authManager->itemTable;
  42. }
  43. // public function safeAttributes() {
  44. // parent::safeAttributes();
  45. // return array('name','type','description','bizrule','data');
  46. // }
  47. /**
  48. * @return array validation rules for model attributes.
  49. */
  50. public function rules() {
  51. return array(
  52. array('name','length','max'=>64),
  53. array('name, type', 'required'),
  54. array('type', 'numerical', 'integerOnly'=>true),
  55. array('name,type,description,bizrule,data','safe'),
  56. );
  57. }
  58. /**
  59. * @return array relational rules.
  60. */
  61. public function relations() {
  62. // NOTE: you may need to adjust the relation name and the related
  63. // class name for the relations automatically generated below.
  64. return array(
  65. );
  66. }
  67. /**
  68. * @return array customized attribute labels (name=>label)
  69. */
  70. public function attributeLabels() {
  71. return array(
  72. 'name'=>Helper::translate('srbac','Name'),
  73. 'type'=>Helper::translate('srbac','Type'),
  74. 'description'=>Helper::translate('srbac','Description'),
  75. 'bizrule'=>Helper::translate('srbac','Bizrule'),
  76. 'data'=>Helper::translate('srbac','Data'),
  77. );
  78. }
  79. // protected function beforeSave() {
  80. // if($this->getIsNewRecord()) {
  81. // $authItem = AuthItem::model()->findByPk($this->name);
  82. // if($authItem !== null) {
  83. // return false;
  84. // }
  85. // }
  86. // parent::beforeSave();
  87. // }
  88. protected function beforeSave() {
  89. $this->data = serialize($this->data);
  90. return parent::beforeSave();
  91. }
  92. protected function afterFind() {
  93. parent::afterFind();
  94. $this->data = unserialize($this->data);
  95. }
  96. protected function afterSave() {
  97. parent::afterSave();
  98. $this->data = unserialize($this->data);
  99. if($this->oldName != $this->name) {
  100. $this->model()->updateByPk($this->oldName, array("name"=>$this->name));
  101. $criteria = new CDbCriteria();
  102. $criteria->condition = "itemname='".$this->oldName."'";
  103. Assignments::model()->updateAll(array('itemname'=>$this->name),$criteria);
  104. $criteria->condition = "parent='".$this->oldName."'";
  105. ItemChildren::model()->updateAll(array('parent'=>$this->name), $criteria);
  106. $criteria->condition = "child='".$this->oldName."'";
  107. ItemChildren::model()->updateAll(array('child'=>$this->name),$criteria);
  108. Yii::app()->user->setFlash('updateName',
  109. Helper::translate('srbac','Updating list'));
  110. }
  111. }
  112. protected function afterDelete() {
  113. parent::afterDelete();
  114. Assignments::model()->deleteAll("itemname='".$this->name."'");
  115. ItemChildren::model()->deleteAll( "parent='".$this->name."'");
  116. ItemChildren::model()->deleteAll("child='".$this->name."'");
  117. }
  118. }