diff options
author | Chris Cornutt <enygma@phpdeveloper.org> | 2015-07-13 14:06:38 -0500 |
---|---|---|
committer | Chris Cornutt <enygma@phpdeveloper.org> | 2015-07-13 14:06:38 -0500 |
commit | 21b7f39e87fd6b46b97892db145aba0c5072f9b7 (patch) | |
tree | f724fc145a18ec9572cc11cb230a6671c3fadd09 | |
parent | 2f2f0c5379e02f771d1f58958d4e7c7ec0a67a24 (diff) | |
download | gatekeeper-21b7f39e87fd6b46b97892db145aba0c5072f9b7.zip gatekeeper-21b7f39e87fd6b46b97892db145aba0c5072f9b7.tar.gz gatekeeper-21b7f39e87fd6b46b97892db145aba0c5072f9b7.tar.bz2 |
adding findUsersByPermissionId to user model to get users with a given permission
-rw-r--r-- | src/Psecio/Gatekeeper/PermissionModel.php | 9 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/UserCollection.php | 22 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/Psecio/Gatekeeper/PermissionModel.php b/src/Psecio/Gatekeeper/PermissionModel.php index 581e815..eccb355 100644 --- a/src/Psecio/Gatekeeper/PermissionModel.php +++ b/src/Psecio/Gatekeeper/PermissionModel.php @@ -63,6 +63,15 @@ class PermissionModel extends \Psecio\Gatekeeper\Model\Mysql 'local' => 'id' ) ), + 'users' => array( + 'description' => 'Users that have the permission', + 'type' => 'relation', + 'relation' => array( + 'model' => '\\Psecio\\Gatekeeper\\UserCollection', + 'method' => 'findUsersByPermissionId', + 'local' => 'id' + ) + ), 'children' => array( 'description' => 'Child Permissions', 'type' => 'relation', diff --git a/src/Psecio/Gatekeeper/UserCollection.php b/src/Psecio/Gatekeeper/UserCollection.php index 805ad52..ba9f9df 100644 --- a/src/Psecio/Gatekeeper/UserCollection.php +++ b/src/Psecio/Gatekeeper/UserCollection.php @@ -24,4 +24,26 @@ class UserCollection extends \Psecio\Gatekeeper\Collection\Mysql $this->add($user); } } + + /** + * Find the users that have a permission defined by the + * given ID + * + * @param integer $permId Permission ID + */ + public function findUsersByPermissionId($permId) + { + $prefix = $this->getPrefix(); + $data = array('permId' => $permId); + $sql = 'select u.* from '.$prefix.'users u, '.$prefix.'user_permission up' + .' where up.permission_id = :permId' + .' and up.user_id = u.id'; + + $results = $this->getDb()->fetch($sql, $data); + + foreach ($results as $result) { + $user = new UserModel($this->getDb(), $result); + $this->add($user); + } + } }
\ No newline at end of file |