From e6a215913dad7d2b58b223ce166583ecec8eeff9 Mon Sep 17 00:00:00 2001 From: Nik Samokhvalov Date: Sun, 21 Aug 2016 00:46:17 +0300 Subject: [PATCH] Update comments, refactoring of AccessManager --- LICENSE.md | 2 +- README.md | 6 ++ composer.json | 5 +- ...ccessManager.php => AccessFileHandler.php} | 30 +++----- src/AccessManager.php | 67 +++++++++++++++++ src/GroupAccessManager.php | 72 ------------------- src/bootstrap.php | 7 +- 7 files changed, 90 insertions(+), 99 deletions(-) rename src/{FileAccessManager.php => AccessFileHandler.php} (79%) create mode 100644 src/AccessManager.php delete mode 100644 src/GroupAccessManager.php diff --git a/LICENSE.md b/LICENSE.md index 7912ff8..a53d418 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright © 2015 Nik Samokhvalov (http://samokhvalov.info) +Copyright © 2015—2016 Nik Samokhvalov (http://samokhvalov.info) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 7ae9e50..ba47362 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ [![Total Downloads](https://poser.pugx.org/bitrix-expert/niceaccess/downloads)](https://packagist.org/packages/bitrix-expert/niceaccess) [![License](https://poser.pugx.org/bitrix-expert/niceaccess/license)](https://packagist.org/packages/bitrix-expert/niceaccess) +## `.access.php` + Bitrix writes `.access.php` (files of access) the numerical group IDs of users, which prevents its migration from site to site where different databases (dev zone, test, production, etc.). @@ -21,6 +23,10 @@ $PERM["admin"]["*"]="D"; ?> ``` +## Tools for nice work with access rights + +Class `\Bex\Niceaccess\AccessManager` implements API of checking access of current user. + ## Installation ``` diff --git a/composer.json b/composer.json index 06f9f19..3c7ef22 100644 --- a/composer.json +++ b/composer.json @@ -1,13 +1,12 @@ { "name": "bitrix-expert/niceaccess", - "description": "", + "description": "Tools for nice work with access rights", "keywords": ["bitrix", "access"], "type": "library", "license": "MIT", "support": { "issues": "https://github.com/bitrix-expert/niceaccess/issues", - "source": "https://github.com/bitrix-expert/niceaccess/", - "wiki": "https://github.com/bitrix-expert/niceaccess/wiki" + "source": "https://github.com/bitrix-expert/niceaccess/" }, "authors": [ { diff --git a/src/FileAccessManager.php b/src/AccessFileHandler.php similarity index 79% rename from src/FileAccessManager.php rename to src/AccessFileHandler.php index 312c9c3..d61893e 100644 --- a/src/FileAccessManager.php +++ b/src/AccessFileHandler.php @@ -1,8 +1,7 @@ */ -class FileAccessManager +class AccessFileHandler { protected $path; protected $isFileAccess = false; @@ -32,8 +31,7 @@ class FileAccessManager */ public function __construct($path) { - if (empty($path)) - { + if (empty($path)) { throw new InvalidPathException($path); } @@ -41,8 +39,7 @@ public function __construct($path) $file = new File($path); - if ($file->getName() === '.access.php') - { + if ($file->getName() === '.access.php') { $this->isFileAccess = true; } } @@ -56,12 +53,9 @@ public function __construct($path) */ public function convertContent(&$content) { - if (!$this->isFileAccess) - { + if (!$this->isFileAccess) { return false; - } - elseif (empty($content)) - { + } elseif (empty($content)) { return false; } @@ -69,14 +63,12 @@ public function convertContent(&$content) $matches[2] = trim($matches[2], "\""); $groupId = str_replace('G', '', $matches[2], $addG); - try - { + try { $groupCode = GroupTools::findById($groupId)->code(); - } catch (ValueNotFoundException $e) - { + } catch (ValueNotFoundException $e) { return $matches[0]; } - + $value = ($addG ? "'G'." : '') . '\Bex\Tools\Group\GroupTools::find(\'' . $groupCode . '\', true)->id()'; return $matches[1] . $value . $matches[3]; @@ -87,7 +79,7 @@ public function convertContent(&$content) public static function onBeforeChangeFile($path, &$content) { - $manager = new FileAccessManager($path); + $manager = new AccessFileHandler($path); $manager->convertContent($content); return true; diff --git a/src/AccessManager.php b/src/AccessManager.php new file mode 100644 index 0000000..8b1dace --- /dev/null +++ b/src/AccessManager.php @@ -0,0 +1,67 @@ +inGroup('group_code'); + * ``` + */ +class AccessManager +{ + /** + * @var static access manager instance + */ + protected static $instance; + + private function __construct() + { + } + + private function __clone() + { + } + + /** + * Get access manager instance. + * + * @return static + */ + public static function getInstance() + { + if (empty(static::$instance)) { + static::$instance = new static; + } + + return static::$instance; + } + + /** + * Check current user entry in group by code. + * + * @param string $code Group's code. + * + * @return bool + */ + public function inGroup($code) + { + $groupId = GroupTools::find($code, true)->id(); + + if ((int) $groupId > 0) { + global $USER; + return in_array($groupId, $USER->GetUserGroupArray()); + } + + return false; + } +} diff --git a/src/GroupAccessManager.php b/src/GroupAccessManager.php deleted file mode 100644 index ed12cf3..0000000 --- a/src/GroupAccessManager.php +++ /dev/null @@ -1,72 +0,0 @@ -check('group_code'); - */ -class GroupAccessManager -{ - /** - * @var static access manager instance - */ - protected static $instance; - - /** - * @inheritdoc - * @see GroupAccessManager::instance() - */ - private function __construct() - { - } - - /** - * @inheritdoc - */ - private function __clone() - { - } - - /** - * Get access manager instance - * @return static - */ - public static function getInstance() - { - if (empty(static::$instance)) { - static::$instance = new static; - } - - return static::$instance; - } - - /** - * Check current user entry in group by code - * @param string $groupCode group's code - * @return bool - */ - public function check($groupCode) - { - $groupId = GroupTools::find($groupCode, true)->id(); - if ((int)$groupId > 0) { - global $USER; - return in_array($groupId, $USER->GetUserGroupArray()); - } - - return false; - } -} - diff --git a/src/bootstrap.php b/src/bootstrap.php index afd4bb2..f3f044f 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -1,12 +1,11 @@ addEventHandler('main', 'OnBeforeChangeFile', ['\Bex\Niceaccess\FileAccessManager', 'onBeforeChangeFile']); +$manager->addEventHandler('main', 'OnBeforeChangeFile', ['\Bex\Niceaccess\AccessFileHandler', 'onBeforeChangeFile']);