Skip to content

Commit 76dc912

Browse files
author
Chris Stebe
committed
added ActiveRecordAccessTrait and unit testing, updated readme / howto use
1 parent aca8d58 commit 76dc912

34 files changed

+4638
-14
lines changed

.gitlab-ci.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
before_script:
2+
- export BUILD_PREFIX=buildref${CI_BUILD_REF}$(echo ${CI_BUILD_REF_NAME} | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]')
3+
- export COMPOSE_PROJECT_NAME=${BUILD_PREFIX}db
4+
- cd tests
5+
6+
stages:
7+
- test
8+
- report
9+
- cleanup
10+
11+
test:
12+
stage: test
13+
script:
14+
- set +e
15+
- cd tests
16+
- make up setup
17+
- docker-compose run -e YII_ENV=test --rm phpfpm codecept run --html=_report.html; TESTS_EXIT_CODE=$?
18+
- set -e
19+
- mv _output /tmp/${BUILD_PREFIX}
20+
- exit $TESTS_EXIT_CODE
21+
22+
report:
23+
stage: report
24+
script:
25+
- mv /tmp/${BUILD_PREFIX} _output
26+
artifacts:
27+
paths:
28+
- tests/_output/
29+
when: always
30+
31+
cleanup:
32+
stage: cleanup
33+
script:
34+
- docker-compose kill && docker-compose rm -fv
35+
- docker-compose down --rmi local --volumes
36+
when: always
37+

README.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Create a file migration class
6767
--templateFile='@vendor/dmstr/yii2-db/db/mysql/templates/file-migration.php' init_dump
6868
```
6969

70-
### dmstr\console\controllers\MysqlControllers
70+
### [dmstr\console\controllers](https://github.com/dmstr/yii2-db/blob/master/console/controllers)
7171

7272
Include it in your console configuration
7373

@@ -104,6 +104,59 @@ SUB-COMMANDS
104104
- db/index (default) Displays tables in database
105105
```
106106

107+
Traits
108+
---
109+
110+
### [dmstr\db\traits\ActiveRecordAccessTrait](https://github.com/dmstr/yii2-db/blob/master/db/traits/ActiveRecordAccessTrait.php)
111+
112+
How to equip your active record model with access control
113+
114+
- Use update migration in `db/migrations/m160609_090908_add_access_columns`
115+
116+
- set all `$tableNames` to be updated and run migration
117+
118+
This migrations adds the available access check columns to your database table(s)
119+
120+
```
121+
'access_owner',
122+
'access_read',
123+
'access_update',
124+
'access_delete',
125+
'access_domain',
126+
```
127+
128+
- Add `use \dmstr\db\traits\ActiveRecordAccessTrait;` to your active record model
129+
130+
- *(update your cruds)*
131+
132+
133+
**:secret: Congrats, you are now ready to manage specific access checks on your active records!**
134+
135+
:bulb: Access options:
136+
137+
- All access option -> {*}
138+
- specific rbac roles and permissions assignable
139+
- single or multi
140+
- `{*}`
141+
- `{Role1},{Role2},{Permission1},...`
142+
143+
- limit access to specific domains / languages
144+
- single or multi
145+
- `{*}`
146+
- `{de},{en},{fr},...`
147+
148+
- `Owner` access overrides other given permissions
149+
- every active rocord can have exact one owner!
150+
151+
Planned updates:
152+
---
153+
154+
- ActiveRecordAccessTrait
155+
- in cruds use select2 multi for inputs (domain, read, update, delete)
156+
- Setter: authItemArrayToString()
157+
- Getter: authItemStringToArray()
158+
159+
107160
---
108161

109162
Built by [dmstr](http://diemeisterei.de)

codeception.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
actor: Tester
2+
paths:
3+
tests: tests
4+
log: tests/_output
5+
data: tests/_data
6+
support: tests/_support
7+
envs: tests/_envs
8+
settings:
9+
bootstrap: _bootstrap.php
10+
colors: true
11+
memory_limit: 1024M
12+
extensions:
13+
enabled:
14+
- Codeception\Extension\RunFailed
15+
modules:
16+
config:
17+
Db:
18+
dsn: ''
19+
user: ''
20+
password: ''
21+
dump: tests/_data/dump.sql
22+
config:
23+
test_entry_url: http://web:80/index.php

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"autoload": {
2323
"psr-4": {
2424
"dmstr\\db\\": "db/",
25+
"dmstr\\db\\tests\\": "db/tests/",
2526
"dmstr\\console\\": "console/"
2627
}
2728
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
class m160609_090908_add_access_columns extends Migration
6+
{
7+
// Use safeUp/safeDown to run migration code within a transaction
8+
public function safeUp()
9+
{
10+
// define all table names you want to equip with access control
11+
$tableNames = ['{%table}', '{%table}'];
12+
13+
// add the access control columns to the defined tables
14+
foreach ($tableNames as $tableName) {
15+
$this->addColumn($tableName, 'access_owner', 'INT(11) NULL');
16+
$this->addColumn($tableName, 'access_domain', 'VARCHAR(255) NULL');
17+
$this->addColumn($tableName, 'access_read', 'VARCHAR(255) NULL');
18+
$this->addColumn($tableName, 'access_update', 'VARCHAR(255) NULL');
19+
$this->addColumn($tableName, 'access_delete', 'VARCHAR(255) NULL');
20+
}
21+
}
22+
23+
public function safeDown()
24+
{
25+
echo "m160609_090908_add_access_columns cannot be reverted.\n";
26+
27+
return false;
28+
}
29+
}

0 commit comments

Comments
 (0)