From 043a66706d072619bf3c6106de8afeca7633aabd Mon Sep 17 00:00:00 2001 From: Kurt Jensen Date: Wed, 22 Nov 2017 21:51:55 -0800 Subject: [PATCH] 1.0.11: This update contains breaking changes that may affect other plugins. Permission methods for PHP code are now accessed by App Service like app('PassageService')::passageKeys() or by alias like PassageService::passageKeys().' --- Plugin.php | 127 ++++++-------------- README.md | 49 ++++++-- UPGRADE.md | 20 ++++ classes/KeyRing.php | 178 ++++++++++++++++++++++++++++ services/PassageServiceProvider.php | 13 ++ updates/version.yaml | 4 +- 6 files changed, 287 insertions(+), 104 deletions(-) create mode 100755 classes/KeyRing.php create mode 100755 services/PassageServiceProvider.php diff --git a/Plugin.php b/Plugin.php index 3f4defa..4b4b4c5 100755 --- a/Plugin.php +++ b/Plugin.php @@ -1,12 +1,10 @@ alias('Passage', '\KurtJensen\Passage\Services\PassageServiceProvider'); - -// App::register('\KurtJensen\Passage\Classes\Keys'); // Register aliases - // $alias = AliasLoader::getInstance(); - // $alias->alias('Passage', '\KurtJensen\Passage\Classes\Keys'); - } - - /** - * Registers any front-end components implemented in this plugin. - * - * @return array - */ - public function registerComponents() { - return [ - //'KurtJensen\Passage\Components\Lock' => 'Lock', - ]; + $alias->alias('PassageService', '\KurtJensen\Passage\Classes\KeyRing'); + App::register('\KurtJensen\Passage\Services\PassageServiceProvider'); } /** @@ -128,110 +111,66 @@ public function registerPermissions() { ]; } - /** - * Registers back-end navigation items for this plugin. - * - * @return array - */ - public function registerNavigation() { - return []; // Remove this line to activate - } - public function registerMarkupTags() { return [ 'functions' => [ - 'can' => function ($lock) {return $this->can($lock);}, - 'inGroup' => function ($group) {return $this->inGroup($group);}, - 'inGroupName' => function ($group) {return $this->inGroupName($group);}, + 'can' => function ($key) {return app('PassageService')::hasKeyName($key);}, + 'hasKeyName' => function ($key) {return app('PassageService')::hasKeyName($key);}, + 'hasKeyNames' => function ($keys) {return app('PassageService')::hasKeyNames($keys);}, + 'hasKey' => function ($key_id) {return app('PassageService')::hasKey($key_id);}, + 'hasKeys' => function ($key_ids) {return app('PassageService')::hasKeys($key_ids);}, + + 'inGroupName' => function ($group) {return app('PassageService')::inGroupName($group);}, + 'inGroupNames' => function ($groups) {return app('PassageService')::inGroupNames($groups);}, + 'inGroup' => function ($group_key) {return app('PassageService')::inGroup($group_key);}, + 'inGroups' => function ($group_keys) {return app('PassageService')::inGroups($group_keys);}, ], ]; } - public static function getUser() { - if (!$user = Auth::getUser()) { - return false; - } - if (!$user->is_activated) { - return false; - } - return $user; - } - - private function inGroup($code) { - $answer = array_key_exists($code, $this->passage_groups()); - if (!$answer) { - $answer = $this->inGroupName($code); - if ($answer) { - trigger_error("Possible Deprecated use of twig function inGroup. The inGroup funtion now should use the unique user group code rather than the user group name.", E_USER_NOTICE); - } - } - return $answer; - } - - private function inGroupName($name) { - if (!$user = self::getUser()) { - return false; - } - return in_array($name, $this->passage_groups()); - } - - private function can($lock) { - return in_array($lock, self::passage_keys()); - } - - public static function passage_groups() { - if (self::$groups === null) { - if (!$user = self::getUser()) { - return self::$groups = []; - } - self::$groups = $user->groups->lists('name', 'code'); - } - return self::$groups; - } - - public static function passage_keys() { - if (self::$keys === null) { - if (!self::getUser()) { - return []; - } - self::$keys = Key::whereHas('groups.users', function ($q) { - $q->where('user_id', self::getUser()->id); - }) - ->lists('name', 'id'); - } - return self::$keys; - } - public static function globalPassageKeys() { - trigger_error("globalPassageKeys() Deprecated use passageKeys() instead.", E_USER_NOTICE); - return \System\Classes\PluginManager::instance()->findByNamespace(__CLASS__)->passage_keys(); + traceLog("Deprecated method \KurtJensen\Passage\Plugin::globalPassageKeys() called. Use PassageService::passageKeys() instead. See Passage Upgrade Guide."); + //trigger_error("Deprecated method \KurtJensen\Passage\Plugin::globalPassageKeys() called. Use app('PassageService')::passageKeys() instead.", E_USER_DEPRECATED); + return app('PassageService')::passageKeys(); } public static function passageKeys() { - return \System\Classes\PluginManager::instance()->findByNamespace(__CLASS__)->passage_keys(); + traceLog("Deprecated method \KurtJensen\Passage\Plugin::passageKeys() called. Use PassageService::passageKeys() instead. See Passage Upgrade Guide."); + //trigger_error("Deprecated method \KurtJensen\Passage\Plugin::passageKeys() called. Use app('PassageService')::passageKeys() instead.", E_USER_DEPRECATED); + return app('PassageService')::passageKeys(); } public static function hasKeyName($key_name) { - $keys = \System\Classes\PluginManager::instance()->findByNamespace(__CLASS__)->passage_keys(); + traceLog("Deprecated method \KurtJensen\Passage\Plugin::hasKeyName() called. Use PassageService::hasKeyName() instead. See Passage Upgrade Guide."); + //trigger_error("Deprecated method \KurtJensen\Passage\Plugin::hasKeyName() called. Use app('PassageService')::hasKeyName() instead.", E_USER_DEPRECATED); + $keys = app('PassageService')::passageKeys(); return in_array($key_name, $keys); } public static function hasKey($key_id) { - $keys = \System\Classes\PluginManager::instance()->findByNamespace(__CLASS__)->passage_keys(); + traceLog("Deprecated method \KurtJensen\Passage\Plugin::hasKey() called. Use PassageService::hasKey() instead. See Passage Upgrade Guide."); + //trigger_error("Deprecated method \KurtJensen\Passage\Plugin::hasKey() called. Use app('PassageService')::hasKey() instead.", E_USER_DEPRECATED); + $keys = app('PassageService')::passageKeys(); return array_key_exists($key_id, $keys); } public static function passageGroups() { - return \System\Classes\PluginManager::instance()->findByNamespace(__CLASS__)->passage_groups(); + traceLog("Deprecated method \KurtJensen\Passage\Plugin::passageGroups() called. Use PassageService::passageGroups() instead. See Passage Upgrade Guide."); + //trigger_error("Deprecated method \KurtJensen\Passage\Plugin::passageGroups() called. Use app('PassageService')::passageGroups() instead.", E_USER_DEPRECATED); + return app('PassageService')::passageGroups(); } public static function hasGroupName($group_name) { - $groups = \System\Classes\PluginManager::instance()->findByNamespace(__CLASS__)->passage_groups(); + traceLog("Deprecated method \KurtJensen\Passage\Plugin::hasGroupName() called. Use PassageService::hasGroupName() instead. See Passage Upgrade Guide."); + //trigger_error("Deprecated method \KurtJensen\Passage\Plugin::hasGroupName() called. Use app('PassageService')::hasGroupName() instead.", E_USER_DEPRECATED); + $groups = app('PassageService')::passageGroups(); return in_array($group_name, $groups); } public static function hasGroup($group_code) { - $groups = \System\Classes\PluginManager::instance()->findByNamespace(__CLASS__)->passage_groups(); + traceLog("Deprecated method \KurtJensen\Passage\Plugin::hasGroup() called. Use PassageService::hasGroup() instead. See Passage Upgrade Guide."); + //trigger_error("Deprecated method \KurtJensen\Passage\Plugin::hasGroup() called. Use app('PassageService')::hasGroup() instead.", E_USER_DEPRECATED); + $groups = app('PassageService')::passageGroups(); return array_key_exists($group_code, $groups); } } \ No newline at end of file diff --git a/README.md b/README.md index d270c9a..941869d 100755 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ In the backend under Users you will find a button at the top called __"User Grou ###User Permisions in Pages or Partials###### -On a page you may restrict access to a portion of view by using the following code: +On a page you may restrict access to a portion of view by using the following twig functions: {% if can('calendar_meetings') %} @@ -57,12 +57,27 @@ On a page you may restrict access to a portion of view by using the following co

This will show for all users regardless of permissions.

-###User Permisions in Your Own Plugins###### +##Available Twig Functions## +- can('KeyName') - Check a passage key name +- hasKeyName('KeyName') - Check a passage key name +- hasKeyNames(['KeyName1','KeyName2','KeyName3']) - Check an array of passage key names +- hasKey(KeyId) (where KeyId is an integer) - Check a passage key id +- hasKeys([KeyId1,KeyId2,KeyId3]) - Check an array of passage key ids + +- inGroupName('GroupName') - Check a passage group name +- inGroupNames(['Group Name','Group Name 2','Group Name 3']) - Check an array of passage group names +- inGroup('GroupCode') - Check a passage group code +- inGroups(['GroupCode1','GroupCode2','GroupCode3']) - Check an array of passage group codes +###User Permisions in Your Own Plugins###### + // Passage Service Methods can be accessed in one of two ways: + $permission_keys_by_name = PassageService::passageKeys(); // by Alias + //OR + $permission_keys_by_name = app('PassageService')::passageKeys(); // by App Service // Get all permision keys for the user in an array - $permission_keys_by_name = \KurtJensen\Passage\Plugin::passageKeys(); + $permission_keys_by_name = app('PassageService')::passageKeys(); /** * OR @@ -71,7 +86,7 @@ On a page you may restrict access to a portion of view by using the following co **/ // check for permission directly using hasKeyName( $key_name ) - $permissionGranted = \KurtJensen\Passage\Plugin::hasKeyName('view_magic_dragon'); + $permissionGranted = app('PassageService')::hasKeyName('view_magic_dragon'); if($permissionGranted) { // Do stuff } @@ -84,12 +99,12 @@ On a page you may restrict access to a portion of view by using the following co * * Example: * $model->perm_id = 5 which came from a dropdown that contained keys - * from \KurtJensen\Passage\Plugin::passageKeys(); + * from PassageService::passageKeys(); **/ $model = Model::first(); // check for permission directly using hasKey( $key_id ) - if(\KurtJensen\Passage\Plugin::hasKey($model->perm_id)) { + if(PassageService::hasKey($model->perm_id)) { // Do Stuff }else{ // Do other Stuff if user does NOT have permission @@ -102,7 +117,7 @@ On a page you may restrict access to a portion of view by using the following co **/ // You can get array of the users groups keyed by the code of the group - $groups = \KurtJensen\Passage\Plugin::passageGroups() + $groups = PassageService::passageGroups() /** * OR @@ -111,7 +126,7 @@ On a page you may restrict access to a portion of view by using the following co **/ // use hasGroup($group_code) to check membership - $isCool = \KurtJensen\Passage\Plugin::hasGroup('cool_people') + $isCool = PassageService::hasGroup('cool_people') /** * OR @@ -122,7 +137,23 @@ On a page you may restrict access to a portion of view by using the following co **/ // use hasGroupName($group_name) to check membership - $isInGroupNamedCool = \KurtJensen\Passage\Plugin::hasGroupName('Cool') + $isInGroupNamedCool = PassageService::hasGroupName('Cool') + +##Available Passage Service Methods## +- passageKeys() - Get an array of all approved passage keys for the user +- can($key_name) - (alias of hasKeyName()) +- hasKeyName($key_name) - Check a passage key name +- hasKey(integer $key_id) - Check a passage key id +- hasKeys(array $check_key_ids) - Check an array of passage key ids +- hasKeyNames(array $check_keys) - Check an array of passage key names + +- passageGroups() - Get an array of all approved passage groups for the user +- inGroupName($group_name) - Check a passage group name +- hasGroupName($group_name) - (alias of inGroupName()) +- inGroup($group_code) - Check a passage group code +- hasGroup($group_code) - (alias of inGroup()) +- inGroups(array $check_group_codes) - Check an array of passage group ids +- inGroupNames(array $check_groups) - Check an array of passage group names ## Like this plugin? diff --git a/UPGRADE.md b/UPGRADE.md index 6ef411f..17650e8 100755 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,8 +1,28 @@ # Upgrade guide +- [Upgrading to 1.0.11 from 1.0.10](#upgrade-1.0.11) + - [Upgrading to 1.0.5 from 1.0.4](#upgrade-1.0.5) - [Upgrading from Shahiem Seymor's Frontend User Roles Manager](#roles) + +## Upgrading To 1.0.11 +**This is an important update that contains breaking changes.** +Permission methods for PHP code have changed. +Passage Service Methods can be accessed in one of two ways: + + $permission_keys_by_name = PassageService::passageKeys(); // by Alias + +or + + $permission_keys_by_name = app('PassageService')::passageKeys(); // by App Service + +This will no longer work after version future version 1.0.12: + + $permission_keys_by_name = \KurtJensen\Passage\Plugin::passageKeys(); + +If you have other plugins that rely on Passage Permissions it is suggested that you ensure they have been updated to be compatible with version 1.0.12 or application will halt by throwing a Deprecated method error. + ## Upgrading To 1.0.5 diff --git a/classes/KeyRing.php b/classes/KeyRing.php new file mode 100755 index 0000000..c59fd19 --- /dev/null +++ b/classes/KeyRing.php @@ -0,0 +1,178 @@ +is_activated) { + return false; + } + return $user; + } + +/** + * Alias of hasKeyName() + * @param string $key_name name of Key + * @return boolean true if user has key + */ + public static function can($key_name) { + return self::hasKeyName($key_name); + } + +/** + * Get an array of all keys approved for user + * @return array approved user keys names keyed by id + */ + public static function passageKeys() { + if (self::$keys === null) { + if (!self::getUser()) { + return []; + } + self::$keys = Key::whereHas('groups.users', function ($q) { + $q->where('user_id', self::getUser()->id); + }) + ->lists('name', 'id'); + } + return self::$keys; + } + +/** + * Test if user has a approved key of a given name + * @param string $key_name name of Key + * @return boolean true if user has key + */ + public static function hasKeyName(string $key_name) { + $keys = self::passageKeys(); + return in_array($key_name, $keys); + } + +/** + * Test if user has a approved key of a given key id + * @param integer $key_id id of a Key + * @return boolean true if user has corresponding key + */ + public static function hasKey(int $key_id) { + $keys = self::passageKeys(); + return array_key_exists($key_id, $keys); + } + +/** + * Test if user has all keys in a given array approved + * @param array $check_keys names of Keys to check + * @return boolean true if user has corresponding keys + */ + public static function hasKeys(array $check_key_ids) { + $keys = array_flip(self::passageKeys()); + return count(array_intersect($check_key_ids, $keys)) == count($check_key_ids); + } + +/** + * Test if user has all keys in a given array approved + * @param array $check_keys names of Keys to check + * @return boolean true if user has corresponding keys + */ + public static function hasKeyNames(array $check_keys) { + $keys = self::passageKeys(); + return count(array_intersect($check_keys, $keys)) == count($check_keys); + } + + /** + * Group methods + */ + +/** + * Get an array of all groups approved for user + * @return array approved user group names keyed by code + */ + public static function passageGroups() { + if (self::$groups === null) { + if (!$user = self::getUser()) { + return self::$groups = []; + } + self::$groups = $user->groups->lists('name', 'code'); + } + return self::$groups; + } + +/** + * Test if user is in a group of a given name + * @param string $group_name name of UsersGroup + * @return boolean true if user is part of group + */ + public static function inGroupName(string $group_name) { + if (!$user = self::getUser()) { + return false; + } + return in_array($group_name, self::passageGroups()); + } + +/** + * Alias for inGroupName() + * @param string $group_name name of UsersGroup + * @return boolean true if user is part of group + */ + public static function hasGroupName(string $group_name) { + return self::inGroupName($group_name); + } + +/** + * Test if user is in a group of a given user group code + * @param string $group_code code of UsersGroup + * @return boolean true if user is part of group + */ + public static function inGroup(string $group_code) { + return array_key_exists($group_code, self::passageGroups()); + } + +/** + * Test if user is in a group of a given user group code + * @param string $group_code code of UsersGroup + * @return boolean true if user is part of group + */ + public static function hasGroup(string $group_code) { + return self::inGroup($group_code); + } + +/** + * Test if user is in groups in a given array of group codes + * @param array $check_group_codes names of Groups to check + * @return boolean true if user is in all groups + */ + public static function inGroups(array $check_group_codes) { + $group_codes = array_flip(self::passageGroups()); + return count(array_intersect($check_group_codes, $group_codes)) == count($check_group_codes); + } + +/** + * Test if user is in groups in a given array of group names + * @param array $check_groups names of Groups to check + * @return boolean true if user is in all groups + */ + public static function inGroupNames(array $check_groups) { + $group_names = self::passageGroups(); + return count(array_intersect($check_groups, $group_names)) == count($check_groups); + } +} \ No newline at end of file diff --git a/services/PassageServiceProvider.php b/services/PassageServiceProvider.php new file mode 100755 index 0000000..adb78bb --- /dev/null +++ b/services/PassageServiceProvider.php @@ -0,0 +1,13 @@ +app->singleton('PassageService', function ($app) { + return new \KurtJensen\Passage\Classes\KeyRing; + }); + } + +} \ No newline at end of file diff --git a/updates/version.yaml b/updates/version.yaml index 5ce0ffd..53abbaa 100755 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -10,4 +10,6 @@ 1.0.7: Fixes dumb mistake at plugin.php line 150 and caches groups better 1.0.8: Added model UsersGroups (from Rainlab) to make easier to get users of a permission key 1.0.9: Prevents unactivated users from having any permissions -1.0.10: Fixed nullpointer when no user is logged in ( Thanks jhendess ) \ No newline at end of file +1.0.10: Fixed nullpointer when no user is logged in ( Thanks jhendess ) +1.0.11: + - '!!! This update contains breaking changes that may affect other plugins. Permission methods for PHP code are now accessed by App Service like app('PassageService')::passageKeys() or by alias like PassageService::passageKeys().' \ No newline at end of file