Skip to content

Commit

Permalink
Fixes yiisoft#11702: Added yii\rbac\DbManager::$assignmentTablePk t…
Browse files Browse the repository at this point in the history
…o be able to customize RBAC DB schema more
  • Loading branch information
Jorge Robles authored and samdark committed Jun 6, 2016
1 parent b9bff5f commit 5322420
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Yii Framework 2 Change Log
- Bug #11571: Fixed `yii\db\ColumnSchemaBuilder` to work with custom column types (andrey-mokhov, silverfire)
- Bug #11662: Fixed `schema-oci.sql` for RBAC (jonny7)
- Bug #11527: Fixed `bigPrimaryKey()` for SQLite (dynasource)
- Enh #11702: Added `yii\rbac\DbManager::$assignmentTablePk` to be able to customize RBAC DB schema more (jrobles-boom)


2.0.8 April 28, 2016
Expand Down
27 changes: 16 additions & 11 deletions framework/rbac/DbManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class DbManager extends BaseManager
* @var string the name of the table storing rules. Defaults to "auth_rule".
*/
public $ruleTable = '{{%auth_rule}}';
/**
* @var string the name of the primary key for assignment table. Defaults to "user_id".
* @since 2.0.9
*/
public $assignmentTablePk = 'user_id';
/**
* @var Cache|array|string the cache used to improve RBAC performance. This can be one of the following:
*
Expand Down Expand Up @@ -460,7 +465,7 @@ public function getRolesByUser($userId)
$query = (new Query)->select('b.*')
->from(['a' => $this->assignmentTable, 'b' => $this->itemTable])
->where('{{a}}.[[item_name]]={{b}}.[[name]]')
->andWhere(['a.user_id' => (string) $userId])
->andWhere(['a.'.$this->assignmentTablePk => (string) $userId])
->andWhere(['b.type' => Item::TYPE_ROLE]);

$roles = [];
Expand Down Expand Up @@ -518,7 +523,7 @@ protected function getDirectPermissionsByUser($userId)
$query = (new Query)->select('b.*')
->from(['a' => $this->assignmentTable, 'b' => $this->itemTable])
->where('{{a}}.[[item_name]]={{b}}.[[name]]')
->andWhere(['a.user_id' => (string) $userId])
->andWhere(['a.'.$this->assignmentTablePk => (string) $userId])
->andWhere(['b.type' => Item::TYPE_PERMISSION]);

$permissions = [];
Expand All @@ -538,7 +543,7 @@ protected function getInheritedPermissionsByUser($userId)
{
$query = (new Query)->select('item_name')
->from($this->assignmentTable)
->where(['user_id' => (string) $userId]);
->where([$this->assignmentTablePk => (string) $userId]);

$childrenList = $this->getChildrenList();
$result = [];
Expand Down Expand Up @@ -637,15 +642,15 @@ public function getAssignment($roleName, $userId)
}

$row = (new Query)->from($this->assignmentTable)
->where(['user_id' => (string) $userId, 'item_name' => $roleName])
->where([$this->assignmentTablePk => (string) $userId, 'item_name' => $roleName])
->one($this->db);

if ($row === false) {
return null;
}

return new Assignment([
'userId' => $row['user_id'],
'userId' => $row[$this->assignmentTablePk],
'roleName' => $row['item_name'],
'createdAt' => $row['created_at'],
]);
Expand All @@ -662,12 +667,12 @@ public function getAssignments($userId)

$query = (new Query)
->from($this->assignmentTable)
->where(['user_id' => (string) $userId]);
->where([$this->assignmentTablePk => (string) $userId]);

$assignments = [];
foreach ($query->all($this->db) as $row) {
$assignments[$row['item_name']] = new Assignment([
'userId' => $row['user_id'],
'userId' => $row[$this->assignmentTablePk],
'roleName' => $row['item_name'],
'createdAt' => $row['created_at'],
]);
Expand Down Expand Up @@ -800,7 +805,7 @@ public function assign($role, $userId)

$this->db->createCommand()
->insert($this->assignmentTable, [
'user_id' => $assignment->userId,
$this->assignmentTablePk => $assignment->userId,
'item_name' => $assignment->roleName,
'created_at' => $assignment->createdAt,
])->execute();
Expand All @@ -818,7 +823,7 @@ public function revoke($role, $userId)
}

return $this->db->createCommand()
->delete($this->assignmentTable, ['user_id' => (string) $userId, 'item_name' => $role->name])
->delete($this->assignmentTable, [$this->assignmentTablePk => (string) $userId, 'item_name' => $role->name])
->execute() > 0;
}

Expand All @@ -832,7 +837,7 @@ public function revokeAll($userId)
}

return $this->db->createCommand()
->delete($this->assignmentTable, ['user_id' => (string) $userId])
->delete($this->assignmentTable, [$this->assignmentTablePk => (string) $userId])
->execute() > 0;
}

Expand Down Expand Up @@ -976,7 +981,7 @@ public function getUserIdsByRole($roleName)
return [];
}

return (new Query)->select('[[user_id]]')
return (new Query)->select('[['.$this->assignmentTablePk.']]')
->from($this->assignmentTable)
->where(['item_name' => $roleName])->column($this->db);
}
Expand Down

0 comments on commit 5322420

Please sign in to comment.