-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update comments, refactoring of AccessManager
- Loading branch information
1 parent
1542cf9
commit e6a2159
Showing
7 changed files
with
90 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
<?php | ||
/** | ||
* @link https://github.com/bitrix-expert/niceaccess | ||
* @copyright Copyright © 2015 Nik Samokhvalov | ||
* @license MIT | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Bex\Niceaccess; | ||
|
@@ -18,7 +17,7 @@ | |
* | ||
* @author Nik Samokhvalov <[email protected]> | ||
*/ | ||
class FileAccessManager | ||
class AccessFileHandler | ||
{ | ||
protected $path; | ||
protected $isFileAccess = false; | ||
|
@@ -32,17 +31,15 @@ class FileAccessManager | |
*/ | ||
public function __construct($path) | ||
{ | ||
if (empty($path)) | ||
{ | ||
if (empty($path)) { | ||
throw new InvalidPathException($path); | ||
} | ||
|
||
$this->path = $path; | ||
|
||
$file = new File($path); | ||
|
||
if ($file->getName() === '.access.php') | ||
{ | ||
if ($file->getName() === '.access.php') { | ||
$this->isFileAccess = true; | ||
} | ||
} | ||
|
@@ -56,27 +53,22 @@ public function __construct($path) | |
*/ | ||
public function convertContent(&$content) | ||
{ | ||
if (!$this->isFileAccess) | ||
{ | ||
if (!$this->isFileAccess) { | ||
return false; | ||
} | ||
elseif (empty($content)) | ||
{ | ||
} elseif (empty($content)) { | ||
return false; | ||
} | ||
|
||
$content = preg_replace_callback('/(PERM\[.+\]\[)(\"G?[0-9]+\")(\])/', function ($matches) { | ||
$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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Bex\Niceaccess; | ||
|
||
use Bex\Tools\Group\GroupTools; | ||
|
||
/** | ||
* Implements methods of checking access of current user. | ||
* | ||
* Example: | ||
* ``` | ||
* use Bex\Niceaccess\AccessManager; | ||
* $result = AccessManager::getInstance()->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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
<?php | ||
/** | ||
* @link https://github.com/bitrix-expert/niceaccess | ||
* @copyright Copyright © 2015 Nik Samokhvalov | ||
* @license MIT | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) return false; | ||
|
||
$manager = \Bitrix\Main\EventManager::getInstance(); | ||
|
||
$manager->addEventHandler('main', 'OnBeforeChangeFile', ['\Bex\Niceaccess\FileAccessManager', 'onBeforeChangeFile']); | ||
$manager->addEventHandler('main', 'OnBeforeChangeFile', ['\Bex\Niceaccess\AccessFileHandler', 'onBeforeChangeFile']); |