diff options
author | Chris Cornutt <enygma@phpdeveloper.org> | 2015-01-24 11:03:24 +0100 |
---|---|---|
committer | Chris Cornutt <enygma@phpdeveloper.org> | 2015-01-24 11:03:24 +0100 |
commit | d28e80f50d291bbfcdd83a47d8a20285de68f978 (patch) | |
tree | a222f8e3dfd0c1cf19ef1332737b6e4192915fff | |
parent | 2458f4f00d771ae50d3c7eb65cda695b197ade41 (diff) | |
download | gatekeeper-1.9.1.zip gatekeeper-1.9.1.tar.gz gatekeeper-1.9.1.tar.bz2 |
refactoring the grant handling (again? yes, again)1.9.1
-rw-r--r-- | src/Psecio/Gatekeeper/UserModel.php | 69 |
1 files changed, 51 insertions, 18 deletions
diff --git a/src/Psecio/Gatekeeper/UserModel.php b/src/Psecio/Gatekeeper/UserModel.php index 090329d..be55065 100644 --- a/src/Psecio/Gatekeeper/UserModel.php +++ b/src/Psecio/Gatekeeper/UserModel.php @@ -370,30 +370,63 @@ class UserModel extends \Psecio\Gatekeeper\Model\Mysql { $return = true; if (isset($config['permissions'])) { - foreach ($config['permissions'] as $permission) { - $permission = ($permission instanceof PermissionModel) ? $permission->id : $permission; - $userPerm = new UserPermissionModel($this->getDb(), array( - 'userId' => $this->id, - 'permissionId' => $permission - )); - $result = $this->getDb()->save($userPerm); - if ($result === false && $return === true) { + $result = $this->grantPermissions($config['permissions']); + if ($result === false && $return === true) { $return = false; - } } } if (isset($config['groups'])) { - foreach ($config['groups'] as $group) { - $group = ($group instanceof GroupModel) ? $group->id : $group; - $userGroup = new UserGroupModel($this->getDb(), array( - 'userId' => $this->id, - 'groupId' => $group - )); - $result = $this->getDb()->save($userGroup); - if ($result === false && $return === true) { + $result = $this->grantGroups($config['groups']); + if ($result === false && $return === true) { $return = false; - } } } + return $return; + } + + /** + * Handle granting of multiple permissions + * + * @param array $permissions Set of permissions (either IDs or objects) + * @return boolean Success/fail of all saves + */ + public function grantPermissions(array $permissions) + { + $return = true; + foreach ($permissions as $permission) { + $permission = ($permission instanceof PermissionModel) ? $permission->id : $permission; + $userPerm = new UserPermissionModel($this->getDb(), array( + 'userId' => $this->id, + 'permissionId' => $permission + )); + $result = $this->getDb()->save($userPerm); + if ($result === false && $return === true) { + $return = false; + } + } + return $return; + } + + /** + * Handle granting of multiple groups + * + * @param array $groups Set of groups (either IDs or objects) + * @return boolean Success/fail of all saves + */ + public function grantGroups(array $groups) + { + $return = true; + foreach ($groups as $group) { + $group = ($group instanceof GroupModel) ? $group->id : $group; + $userGroup = new UserGroupModel($this->getDb(), array( + 'userId' => $this->id, + 'groupId' => $group + )); + $result = $this->getDb()->save($userGroup); + if ($result === false && $return === true) { + $return = false; + } + } + return $return; } }
\ No newline at end of file |