From af3f5af3be9ee8afd4ccab4ddaefba57f6bc1ca7 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sat, 11 May 2013 09:24:02 +0400 Subject: [PATCH] RBAC: Item and Assignment optimized save approach --- tests/unit/framework/rbac/ManagerTestBase.php | 1 + yii/rbac/Assignment.php | 10 ++++++++-- yii/rbac/Item.php | 16 +++++++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/tests/unit/framework/rbac/ManagerTestBase.php b/tests/unit/framework/rbac/ManagerTestBase.php index 03d5354..542c1bb 100644 --- a/tests/unit/framework/rbac/ManagerTestBase.php +++ b/tests/unit/framework/rbac/ManagerTestBase.php @@ -57,6 +57,7 @@ abstract class ManagerTestBase extends TestCase $this->assertTrue($item instanceof Item); $this->assertTrue($this->auth->hasItemChild('reader', 'readPost')); $item->name = 'readPost2'; + $item->save(); $this->assertNull($this->auth->getItem('readPost')); $this->assertEquals($this->auth->getItem('readPost2'), $item); $this->assertFalse($this->auth->hasItemChild('reader', 'readPost')); diff --git a/yii/rbac/Assignment.php b/yii/rbac/Assignment.php index 5b6a607..b3aa74f 100644 --- a/yii/rbac/Assignment.php +++ b/yii/rbac/Assignment.php @@ -81,7 +81,6 @@ class Assignment extends Object { if ($this->_bizRule !== $value) { $this->_bizRule = $value; - $this->_auth->saveAssignment($this); } } @@ -100,7 +99,14 @@ class Assignment extends Object { if ($this->_data !== $value) { $this->_data = $value; - $this->_auth->saveAssignment($this); } } + + /** + * Saves the changes to an authorization assignment. + */ + public function save() + { + $this->_auth->saveAssignment($this); + } } diff --git a/yii/rbac/Item.php b/yii/rbac/Item.php index ef56a53..57a7f7e 100644 --- a/yii/rbac/Item.php +++ b/yii/rbac/Item.php @@ -39,6 +39,7 @@ class Item extends Object private $_auth; private $_type; private $_name; + private $_oldName; private $_description; private $_bizRule; private $_data; @@ -116,9 +117,8 @@ class Item extends Object public function setName($value) { if ($this->_name !== $value) { - $oldName = $this->_name; + $this->_oldName = $this->_name; $this->_name = $value; - $this->_auth->saveItem($this, $oldName); } } @@ -137,7 +137,6 @@ class Item extends Object { if ($this->_description !== $value) { $this->_description = $value; - $this->_auth->saveItem($this); } } @@ -156,7 +155,6 @@ class Item extends Object { if ($this->_bizRule !== $value) { $this->_bizRule = $value; - $this->_auth->saveItem($this); } } @@ -175,7 +173,6 @@ class Item extends Object { if ($this->_data !== $value) { $this->_data = $value; - $this->_auth->saveItem($this); } } @@ -272,4 +269,13 @@ class Item extends Object { return $this->_auth->getAssignment($userId, $this->_name); } + + /** + * Saves an authorization item to persistent storage. + */ + public function save() + { + $this->_auth->saveItem($this, $this->_oldName); + unset($this->_oldName); + } }