Skip to content

Commit

Permalink
added ActiveRecordAccessTrait and unit testing, updated readme / howt…
Browse files Browse the repository at this point in the history
…o use
  • Loading branch information
Chris Stebe committed Jun 15, 2016
1 parent aca8d58 commit 76dc912
Show file tree
Hide file tree
Showing 34 changed files with 4,638 additions and 14 deletions.
37 changes: 37 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
before_script:
- export BUILD_PREFIX=buildref${CI_BUILD_REF}$(echo ${CI_BUILD_REF_NAME} | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]')
- export COMPOSE_PROJECT_NAME=${BUILD_PREFIX}db
- cd tests

stages:
- test
- report
- cleanup

test:
stage: test
script:
- set +e
- cd tests
- make up setup
- docker-compose run -e YII_ENV=test --rm phpfpm codecept run --html=_report.html; TESTS_EXIT_CODE=$?
- set -e
- mv _output /tmp/${BUILD_PREFIX}
- exit $TESTS_EXIT_CODE

report:
stage: report
script:
- mv /tmp/${BUILD_PREFIX} _output
artifacts:
paths:
- tests/_output/
when: always

cleanup:
stage: cleanup
script:
- docker-compose kill && docker-compose rm -fv
- docker-compose down --rmi local --volumes
when: always

55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Create a file migration class
--templateFile='@vendor/dmstr/yii2-db/db/mysql/templates/file-migration.php' init_dump
```

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

Include it in your console configuration

Expand Down Expand Up @@ -104,6 +104,59 @@ SUB-COMMANDS
- db/index (default) Displays tables in database
```

Traits
---

### [dmstr\db\traits\ActiveRecordAccessTrait](https://github.com/dmstr/yii2-db/blob/master/db/traits/ActiveRecordAccessTrait.php)

How to equip your active record model with access control

- Use update migration in `db/migrations/m160609_090908_add_access_columns`

- set all `$tableNames` to be updated and run migration

This migrations adds the available access check columns to your database table(s)

```
'access_owner',
'access_read',
'access_update',
'access_delete',
'access_domain',
```

- Add `use \dmstr\db\traits\ActiveRecordAccessTrait;` to your active record model

- *(update your cruds)*


**:secret: Congrats, you are now ready to manage specific access checks on your active records!**

:bulb: Access options:

- All access option -> {*}
- specific rbac roles and permissions assignable
- single or multi
- `{*}`
- `{Role1},{Role2},{Permission1},...`

- limit access to specific domains / languages
- single or multi
- `{*}`
- `{de},{en},{fr},...`

- `Owner` access overrides other given permissions
- every active rocord can have exact one owner!

Planned updates:
---

- ActiveRecordAccessTrait
- in cruds use select2 multi for inputs (domain, read, update, delete)
- Setter: authItemArrayToString()
- Getter: authItemStringToArray()


---

Built by [dmstr](http://diemeisterei.de)
23 changes: 23 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
extensions:
enabled:
- Codeception\Extension\RunFailed
modules:
config:
Db:
dsn: ''
user: ''
password: ''
dump: tests/_data/dump.sql
config:
test_entry_url: http://web:80/index.php
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"autoload": {
"psr-4": {
"dmstr\\db\\": "db/",
"dmstr\\db\\tests\\": "db/tests/",
"dmstr\\console\\": "console/"
}
}
Expand Down
29 changes: 29 additions & 0 deletions db/migrations/m160609_090908_add_access_columns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use yii\db\Migration;

class m160609_090908_add_access_columns extends Migration
{
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
// define all table names you want to equip with access control
$tableNames = ['{%table}', '{%table}'];

// add the access control columns to the defined tables
foreach ($tableNames as $tableName) {
$this->addColumn($tableName, 'access_owner', 'INT(11) NULL');
$this->addColumn($tableName, 'access_domain', 'VARCHAR(255) NULL');
$this->addColumn($tableName, 'access_read', 'VARCHAR(255) NULL');
$this->addColumn($tableName, 'access_update', 'VARCHAR(255) NULL');
$this->addColumn($tableName, 'access_delete', 'VARCHAR(255) NULL');
}
}

public function safeDown()
{
echo "m160609_090908_add_access_columns cannot be reverted.\n";

return false;
}
}
Loading

0 comments on commit 76dc912

Please sign in to comment.