Commit 40eb1166 by Alexander Makarov

Splitted saveRule into insertRule and updateRule

parent f3dd74ff
...@@ -631,14 +631,20 @@ class DbManager extends Manager ...@@ -631,14 +631,20 @@ class DbManager extends Manager
* *
* @param Rule $rule the rule that has been changed. * @param Rule $rule the rule that has been changed.
*/ */
public function saveRule(Rule $rule) public function insertRule(Rule $rule)
{ {
$data = serialize($rule); $this->db->createCommand()->insert($this->ruleTable, ['name' => $rule->name, 'data' => serialize($rule)])->execute();
try {
$this->db->createCommand()->insert($this->ruleTable, ['name' => $rule->name, 'data' => $data])->execute();
} catch (\yii\db\Exception $e) {
$this->db->createCommand()->update($this->ruleTable, ['data' => $data], ['name' => $rule->name])->execute();
} }
/**
* Updates existing rule.
*
* @param string $name the name of the rule to update
* @param Rule $rule new rule
*/
public function updateRule($name, Rule $rule)
{
$this->db->createCommand()->update($this->ruleTable, ['name' => $rule->name, 'data' => serialize($rule)], ['name' => $name])->execute();
} }
/** /**
......
...@@ -323,11 +323,19 @@ abstract class Manager extends Component ...@@ -323,11 +323,19 @@ abstract class Manager extends Component
abstract public function removeRule($name); abstract public function removeRule($name);
/** /**
* Saves the changes to the rule. * Inserts new rule.
* *
* @param Rule $rule the rule that has been changed. * @param Rule $rule the rule that needs to be stored.
*/ */
abstract public function saveRule(Rule $rule); abstract public function insertRule(Rule $rule);
/**
* Updates existing rule.
*
* @param string $name the name of the rule to update
* @param Rule $rule new rule
*/
abstract public function updateRule($name, Rule $rule);
/** /**
* Returns rule given its name. * Returns rule given its name.
......
...@@ -601,12 +601,26 @@ class PhpManager extends Manager ...@@ -601,12 +601,26 @@ class PhpManager extends Manager
* *
* @param Rule $rule the rule that has been changed. * @param Rule $rule the rule that has been changed.
*/ */
public function saveRule(Rule $rule) public function insertRule(Rule $rule)
{ {
$this->_rules[$rule->name] = $rule; $this->_rules[$rule->name] = $rule;
} }
/** /**
* Updates existing rule.
*
* @param string $name the name of the rule to update
* @param Rule $rule new rule
*/
public function updateRule($name, Rule $rule)
{
if ($rule->name !== $name) {
unset($this->_rules[$name]);
}
$this->_rules[$rule->name] = $rule;
}
/**
* Returns rule given its name. * Returns rule given its name.
* *
* @param string $name name of the rule. * @param string $name name of the rule.
......
...@@ -178,29 +178,44 @@ abstract class ManagerTestCase extends TestCase ...@@ -178,29 +178,44 @@ abstract class ManagerTestCase extends TestCase
$this->assertNull($rule); $this->assertNull($rule);
} }
public function testSaveRule() public function testInsertRule()
{ {
$ruleName = 'isReallyReallyAuthor'; $ruleName = 'isReallyReallyAuthor';
$rule = new AuthorRule(['name' => $ruleName, 'reallyReally' => true]); $rule = new AuthorRule(['name' => $ruleName, 'reallyReally' => true]);
$this->auth->saveRule($rule); $this->auth->insertRule($rule);
/** @var AuthorRule $rule */ /** @var AuthorRule $rule */
$rule = $this->auth->getRule($ruleName); $rule = $this->auth->getRule($ruleName);
$this->assertEquals($ruleName, $rule->name); $this->assertEquals($ruleName, $rule->name);
$this->assertEquals(true, $rule->reallyReally); $this->assertEquals(true, $rule->reallyReally);
}
public function testUpdateRule()
{
$rule = $this->auth->getRule('isAuthor');
$rule->name = "newName";
$rule->reallyReally = false; $rule->reallyReally = false;
$this->auth->saveRule($rule); $this->auth->updateRule('isAuthor', $rule);
/** @var AuthorRule $rule */ /** @var AuthorRule $rule */
$rule = $this->auth->getRule($ruleName); $rule = $this->auth->getRule('isAuthor');
$this->assertEquals(null, $rule);
$rule = $this->auth->getRule('newName');
$this->assertEquals("newName", $rule->name);
$this->assertEquals(false, $rule->reallyReally); $this->assertEquals(false, $rule->reallyReally);
$rule->reallyReally = true;
$this->auth->updateRule('newName', $rule);
$rule = $this->auth->getRule('newName');
$this->assertEquals(true, $rule->reallyReally);
} }
public function testGetRules() public function testGetRules()
{ {
$rule = new AuthorRule(['name' => 'isReallyReallyAuthor', 'reallyReally' => true]); $rule = new AuthorRule(['name' => 'isReallyReallyAuthor', 'reallyReally' => true]);
$this->auth->saveRule($rule); $this->auth->insertRule($rule);
$rules = $this->auth->getRules(); $rules = $this->auth->getRules();
...@@ -281,7 +296,7 @@ abstract class ManagerTestCase extends TestCase ...@@ -281,7 +296,7 @@ abstract class ManagerTestCase extends TestCase
protected function prepareData() protected function prepareData()
{ {
$this->auth->saveRule(new AuthorRule()); $this->auth->insertRule(new AuthorRule());
$this->auth->createOperation('createPost', 'create a post'); $this->auth->createOperation('createPost', 'create a post');
$this->auth->createOperation('readPost', 'read a post'); $this->auth->createOperation('readPost', 'read a post');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment