diff options
-rw-r--r-- | docs/groups.md | 24 | ||||
-rw-r--r-- | docs/permissions.md | 35 | ||||
-rw-r--r-- | docs/users.md | 27 | ||||
-rw-r--r-- | examples/README.md | 9 | ||||
-rw-r--r-- | examples/login-form.html | 36 | ||||
-rw-r--r-- | migrations/20150702224804_add_permission_group_expire.php | 24 | ||||
-rw-r--r-- | migrations/20150703015048_add_user_permission_group_expire.php | 24 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/GroupModel.php | 15 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/PermissionModel.php | 15 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/PolicyCollection.php | 2 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/UserGroupModel.php | 5 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/UserModel.php | 21 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/UserPermissionCollection.php | 3 | ||||
-rw-r--r-- | src/Psecio/Gatekeeper/UserPermissionModel.php | 5 | ||||
-rw-r--r-- | tests/Psecio/Gatekeeper/GroupModelTest.php | 28 | ||||
-rw-r--r-- | tests/Psecio/Gatekeeper/PermissionModelTest.php | 28 | ||||
-rw-r--r-- | tests/Psecio/Gatekeeper/PolicyCollectionTest.php | 45 | ||||
-rw-r--r-- | tests/Psecio/Gatekeeper/SecurityQuestionCollectionTest.php | 24 | ||||
-rw-r--r-- | tests/Psecio/Gatekeeper/UserModelTest.php | 106 |
19 files changed, 397 insertions, 79 deletions
diff --git a/docs/groups.md b/docs/groups.md index dc26d94..5c81733 100644 --- a/docs/groups.md +++ b/docs/groups.md @@ -36,6 +36,30 @@ Gatekeeper::createGroup($attrs); ?> ``` +You can also create a group with an expiration timeout, allowing users in that group only a certain timeframe for their access. You use the `expires` value on the creation to set this with a Unix timestamp: + +```php +<?php +$attrs = array( + 'name' => 'group1', + 'description' => 'Group #1', + 'expires' => strtotime('+1 day') +); +Gatekeeper::createGroup($attrs); +?> +``` + +You can then check to see if a group has expired with the `isExpired` method: + +```php +<?php +if (Gatekeeper::findGroupById(1)-isExpired() === true) { + echo 'Group expired!'; +} + +?> +``` + ## Getting Group Users Much like you can easily get the groups the user belongs to, you can also get the members of a group. This will return a collection of user objects: diff --git a/docs/permissions.md b/docs/permissions.md index 682c222..7d32dc6 100644 --- a/docs/permissions.md +++ b/docs/permissions.md @@ -25,6 +25,39 @@ if (Gatekeeper::createPermission($perm) === true) { ?> ``` +You can also set an expiration date on your permissions using the `expire` property: + +```php +<?php +$perm = [ + 'name' => 'perm1', + 'description' => 'Permission #1', + 'expire' => strtotime('+1 day') +]; +?> +``` + +These values are stored as Unix timestamps on the permission records themselves. This will cause the permission to exire, **not** the permission to no longer be allowed for a user (that's in the user-to-permission relationship). You can also check to see if a permission is expired with the `isExpired` method: + +```php +<?php +$permission = Gatekeeper::findPermissionById(1); +if ($permission->isExpired() === true) { + echo 'Oh noes, the permission expired!'; +} +?> +``` + +You can also update the expiration time directly when you have a permission object in hand: + +```php +<?php +$permission = Gatekeeper::findPermissionById(1); +$permission->expire = strtotime('+1 month'); +$permission->save(); +?> +``` + ## Adding Child Permissions Much like groups, permissions also support the concept of children. Adding a permission as a child to a parent is easy and can be done in one of two ways: @@ -41,7 +74,7 @@ $permission->addChild($permission); ?> ``` -### Removing Child Permissions +## Removing Child Permissions You can also remove child permissions in a similar way: diff --git a/docs/users.md b/docs/users.md index 0ee2e51..489ff59 100644 --- a/docs/users.md +++ b/docs/users.md @@ -30,7 +30,7 @@ echo 'Full name: '.$user->firstName.' '.$user->lastName."\n"; ## Getting All Users -You can use the `findUsers` method on the `Gatekeeper` class to get a list (returnes a `UserCollection`) of the current users: +You can use the `findUsers` method on the `Gatekeeper` class to get a list (returns a `UserCollection`) of the current users: ```php $users = Gatekeeper::findUsers(); @@ -57,7 +57,7 @@ Gatekeeper::register($credentials); ``` The return value from the `register` call is a *boolean* indicating the pass/fail status of the registration. -Addiitonally, you can also link the user to permissions at create time: +Additionally, you can also link the user to permissions at create time: ```php <?php @@ -101,7 +101,7 @@ Gatekeeper::register($credentials); ## Removing users -Deleteing user records can be done with the `deleteUserById` method: +Deleting user records can be done with the `deleteUserById` method: ```php <?php @@ -165,11 +165,20 @@ You can add a user to a group by using the group ID: <?php $groupId = 1; if (Gatekeeper::findUserById($userId)->addGroup($groupId) === true) { - echo "User added successfullly!"; + echo "User added successfully!"; } ?> ``` +You can also grant the group to a user with an expiration time, giving them permissions until a certain time. You set the expiration as a second value on the `addGroup` method by passing in a Unix timestamp: + +```php +<?php +if (Gatekeeper::findUserById(1)->addGroup(1, strtotime('+1 day')) === true) { + echo "User added successfully!"; +} +``` + ## Revoking access to a group You can also remove a user from a group by revoking their access: @@ -225,6 +234,16 @@ if (Gatekeeper::findUserById($userId)->addPermission($permissionId) === true) { ?> ``` +You can also provide an optional second parameter with an expiration time if you only want to allow the user the permission for a limited about of time. This parameter should be in the form of a Unix timestamp: + +```php +<?php +Gatekeeper::findUserById(1)->addPermission($permissionId, strtotime('+1 day')); +?> +``` + +When fetching a user's permission list (like with `$user->permissions`) it will only return the non-expired or permanent permissions. + ## Revoking a permission You can remove a permission from a user by revoking it: diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..8fa542c --- /dev/null +++ b/examples/README.md @@ -0,0 +1,9 @@ +Gatekeeper Examples +========================= + +This directory contains some examples of the `Gatekeeper` system in use. Thses examples are just a *starting place* +to help you get up and running quickly and to make it easier to understand how to use the system. + +Examples: + +- [Sample login form](login-form.html)
\ No newline at end of file diff --git a/examples/login-form.html b/examples/login-form.html new file mode 100644 index 0000000..120c078 --- /dev/null +++ b/examples/login-form.html @@ -0,0 +1,36 @@ +<?php +require_once 'vendor/autoload.php'; + +use \Psecio\Gatekeeper\Gatekeeper as g; + +// Replace this path with the location of your +// Gatekeeper .env configuration file +g::init('/path/to/.env'); + +if (isset($_POST['login'])) { + // ProTip: do validation here! + $credentials = array( + 'username' => $_POST['username'], + 'password' => $_POST['password'] + ); + if (g::authenticate($credentials) === true) { + echo 'Login successful!'; + } else { + echo 'Login failed!'; + } +} + +?> +<html> + <head> + <title></title> + </head> + <body> + <form action="/login" method="POST"> + <b>Username:</b> <input type="text" name="username" size="20" maxlength="20"/><br/> + <b>Password:</b> <input type="password" name="password" size="20"/><br/> + <br/> + <input type="submit" name="login" value="Login"/> + </form> + </body> +</html>
\ No newline at end of file diff --git a/migrations/20150702224804_add_permission_group_expire.php b/migrations/20150702224804_add_permission_group_expire.php new file mode 100644 index 0000000..88323b2 --- /dev/null +++ b/migrations/20150702224804_add_permission_group_expire.php @@ -0,0 +1,24 @@ +<?php + +use Phinx\Migration\AbstractMigration; + +class AddPermissionGroupExpire extends \Psecio\Gatekeeper\PhinxMigration +{ + /** + * Migrate Up. + */ + public function up() + { + $this->execute('alter table permissions add expire INT'); + $this->execute('alter table groups add expire INT'); + } + + /** + * Migrate Down. + */ + public function down() + { + $this->execute('alter table permissions drop column expire'); + $this->execute('alter table groups drop column expire'); + } +}
\ No newline at end of file diff --git a/migrations/20150703015048_add_user_permission_group_expire.php b/migrations/20150703015048_add_user_permission_group_expire.php new file mode 100644 index 0000000..ffe3595 --- /dev/null +++ b/migrations/20150703015048_add_user_permission_group_expire.php @@ -0,0 +1,24 @@ +<?php + +use Phinx\Migration\AbstractMigration; + +class AddUserPermissionGroupExpire extends \Psecio\Gatekeeper\PhinxMigration +{ + /** + * Migrate Up. + */ + public function up() + { + $this->execute('alter table user_permission add expire INT'); + $this->execute('alter table group_permission add expire INT'); + } + + /** + * Migrate Down. + */ + public function down() + { + $this->execute('alter table user_permission drop column expire'); + $this->execute('alter table group_permission drop column expire'); + } +}
\ No newline at end of file diff --git a/src/Psecio/Gatekeeper/GroupModel.php b/src/Psecio/Gatekeeper/GroupModel.php index 899336b..3364339 100644 --- a/src/Psecio/Gatekeeper/GroupModel.php +++ b/src/Psecio/Gatekeeper/GroupModel.php @@ -30,6 +30,11 @@ class GroupModel extends \Psecio\Gatekeeper\Model\Mysql 'column' => 'name', 'type' => 'varchar' ), + 'expire' => array( + 'description' => 'Expiration Date', + 'column' => 'expire', + 'type' => 'datetime' + ), 'created' => array( 'description' => 'Date Created', 'column' => 'created', @@ -215,4 +220,14 @@ class GroupModel extends \Psecio\Gatekeeper\Model\Mysql ); return $this->getDb()->delete($childGroup); } + + /** + * Check to see if the group is expired + * + * @return boolean Expired/Not expired result + */ + public function isExpired() + { + return ($this->expire !== null && $this->expire <= time()); + } }
\ No newline at end of file diff --git a/src/Psecio/Gatekeeper/PermissionModel.php b/src/Psecio/Gatekeeper/PermissionModel.php index 89f79a8..3fb1db5 100644 --- a/src/Psecio/Gatekeeper/PermissionModel.php +++ b/src/Psecio/Gatekeeper/PermissionModel.php @@ -49,6 +49,11 @@ class PermissionModel extends \Psecio\Gatekeeper\Model\Mysql 'column' => 'updated', 'type' => 'datetime' ), + 'expire' => array( + 'description' => 'Expiration Date', + 'column' => 'expire', + 'type' => 'datetime' + ), 'children' => array( 'description' => 'Child Permissions', 'type' => 'relation', @@ -103,4 +108,14 @@ class PermissionModel extends \Psecio\Gatekeeper\Model\Mysql ); return $this->getDb()->delete($childPermission); } + + /** + * Test if the permission is expired + * + * @return boolean Expired/not expired + */ + public function isExpired() + { + return ($this->expire !== null && $this->expire <= time()); + } }
\ No newline at end of file diff --git a/src/Psecio/Gatekeeper/PolicyCollection.php b/src/Psecio/Gatekeeper/PolicyCollection.php index 8f390fe..87a22c8 100644 --- a/src/Psecio/Gatekeeper/PolicyCollection.php +++ b/src/Psecio/Gatekeeper/PolicyCollection.php @@ -18,7 +18,7 @@ class PolicyCollection extends \Psecio\Gatekeeper\Collection\Mysql $results = $this->getDb()->fetch($sql); foreach ($results as $result) { - $policy = new PolicyMoel($this->getDb(), $result); + $policy = new PolicyModel($this->getDb(), $result); $this->add($policy); } } diff --git a/src/Psecio/Gatekeeper/UserGroupModel.php b/src/Psecio/Gatekeeper/UserGroupModel.php index a8451b3..4c3269e 100644 --- a/src/Psecio/Gatekeeper/UserGroupModel.php +++ b/src/Psecio/Gatekeeper/UserGroupModel.php @@ -30,6 +30,11 @@ class UserGroupModel extends \Psecio\Gatekeeper\Model\Mysql 'column' => 'id', 'type' => 'integer' ), + 'expire' => array( + 'description' => 'Expiration Date', + 'column' => 'expire', + 'type' => 'datetime' + ), 'created' => array( 'description' => 'Date Created', 'column' => 'created', diff --git a/src/Psecio/Gatekeeper/UserModel.php b/src/Psecio/Gatekeeper/UserModel.php index a5ab7fa..9389fb2 100644 --- a/src/Psecio/Gatekeeper/UserModel.php +++ b/src/Psecio/Gatekeeper/UserModel.php @@ -187,16 +187,21 @@ class UserModel extends \Psecio\Gatekeeper\Model\Mysql * Attach a permission to a user account * * @param integer|PermissionModel $perm Permission ID or model isntance + * @param integer $expire Expiration time of the permission relationship */ - public function addPermission($perm) + public function addPermission($perm, $expire = null) { if ($perm instanceof PermissionModel) { $perm = $perm->id; } - $perm = new UserPermissionModel($this->getDb(), array( + $data = [ 'user_id' => $this->id, 'permission_id' => $perm - )); + ]; + if ($expire !== null && is_int($expire)) { + $data['expire'] = $expire; + } + $perm = new UserPermissionModel($this->getDb(), $data); return $this->getDb()->save($perm); } @@ -224,15 +229,19 @@ class UserModel extends \Psecio\Gatekeeper\Model\Mysql * @param integer|GroupModel $group Add the user to a group * @return boolean Success/fail of add */ - public function addGroup($group) + public function addGroup($group, $expire = null) { if ($group instanceof GroupModel) { $group = $group->id; } - $group = new UserGroupModel($this->getDb(), array( + $data = [ 'group_id' => $group, 'user_id' => $this->id - )); + ]; + if ($expire !== null && is_int($expire)) { + $data['expire'] = $expire; + } + $group = new UserGroupModel($this->getDb(), $data); return $this->getDb()->save($group); } diff --git a/src/Psecio/Gatekeeper/UserPermissionCollection.php b/src/Psecio/Gatekeeper/UserPermissionCollection.php index 0de1d6e..872aafe 100644 --- a/src/Psecio/Gatekeeper/UserPermissionCollection.php +++ b/src/Psecio/Gatekeeper/UserPermissionCollection.php @@ -15,7 +15,8 @@ class UserPermissionCollection extends \Psecio\Gatekeeper\Collection\Mysql $data = array('userId' => $userId); $sql = 'select p.* from '.$prefix.'permissions p, '.$prefix.'user_permission up' .' where p.id = up.permission_id' - .' and up.user_id = :userId'; + .' and up.user_id = :userId' + .' and (up.expire >= UNIX_TIMESTAMP(NOW()) or up.expire is null)'; $results = $this->getDb()->fetch($sql, $data); diff --git a/src/Psecio/Gatekeeper/UserPermissionModel.php b/src/Psecio/Gatekeeper/UserPermissionModel.php index 5bb769f..ca5051a 100644 --- a/src/Psecio/Gatekeeper/UserPermissionModel.php +++ b/src/Psecio/Gatekeeper/UserPermissionModel.php @@ -30,6 +30,11 @@ class UserPermissionModel extends \Psecio\Gatekeeper\Model\Mysql 'column' => 'id', 'type' => 'integer' ), + 'expire' => array( + 'description' => 'Expiration Date', + 'column' => 'expire', + 'type' => 'datetime' + ), 'created' => array( 'description' => 'Date Created', 'column' => 'created', diff --git a/tests/Psecio/Gatekeeper/GroupModelTest.php b/tests/Psecio/Gatekeeper/GroupModelTest.php index ef25e90..aa264ad 100644 --- a/tests/Psecio/Gatekeeper/GroupModelTest.php +++ b/tests/Psecio/Gatekeeper/GroupModelTest.php @@ -178,4 +178,32 @@ class GroupModelTest extends \Psecio\Gatekeeper\Base $group = new GroupModel($ds); $this->assertFalse($group->removeChild(1)); } + + /** + * Test that a group is not expired + */ + public function testGroupNotExpired() + { + $ds = $this->buildMock(true); + $group = new GroupModel($ds, [ + 'id' => 1234, + 'expire' => strtotime('+1 day') + ]); + + $this->assertFalse($group->isExpired()); + } + + /** + * Test that a group is marked as expired + */ + public function testGroupIsExpired() + { + $ds = $this->buildMock(true); + $group = new GroupModel($ds, [ + 'id' => 1234, + 'expire' => strtotime('-1 day') + ]); + + $this->assertTrue($group->isExpired()); + } } diff --git a/tests/Psecio/Gatekeeper/PermissionModelTest.php b/tests/Psecio/Gatekeeper/PermissionModelTest.php index 989983e..e47f8be 100644 --- a/tests/Psecio/Gatekeeper/PermissionModelTest.php +++ b/tests/Psecio/Gatekeeper/PermissionModelTest.php @@ -87,4 +87,32 @@ class PermissionModelTest extends \Psecio\Gatekeeper\Base $perm = new PermissionModel($ds); $this->assertFalse($perm->removeChild(1)); } + + /** + * Test that a permission is not expired + */ + public function testPermissionNotExpired() + { + $ds = $this->buildMock(true); + $perm = new PermissionModel($ds, [ + 'id' => 1234, + 'expire' => strtotime('+1 day') + ]); + + $this->assertFalse($perm->isExpired()); + } + + /** + * Test that a permission is marked as expired + */ + public function testPermissionIsExpired() + { + $ds = $this->buildMock(true); + $perm = new PermissionModel($ds, [ + 'id' => 1234, + 'expire' => strtotime('-1 day') + ]); + + $this->assertTrue($perm->isExpired()); + } }
\ No newline at end of file diff --git a/tests/Psecio/Gatekeeper/PolicyCollectionTest.php b/tests/Psecio/Gatekeeper/PolicyCollectionTest.php new file mode 100644 index 0000000..afa9e7a --- /dev/null +++ b/tests/Psecio/Gatekeeper/PolicyCollectionTest.php @@ -0,0 +1,45 @@ +<?php + +namespace Psecio\Gatekeeper; + +class PolicyCollectionTest extends \Psecio\Gatekeeper\Base +{ + /** + * Test the location of policies in the system + */ + public function testFindPoliciesList() + { + $return = array( + array('name' => 'policy1', 'expression' => 'test expression'), + array('name' => 'policy2', 'expression' => '"group1" in user.groups.getName()') + ); + + $ds = $this->buildMock($return, 'fetch'); + $policies = new PolicyCollection($ds); + + $policies->getList(); + $this->assertCount(2, $policies); + + $policies = $policies->toArray(); + $this->assertTrue($policies[0] instanceof PolicyModel); + } + + /** + * Test the location of policies in the system + */ + public function testFindPoliciesListLimit() + { + $return = array( + array('name' => 'policy1', 'expression' => 'test expression') + ); + + $ds = $this->buildMock($return, 'fetch'); + $policies = new PolicyCollection($ds); + + $policies->getList(1); + $this->assertCount(1, $policies); + + $policies = $policies->toArray(); + $this->assertTrue($policies[0] instanceof PolicyModel); + } +}
\ No newline at end of file diff --git a/tests/Psecio/Gatekeeper/SecurityQuestionCollectionTest.php b/tests/Psecio/Gatekeeper/SecurityQuestionCollectionTest.php new file mode 100644 index 0000000..6ea2ccd --- /dev/null +++ b/tests/Psecio/Gatekeeper/SecurityQuestionCollectionTest.php @@ -0,0 +1,24 @@ +<?php + +namespace Psecio\Gatekeeper; + +class SecurityQuestionCollectionTest extends \Psecio\Gatekeeper\Base +{ + /** + * Test the location of security questions of a user by ID + */ + public function testFindQuestionsByUserId() + { + $userId = 1; + $return = [ + ['question' => 'Arthur', 'answer' => 'Dent', 'user_id' => $userId], + ['name' => 'Ford', 'description' => 'Prefect', 'user_id' => $userId] + ]; + + $ds = $this->buildMock($return, 'fetch'); + $questions = new SecurityQuestionCollection($ds); + + $questions->findByUserId($userId); + $this->assertCount(2, $questions); + } +}
\ No newline at end of file diff --git a/tests/Psecio/Gatekeeper/UserModelTest.php b/tests/Psecio/Gatekeeper/UserModelTest.php index 1e7b671..d1ea4b5 100644 --- a/tests/Psecio/Gatekeeper/UserModelTest.php +++ b/tests/Psecio/Gatekeeper/UserModelTest.php @@ -4,6 +4,30 @@ namespace Psecio\Gatekeeper; class UserModelTest extends \Psecio\Gatekeeper\Base { + private $permissions = array(1, 2, 3); + private $groups = array(1, 2, 3); + + + private function buildPermissionGroupUserMock() + { + $user = $this->getMockBuilder('\Psecio\Gatekeeper\UserModel') + ->disableOriginalConstructor() + ->setMethods(array('grantPermissions', 'grantGroups')) + ->getMock(); + + return $user; + } + + private function buildMysqlDataSourceMock($method = 'save') + { + $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql') + ->disableOriginalConstructor() + ->setMethods(array($method)) + ->getMock(); + + return $ds; + } + /** * Test that a 0 is returned when no throttle record is found (null) */ @@ -279,11 +303,7 @@ class UserModelTest extends \Psecio\Gatekeeper\Base $data = array( array('username' => $username) ); - $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql') - ->disableOriginalConstructor() - ->setMethods(array('fetch')) - ->getMock(); - + $ds = $this->buildMysqlDataSourceMock('fetch'); $ds->method('fetch') ->willReturn($data); @@ -312,11 +332,7 @@ class UserModelTest extends \Psecio\Gatekeeper\Base */ public function testGrantPermissionsByIdValid() { - $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql') - ->disableOriginalConstructor() - ->setMethods(array('save')) - ->getMock(); - + $ds = $this->buildMysqlDataSourceMock(); $ds->method('save')->willReturn(true); $perms = array(1, 2, 3); @@ -329,11 +345,7 @@ class UserModelTest extends \Psecio\Gatekeeper\Base */ public function testGrantPermissionsByIdInalid() { - $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql') - ->disableOriginalConstructor() - ->setMethods(array('save')) - ->getMock(); - + $ds = $this->buildMysqlDataSourceMock(); $ds->method('save')->willReturn(false); $user = new UserModel($ds); $this->assertFalse($user->grantPermissions(array(1, 2, 3))); @@ -344,11 +356,7 @@ class UserModelTest extends \Psecio\Gatekeeper\Base */ public function testGrantPermissionsByModelValid() { - $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql') - ->disableOriginalConstructor() - ->setMethods(array('save')) - ->getMock(); - + $ds = $this->buildMysqlDataSourceMock(); $ds->method('save')->willReturn(true); $perms = array( @@ -365,11 +373,7 @@ class UserModelTest extends \Psecio\Gatekeeper\Base */ public function testGrantGroupsByIdValid() { - $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql') - ->disableOriginalConstructor() - ->setMethods(array('save')) - ->getMock(); - + $ds = $this->buildMysqlDataSourceMock(); $ds->method('save')->willReturn(true); $groups = array(1, 2, 3); @@ -382,11 +386,7 @@ class UserModelTest extends \Psecio\Gatekeeper\Base */ public function testGrantGroupsByIdInvalid() { - $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql') - ->disableOriginalConstructor() - ->setMethods(array('save')) - ->getMock(); - + $ds = $this->buildMysqlDataSourceMock(); $ds->method('save')->willReturn(false); $user = new UserModel($ds); @@ -398,11 +398,7 @@ class UserModelTest extends \Psecio\Gatekeeper\Base */ public function testGrantGroupsByModelValid() { - $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql') - ->disableOriginalConstructor() - ->setMethods(array('save')) - ->getMock(); - + $ds = $this->buildMysqlDataSourceMock(); $ds->method('save')->willReturn(true); $groups = array( @@ -419,20 +415,13 @@ class UserModelTest extends \Psecio\Gatekeeper\Base */ public function testGrantGroupsAndPermissionsAllValid() { - $permissions = array(1, 2, 3); - $groups = array(1, 2, 3); - - $user = $this->getMockBuilder('\Psecio\Gatekeeper\UserModel') - ->disableOriginalConstructor() - ->setMethods(array('grantPermissions', 'grantGroups')) - ->getMock(); - + $user = $this->buildPermissionGroupUserMock(); $user->method('grantPermissions')->willReturn(true); $user->method('grantGroups')->willReturn(true); $result = $user->grant(array( - 'permissions' => $permissions, - 'groups' => $groups + 'permissions' => $this->permissions, + 'groups' => $this->groups )); $this->assertTrue($result); } @@ -442,20 +431,13 @@ class UserModelTest extends \Psecio\Gatekeeper\Base */ public function testGrantGroupsInvalid() { - $permissions = array(1, 2, 3); - $groups = array(1, 2, 3); - - $user = $this->getMockBuilder('\Psecio\Gatekeeper\UserModel') - ->disableOriginalConstructor() - ->setMethods(array('grantPermissions', 'grantGroups')) - ->getMock(); - + $user = $this->buildPermissionGroupUserMock(); $user->method('grantPermissions')->willReturn(true); $user->method('grantGroups')->willReturn(false); $result = $user->grant(array( - 'permissions' => $permissions, - 'groups' => $groups + 'permissions' => $this->permissions, + 'groups' => $this->groups )); $this->assertFalse($result); } @@ -465,20 +447,13 @@ class UserModelTest extends \Psecio\Gatekeeper\Base */ public function testGrantPermissionsInvalid() { - $permissions = array(1, 2, 3); - $groups = array(1, 2, 3); - - $user = $this->getMockBuilder('\Psecio\Gatekeeper\UserModel') - ->disableOriginalConstructor() - ->setMethods(array('grantPermissions', 'grantGroups')) - ->getMock(); - + $user = $this->buildPermissionGroupUserMock(); $user->method('grantPermissions')->willReturn(false); $user->method('grantGroups')->willReturn(true); $result = $user->grant(array( - 'permissions' => $permissions, - 'groups' => $groups + 'permissions' => $this->permissions, + 'groups' => $this->groups )); $this->assertFalse($result); } @@ -528,6 +503,5 @@ class UserModelTest extends \Psecio\Gatekeeper\Base 'question' => 'Question #1', 'answer' => 'mypass' )); - var_export($result); } }
\ No newline at end of file |