Skip to content

Commit

Permalink
Update comments, refactoring of AccessManager
Browse files Browse the repository at this point in the history
  • Loading branch information
niksamokhvalov committed Aug 20, 2016
1 parent 1542cf9 commit e6a2159
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 99 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.).

Expand All @@ -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

```
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": [
{
Expand Down
30 changes: 11 additions & 19 deletions src/FileAccessManager.php → src/AccessFileHandler.php
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;
Expand All @@ -18,7 +17,7 @@
*
* @author Nik Samokhvalov <[email protected]>
*/
class FileAccessManager
class AccessFileHandler
{
protected $path;
protected $isFileAccess = false;
Expand All @@ -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;
}
}
Expand All @@ -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];
Expand All @@ -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;
Expand Down
67 changes: 67 additions & 0 deletions src/AccessManager.php
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;
}
}
72 changes: 0 additions & 72 deletions src/GroupAccessManager.php

This file was deleted.

7 changes: 3 additions & 4 deletions src/bootstrap.php
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']);

0 comments on commit e6a2159

Please sign in to comment.