summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Cornutt <chris.cornutt@hp.com>2015-01-16 15:41:39 -0600
committerChris Cornutt <chris.cornutt@hp.com>2015-01-16 15:41:39 -0600
commit23305b2c6eb1cb04dead2c9b7ad8a6ecdf5864da (patch)
tree1e759f90fe69386da6e5a283b54c8cd009c26126
parent2b8cbe7061be10e8e4fb33bceea056d9dc175455 (diff)
downloadgatekeeper-23305b2c6eb1cb04dead2c9b7ad8a6ecdf5864da.zip
gatekeeper-23305b2c6eb1cb04dead2c9b7ad8a6ecdf5864da.tar.gz
gatekeeper-23305b2c6eb1cb04dead2c9b7ad8a6ecdf5864da.tar.bz2
adding UserPermissionCollection
-rw-r--r--src/Psecio/Gatekeeper/UserPermissionCollection.php57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/Psecio/Gatekeeper/UserPermissionCollection.php b/src/Psecio/Gatekeeper/UserPermissionCollection.php
new file mode 100644
index 0000000..42b22f5
--- /dev/null
+++ b/src/Psecio/Gatekeeper/UserPermissionCollection.php
@@ -0,0 +1,57 @@
+<?php
+
+namespace Psecio\Gatekeeper;
+
+class UserPermissionCollection extends \Psecio\Gatekeeper\Collection\Mysql
+{
+ /**
+ * Find the permissions for a given user ID
+ *
+ * @param integer $userId User ID
+ */
+ public function findByUserId($userId)
+ {
+ $data = array('userId' => $userId);
+ $sql = 'select p.* from permissions p, user_permission up'
+ .' where p.id = up.permission_id'
+ .' and up.user_id = :userId';
+
+ $results = $this->getDb()->fetch($sql, $data);
+
+ foreach ($results as $result) {
+ $perm = new PermissionModel($this->getDb(), $result);
+ $this->add($perm);
+ }
+ }
+
+ /**
+ * Create relational records linking the user and permission
+ *
+ * @param \Gatekeeper\UserModel $model Model instance
+ * @param array $data Data to use in create
+ */
+ public function create($model, array $data)
+ {
+ foreach ($data as $permission) {
+ // Determine if it's an integer (permissionId) or name
+ if (is_int($permission) === true) {
+ $where = 'id = :id';
+ $dbData = array('id' => $permission);
+ } else {
+ $where = 'name = :name';
+ $dbData = array('name' => $permission);
+ }
+
+ $sql = 'select id, name from permissions where '.$where;
+ $results = $this->getDb()->fetch($sql, array('name' => $permission));
+ if (!empty($results) && count($results) == 1) {
+ // exists, make the relation
+ $model = new UserPermissionModel(
+ $this->getDb(),
+ array('permissionId' => $results[0]['id'], 'userId' => $model->id)
+ );
+ $this->getDb()->save($model);
+ }
+ }
+ }
+} \ No newline at end of file