diff options
author | Chris Cornutt <chris.cornutt@hp.com> | 2015-01-16 16:32:15 -0600 |
---|---|---|
committer | Chris Cornutt <chris.cornutt@hp.com> | 2015-01-16 16:32:15 -0600 |
commit | c39fc2b83ab640bdd168b22c77c9dbccf3b187f2 (patch) | |
tree | 8df4fd7de885ede75399d800ce5449da331dc5d2 | |
parent | 6b92a87d63223cd25f92e3309f38952c40dca165 (diff) | |
download | gatekeeper-c39fc2b83ab640bdd168b22c77c9dbccf3b187f2.zip gatekeeper-c39fc2b83ab640bdd168b22c77c9dbccf3b187f2.tar.gz gatekeeper-c39fc2b83ab640bdd168b22c77c9dbccf3b187f2.tar.bz2 |
adding the user group collection and matching test, updating group collection test to move method
-rw-r--r-- | src/Psecio/Gatekeeper/UserGroupCollection.php | 57 | ||||
-rw-r--r-- | tests/Psecio/Gatekeeper/GroupCollectionTest.php | 18 | ||||
-rw-r--r-- | tests/Psecio/Gatekeeper/UserGroupCollectionTest.php | 24 |
3 files changed, 81 insertions, 18 deletions
diff --git a/src/Psecio/Gatekeeper/UserGroupCollection.php b/src/Psecio/Gatekeeper/UserGroupCollection.php new file mode 100644 index 0000000..036aee7 --- /dev/null +++ b/src/Psecio/Gatekeeper/UserGroupCollection.php @@ -0,0 +1,57 @@ +<?php + +namespace Psecio\Gatekeeper; + +class UserGroupCollection extends \Psecio\Gatekeeper\Collection\Mysql +{ + /** + * Find the groups that the given user belongs to + * + * @param integer $userId User ID + */ + public function findByUserId($userId) + { + $data = array('userId' => $userId); + $sql = 'select g.* from groups g, user_group ug' + .' where ug.user_id = :userId' + .' and ug.group_id = g.id'; + + $results = $this->getDb()->fetch($sql, $data); + + foreach ($results as $result) { + $group = new GroupModel($this->getDb(), $result); + $this->add($group); + } + } + + /** + * Create relational records linking the user and group + * + * @param \Gatekeeper\UserModel $model Model instance + * @param array $data Data to use in create + */ + public function create($model, array $data) + { + foreach ($data as $group) { + // Determine if it's an integer (permissionId) or name + if (is_int($group) === true) { + $where = 'id = :id'; + $dbData = array('id' => $group); + } else { + $where = 'name = :name'; + $dbData = array('name' => $group); + } + + $sql = 'select id, name from groups where '.$where; + $results = $this->getDb()->fetch($sql, $dbData); + if (!empty($results) && count($results) == 1) { + // exists, make the relation + $model = new UserGroupModel( + $this->getDb(), + array('groupId' => $results[0]['id'], 'userId' => $model->id) + ); + $this->getDb()->save($model); + } + } + } +}
\ No newline at end of file diff --git a/tests/Psecio/Gatekeeper/GroupCollectionTest.php b/tests/Psecio/Gatekeeper/GroupCollectionTest.php index 784afe6..993e3e5 100644 --- a/tests/Psecio/Gatekeeper/GroupCollectionTest.php +++ b/tests/Psecio/Gatekeeper/GroupCollectionTest.php @@ -5,24 +5,6 @@ namespace Psecio\Gatekeeper; class GroupCollectionTest extends \Psecio\Gatekeeper\Base { /** - * Test the location of groups a member is a part of - */ - public function testFindGroupsByUserId() - { - $userId = 1; - $return = array( - array('name' => 'group1', 'description' => 'Group #1'), - array('name' => 'group2', 'description' => 'Group #2') - ); - - $ds = $this->buildMock($return, 'fetch'); - $groups = new GroupCollection($ds); - - $groups->findByUserId($userId); - $this->assertCount(2, $groups); - } - - /** * Test the location and conversion of child groups into instances */ public function testFindChildrenGroups() diff --git a/tests/Psecio/Gatekeeper/UserGroupCollectionTest.php b/tests/Psecio/Gatekeeper/UserGroupCollectionTest.php new file mode 100644 index 0000000..f68c5f9 --- /dev/null +++ b/tests/Psecio/Gatekeeper/UserGroupCollectionTest.php @@ -0,0 +1,24 @@ +<?php + +namespace Psecio\Gatekeeper; + +class UserGroupCollectionTest extends \Psecio\Gatekeeper\Base +{ + /** + * Test the location of groups a member is a part of + */ + public function testFindGroupsByUserId() + { + $userId = 1; + $return = array( + array('name' => 'group1', 'description' => 'Group #1'), + array('name' => 'group2', 'description' => 'Group #2') + ); + + $ds = $this->buildMock($return, 'fetch'); + $groups = new UserGroupCollection($ds); + + $groups->findByUserId($userId); + $this->assertCount(2, $groups); + } +}
\ No newline at end of file |