Skip to content
This repository was archived by the owner on May 25, 2020. It is now read-only.

Commit 983ec1a

Browse files
committed
First commit.
1 parent 3da3ced commit 983ec1a

11 files changed

+610
-1
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/vendor/
2+
3+
composer.lock
4+
5+
.idea/
6+
*.iml
7+
8+
# coverage report
9+
/build

.travis.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
language: php
2+
sudo: false
3+
4+
php:
5+
- 5.6
6+
- 7.0
7+
- 7.1
8+
- 7.2
9+
- 7.3
10+
11+
services:
12+
- mysql
13+
- postgresql
14+
15+
before_install:
16+
- travis_retry composer self-update
17+
- mysql -e 'create database if not exists casbin;'
18+
- psql -c 'create database casbin;' -U postgres
19+
20+
install:
21+
- travis_retry composer install --no-suggest --no-interaction
22+
23+
script:
24+
- vendor/bin/phpunit --version
25+
- mkdir -p build/logs
26+
- vendor/bin/phpunit
27+
28+
after_script:
29+
- travis_retry vendor/bin/php-coveralls -v

README.md

100644100755
+86-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,86 @@
1-
# zend-db-adapter
1+
# Zend Db Adapter for PHP-Casbin
2+
3+
[![Build Status](https://travis-ci.org/php-casbin/zend-db-adapter.svg?branch=master)](https://travis-ci.org/php-casbin/zend-db-adapter)
4+
[![Coverage Status](https://coveralls.io/repos/github/php-casbin/zend-db-adapter/badge.svg)](https://coveralls.io/github/php-casbin/zend-db-adapter)
5+
[![Latest Stable Version](https://poser.pugx.org/casbin/zend-db-adapter/v/stable)](https://packagist.org/packages/casbin/zend-db-adapter)
6+
[![Total Downloads](https://poser.pugx.org/casbin/zend-db-adapter/downloads)](https://packagist.org/packages/casbin/zend-db-adapter)
7+
[![License](https://poser.pugx.org/casbin/zend-db-adapter/license)](https://packagist.org/packages/casbin/zend-db-adapter)
8+
9+
[Zend-Db](https://github.com/zendframework/zend-db) adapter for [PHP-Casbin](https://github.com/php-casbin/php-casbin).
10+
11+
The list of officially supported drivers:
12+
13+
- `IbmDb2`: The ext/ibm_db2 driver
14+
- `Mysqli`: The ext/mysqli driver
15+
- `Oci8`: The ext/oci8 driver
16+
- `Pgsql`: The ext/pgsql driver
17+
- `Sqlsrv`: The ext/sqlsrv driver (from Microsoft)
18+
- `Pdo_Mysql`: MySQL via the PDO extension
19+
- `Pdo_Sqlite`: SQLite via the PDO extension
20+
- `Pdo_Pgsql`: PostgreSQL via the PDO extension
21+
22+
### Installation
23+
24+
Use [Composer](https://getcomposer.org/).
25+
26+
```
27+
composer require casbin/zend-db-adapter
28+
```
29+
30+
### Usage
31+
32+
Before using it, you need to create a table named `casbin_rule` for Casbin to store the policy.
33+
34+
Take mysql as an example:
35+
36+
```sql
37+
CREATE TABLE `casbin_rule` (
38+
`ptype` varchar(255) NOT NULL,
39+
`v0` varchar(255) DEFAULT NULL,
40+
`v1` varchar(255) DEFAULT NULL,
41+
`v2` varchar(255) DEFAULT NULL,
42+
`v3` varchar(255) DEFAULT NULL,
43+
`v4` varchar(255) DEFAULT NULL,
44+
`v5` varchar(255) DEFAULT NULL
45+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
46+
```
47+
48+
Then you can start like this:
49+
50+
```php
51+
52+
require_once './vendor/autoload.php';
53+
54+
use Casbin\Enforcer;
55+
use Casbin\Util\Log;
56+
use CasbinAdapter\ZendDb\Adapter;
57+
58+
$adapter = new Adapter([
59+
'driver' => 'Pdo_Mysql', // IbmDb2, Mysqli, Oci8, Pgsql, Sqlsrv, Pdo_Mysql, Pdo_Sqlite, Pdo_Pgsql
60+
'hostname' => '127.0.0.1',
61+
'database' => 'test',
62+
'username' => 'root',
63+
'password' => '',
64+
'hostport' => '3306',
65+
]);
66+
67+
$e = new Enforcer('path/to/model.conf', $adapter);
68+
69+
$sub = "alice"; // the user that wants to access a resource.
70+
$obj = "data1"; // the resource that is going to be accessed.
71+
$act = "read"; // the operation that the user performs on the resource.
72+
73+
if ($e->enforce($sub, $obj, $act) === true) {
74+
// permit alice to read data1
75+
} else {
76+
// deny the request, show an error
77+
}
78+
```
79+
80+
### Getting Help
81+
82+
- [php-casbin](https://github.com/php-casbin/php-casbin)
83+
84+
### License
85+
86+
This project is licensed under the [Apache 2.0 license](LICENSE).

composer.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "casbin/zend-db-adapter",
3+
"keywords": ["casbin", "zend-db", "zend-framework", "access-control", "authorization", "database", "permissions"],
4+
"description": "Zend Db Adapter for Casbin, Casbin is a powerful and efficient open-source access control library. ",
5+
"authors": [
6+
{
7+
"name": "TechLee",
8+
"email": "techlee@qq.com"
9+
}
10+
],
11+
"license": "Apache-2.0",
12+
"require": {
13+
"casbin/casbin": "~1.0",
14+
"zendframework/zend-db": "^2.10"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "~5.7|~6.0|~7.0",
18+
"php-coveralls/php-coveralls": "^2.1"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"CasbinAdapter\\ZendDb\\": "src/"
23+
}
24+
},
25+
"autoload-dev": {
26+
"psr-4": {
27+
"CasbinAdapter\\ZendDb\\Tests\\": "tests/"
28+
}
29+
}
30+
}

phpunit.xml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Application Test Suite">
13+
<directory>./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">./src</directory>
19+
</whitelist>
20+
</filter>
21+
<logging>
22+
<log type="coverage-clover" target="build/logs/clover.xml"/>
23+
<log type="coverage-html" target="build/html"/>
24+
</logging>
25+
<php>
26+
<env name="DB_DATABASE" value="casbin"/>
27+
</php>
28+
</phpunit>

src/Adapter.php

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
namespace CasbinAdapter\ZendDb;
4+
5+
use Casbin\Persist\Adapter as AdapterContract;
6+
use Casbin\Persist\AdapterHelper;
7+
use Zend\Db\Adapter\AdapterInterface as ZendDbAdapterInterface;
8+
use Zend\Db\Adapter\Adapter as ZendDbAdapter;
9+
use Zend\Db\TableGateway\TableGateway;
10+
use Zend\Db\TableGateway\TableGatewayInterface;
11+
use Zend\Db\Sql\Select;
12+
13+
/**
14+
* Zend Db Adapter for Casbin.
15+
*
16+
* @author techlee@qq.com
17+
*/
18+
class Adapter implements AdapterContract
19+
{
20+
use AdapterHelper;
21+
22+
/**
23+
* @var TableGatewayInterface
24+
*/
25+
protected $tableGateway;
26+
27+
/**
28+
* default table name.
29+
*
30+
* @var string
31+
*/
32+
public $casbinRuleTableName = 'casbin_rule';
33+
34+
/**
35+
* the Adapter constructor.
36+
*
37+
* @param TableGatewayInterface|ZendDbAdapterInterface|array $config
38+
*/
39+
public function __construct($config)
40+
{
41+
if ($config instanceof TableGatewayInterface) {
42+
$this->tableGateway = $config;
43+
} else {
44+
if ($config instanceof ZendDbAdapterInterface) {
45+
$dbAdapter = $config;
46+
} else {
47+
$dbAdapter = new ZendDbAdapter($config);
48+
}
49+
50+
$this->tableGateway = new TableGateway($this->casbinRuleTableName, $dbAdapter);
51+
}
52+
}
53+
54+
/**
55+
* Initialize a new Adapter.
56+
*
57+
* @param TableGatewayInterface|ZendDbAdapterInterface|array $config
58+
*/
59+
public static function newAdapter($config)
60+
{
61+
return new static($config);
62+
}
63+
64+
/**
65+
* gets TableGateway.
66+
*
67+
* @return TableGatewayInterface
68+
*/
69+
public function getTableGateway()
70+
{
71+
return $this->tableGateway;
72+
}
73+
74+
/**
75+
* savePolicyLine function.
76+
*
77+
* @param string $ptype
78+
* @param array $rule
79+
*/
80+
public function savePolicyLine($ptype, array $rule)
81+
{
82+
$col['ptype'] = $ptype;
83+
foreach ($rule as $key => $value) {
84+
$col['v'.strval($key).''] = $value;
85+
}
86+
87+
$this->tableGateway->insert($col);
88+
}
89+
90+
/**
91+
* loads all policy rules from the storage.
92+
*
93+
* @param Model $model
94+
*/
95+
public function loadPolicy($model)
96+
{
97+
$rows = $this->tableGateway->select(function (Select $select) {
98+
$select->columns(['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5']);
99+
})->toArray();
100+
101+
foreach ($rows as $row) {
102+
$line = implode(', ', array_filter($row, function ($val) {
103+
return '' != $val && !is_null($val);
104+
}));
105+
$this->loadPolicyLine(trim($line), $model);
106+
}
107+
}
108+
109+
/**
110+
* saves all policy rules to the storage.
111+
*
112+
* @param Model $model
113+
*
114+
* @return bool
115+
*/
116+
public function savePolicy($model)
117+
{
118+
foreach ($model->model['p'] as $ptype => $ast) {
119+
foreach ($ast->policy as $rule) {
120+
$this->savePolicyLine($ptype, $rule);
121+
}
122+
}
123+
124+
foreach ($model->model['g'] as $ptype => $ast) {
125+
foreach ($ast->policy as $rule) {
126+
$this->savePolicyLine($ptype, $rule);
127+
}
128+
}
129+
130+
return true;
131+
}
132+
133+
/**
134+
* Adds a policy rule to the storage.
135+
* This is part of the Auto-Save feature.
136+
*
137+
* @param string $sec
138+
* @param string $ptype
139+
* @param array $rule
140+
*
141+
* @return mixed
142+
*/
143+
public function addPolicy($sec, $ptype, $rule)
144+
{
145+
return $this->savePolicyLine($ptype, $rule);
146+
}
147+
148+
/**
149+
* This is part of the Auto-Save feature.
150+
*
151+
* @param string $sec
152+
* @param string $ptype
153+
* @param array $rule
154+
*/
155+
public function removePolicy($sec, $ptype, $rule)
156+
{
157+
$where['ptype'] = $ptype;
158+
foreach ($rule as $key => $value) {
159+
$where['v'.strval($key)] = $value;
160+
}
161+
162+
$this->tableGateway->delete($where);
163+
}
164+
165+
/**
166+
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
167+
* This is part of the Auto-Save feature.
168+
*
169+
* @param string $sec
170+
* @param string $ptype
171+
* @param int $fieldIndex
172+
* @param mixed ...$fieldValues
173+
*/
174+
public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
175+
{
176+
$where['ptype'] = $ptype;
177+
foreach (range(0, 5) as $value) {
178+
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
179+
if ('' != $fieldValues[$value - $fieldIndex]) {
180+
$where['v'.strval($value)] = $fieldValues[$value - $fieldIndex];
181+
}
182+
}
183+
}
184+
185+
$this->tableGateway->delete($where);
186+
}
187+
}

tests/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.db

0 commit comments

Comments
 (0)