Commit c4e9e003 by Qiang Xue

Merge pull request #230 from creocoder/rbac-db-manager-code-style

RBAC: DbManager code style
parents 71a9efdd a24e4d30
...@@ -146,10 +146,7 @@ class DbManager extends Manager ...@@ -146,10 +146,7 @@ class DbManager extends Manager
} }
$query = new Query; $query = new Query;
$rows = $query->from($this->itemTable) $rows = $query->from($this->itemTable)
->where(array('or', 'name=:name1', 'name=:name2'), array( ->where(array('or', 'name=:name1', 'name=:name2'), array(':name1' => $itemName, ':name2' => $childName))
':name1' => $itemName,
':name2' => $childName
))
->createCommand($this->db) ->createCommand($this->db)
->queryAll(); ->queryAll();
if (count($rows) == 2) { if (count($rows) == 2) {
...@@ -165,10 +162,7 @@ class DbManager extends Manager ...@@ -165,10 +162,7 @@ class DbManager extends Manager
throw new InvalidCallException("Cannot add '$childName' as a child of '$itemName'. A loop has been detected."); throw new InvalidCallException("Cannot add '$childName' as a child of '$itemName'. A loop has been detected.");
} }
$this->db->createCommand() $this->db->createCommand()
->insert($this->itemChildTable, array( ->insert($this->itemChildTable, array('parent' => $itemName, 'child' => $childName));
'parent' => $itemName,
'child' => $childName,
));
return true; return true;
} else { } else {
throw new Exception("Either '$itemName' or '$childName' does not exist."); throw new Exception("Either '$itemName' or '$childName' does not exist.");
...@@ -185,10 +179,7 @@ class DbManager extends Manager ...@@ -185,10 +179,7 @@ class DbManager extends Manager
public function removeItemChild($itemName, $childName) public function removeItemChild($itemName, $childName)
{ {
return $this->db->createCommand() return $this->db->createCommand()
->delete($this->itemChildTable, array( ->delete($this->itemChildTable, array('parent' => $itemName, 'child' => $childName)) > 0;
'parent' => $itemName,
'child' => $childName
)) > 0;
} }
/** /**
...@@ -202,10 +193,7 @@ class DbManager extends Manager ...@@ -202,10 +193,7 @@ class DbManager extends Manager
$query = new Query; $query = new Query;
return $query->select(array('parent')) return $query->select(array('parent'))
->from($this->itemChildTable) ->from($this->itemChildTable)
->where(array( ->where(array('parent' => $itemName, 'child' => $childName))
'parent' => $itemName,
'child' => $childName
))
->createCommand($this->db) ->createCommand($this->db)
->queryScalar() !== false; ->queryScalar() !== false;
} }
...@@ -220,10 +208,7 @@ class DbManager extends Manager ...@@ -220,10 +208,7 @@ class DbManager extends Manager
{ {
$query = new Query; $query = new Query;
$rows = $query->select(array('name', 'type', 'description', 'bizrule', 'data')) $rows = $query->select(array('name', 'type', 'description', 'bizrule', 'data'))
->from(array( ->from(array($this->itemTable, $this->itemChildTable))
$this->itemTable,
$this->itemChildTable
))
->where(array('parent' => $names, 'name' => new Expression('child'))) ->where(array('parent' => $names, 'name' => new Expression('child')))
->createCommand($this->db) ->createCommand($this->db)
->queryAll(); ->queryAll();
...@@ -257,7 +242,7 @@ class DbManager extends Manager ...@@ -257,7 +242,7 @@ class DbManager extends Manager
'user_id' => $userId, 'user_id' => $userId,
'item_name' => $itemName, 'item_name' => $itemName,
'bizrule' => $bizRule, 'bizrule' => $bizRule,
'data' => serialize($data) 'data' => serialize($data),
)); ));
return new Assignment($this, $userId, $itemName, $bizRule, $data); return new Assignment($this, $userId, $itemName, $bizRule, $data);
} }
...@@ -271,10 +256,7 @@ class DbManager extends Manager ...@@ -271,10 +256,7 @@ class DbManager extends Manager
public function revoke($userId, $itemName) public function revoke($userId, $itemName)
{ {
return $this->db->createCommand() return $this->db->createCommand()
->delete($this->assignmentTable, array( ->delete($this->assignmentTable, array('user_id' => $userId, 'item_name' => $itemName)) > 0;
'user_id' => $userId,
'item_name' => $itemName
)) > 0;
} }
/** /**
...@@ -288,10 +270,7 @@ class DbManager extends Manager ...@@ -288,10 +270,7 @@ class DbManager extends Manager
$query = new Query; $query = new Query;
return $query->select(array('item_name')) return $query->select(array('item_name'))
->from($this->assignmentTable) ->from($this->assignmentTable)
->where(array( ->where(array('user_id' => $userId, 'item_name' => $itemName))
'user_id' => $userId,
'item_name' => $itemName
))
->createCommand($this->db) ->createCommand($this->db)
->queryScalar() !== false; ->queryScalar() !== false;
} }
...@@ -307,10 +286,7 @@ class DbManager extends Manager ...@@ -307,10 +286,7 @@ class DbManager extends Manager
{ {
$query = new Query; $query = new Query;
$row = $query->from($this->assignmentTable) $row = $query->from($this->assignmentTable)
->where(array( ->where(array('user_id' => $userId, 'item_name' => $itemName))
'user_id' => $userId,
'item_name' => $itemName
))
->createCommand($this->db) ->createCommand($this->db)
->queryRow(); ->queryRow();
if ($row !== false) { if ($row !== false) {
...@@ -358,7 +334,7 @@ class DbManager extends Manager ...@@ -358,7 +334,7 @@ class DbManager extends Manager
'data' => serialize($assignment->getData()), 'data' => serialize($assignment->getData()),
), array( ), array(
'user_id' => $assignment->getUserId(), 'user_id' => $assignment->getUserId(),
'item_name' => $assignment->getItemName() 'item_name' => $assignment->getItemName(),
)); ));
} }
...@@ -382,23 +358,13 @@ class DbManager extends Manager ...@@ -382,23 +358,13 @@ class DbManager extends Manager
->createCommand($this->db); ->createCommand($this->db);
} elseif ($type === null) { } elseif ($type === null) {
$command = $query->select(array('name', 'type', 'description', 't1.bizrule', 't1.data')) $command = $query->select(array('name', 'type', 'description', 't1.bizrule', 't1.data'))
->from(array( ->from(array($this->itemTable . ' t1', $this->assignmentTable . ' t2'))
$this->itemTable . ' t1',
$this->assignmentTable . ' t2'
))
->where(array('user_id' => $userId, 'name' => new Expression('item_name'))) ->where(array('user_id' => $userId, 'name' => new Expression('item_name')))
->createCommand($this->db); ->createCommand($this->db);
} else { } else {
$command = $query->select('name', 'type', 'description', 't1.bizrule', 't1.data') $command = $query->select('name', 'type', 'description', 't1.bizrule', 't1.data')
->from(array( ->from(array($this->itemTable . ' t1', $this->assignmentTable . ' t2'))
$this->itemTable . ' t1', ->where(array('user_id' => $userId, 'type' => $type, 'name' => new Expression('item_name')))
$this->assignmentTable . ' t2'
))
->where(array(
'user_id' => $userId,
'type' => $type,
'name' => new Expression('item_name'),
))
->createCommand($this->db); ->createCommand($this->db);
} }
$items = array(); $items = array();
...@@ -434,7 +400,7 @@ class DbManager extends Manager ...@@ -434,7 +400,7 @@ class DbManager extends Manager
'type' => $type, 'type' => $type,
'description' => $description, 'description' => $description,
'bizrule' => $bizRule, 'bizrule' => $bizRule,
'data' => serialize($data) 'data' => serialize($data),
)); ));
return new Item($this, $name, $type, $description, $bizRule, $data); return new Item($this, $name, $type, $description, $bizRule, $data);
} }
...@@ -448,13 +414,12 @@ class DbManager extends Manager ...@@ -448,13 +414,12 @@ class DbManager extends Manager
{ {
if ($this->usingSqlite()) { if ($this->usingSqlite()) {
$this->db->createCommand() $this->db->createCommand()
->delete($this->itemChildTable, array('or', 'parent=:name1', 'child=:name2'), array( ->delete($this->itemChildTable, array('or', 'parent=:name', 'child=:name'), array(':name' => $name));
':name1' => $name, $this->db->createCommand()
':name2' => $name ->delete($this->assignmentTable, array('item_name' => $name));
));
$this->db->createCommand()->delete($this->assignmentTable, array('item_name' => $name));
} }
return $this->db->createCommand()->delete($this->itemTable, array('name' => $name)) > 0; return $this->db->createCommand()
->delete($this->itemTable, array('name' => $name)) > 0;
} }
/** /**
...@@ -488,23 +453,11 @@ class DbManager extends Manager ...@@ -488,23 +453,11 @@ class DbManager extends Manager
{ {
if ($this->usingSqlite() && $oldName !== null && $item->getName() !== $oldName) { if ($this->usingSqlite() && $oldName !== null && $item->getName() !== $oldName) {
$this->db->createCommand() $this->db->createCommand()
->update($this->itemChildTable, array( ->update($this->itemChildTable, array('parent' => $item->getName()), array('parent' => $oldName));
'parent' => $item->getName(),
), array(
'parent' => $oldName,
));
$this->db->createCommand() $this->db->createCommand()
->update($this->itemChildTable, array( ->update($this->itemChildTable, array('child' => $item->getName()), array('child' => $oldName));
'child' => $item->getName(),
), array(
'child' => $oldName,
));
$this->db->createCommand() $this->db->createCommand()
->update($this->assignmentTable, array( ->update($this->assignmentTable, array('item_name' => $item->getName()), array('item_name' => $oldName));
'item_name' => $item->getName(),
), array(
'item_name' => $oldName,
));
} }
$this->db->createCommand() $this->db->createCommand()
......
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