PHPackages                             claudejanz/yii2-context-access-filter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. claudejanz/yii2-context-access-filter

ActiveLibrary[Framework](/categories/framework)

claudejanz/yii2-context-access-filter
=====================================

This shows how to implement Context and Access Filter for Yii2 framework

0.0.1(11y ago)3301BSD-4-ClausePHP

Since Nov 26Pushed 8y ago1 watchersCompare

[ Source](https://github.com/claudejanz/yii2-context-access-filter)[ Packagist](https://packagist.org/packages/claudejanz/yii2-context-access-filter)[ RSS](/packages/claudejanz-yii2-context-access-filter/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (1)Versions (2)Used By (0)

Context and Access Filters for yii2 framework
=============================================

[](#context-and-access-filters-for-yii2-framework)

[![Latest Stable Version](https://camo.githubusercontent.com/3dc1e66047c9c0ab0f04f4083e1025b31124b2376f101527daf873b5e163f219/68747470733a2f2f706f7365722e707567782e6f72672f636c617564656a616e7a2f796969322d636f6e746578742d6163636573732d66696c7465722f762f737461626c652e737667)](https://packagist.org/packages/claudejanz/yii2-context-access-filter) [![Total Downloads](https://camo.githubusercontent.com/0f93c5bb5022e87541a3ef0e62b417ae807c27d9eef9db55f664cd3257ac034f/68747470733a2f2f706f7365722e707567782e6f72672f636c617564656a616e7a2f796969322d636f6e746578742d6163636573732d66696c7465722f646f776e6c6f6164732e737667)](https://packagist.org/packages/claudejanz/yii2-context-access-filter) [![Latest Unstable Version](https://camo.githubusercontent.com/fa0f8accc5ad9f4ecd78df7e596fd054b56b7380fe7bc858aff43733abf1a833/68747470733a2f2f706f7365722e707567782e6f72672f636c617564656a616e7a2f796969322d636f6e746578742d6163636573732d66696c7465722f762f756e737461626c652e737667)](https://packagist.org/packages/claudejanz/yii2-context-access-filter) [![License](https://camo.githubusercontent.com/aa0bd5933b5547333cd2249ca0149b0cc6b96bbdb66f4b3af2ea1d7f9dd6edc6/68747470733a2f2f706f7365722e707567782e6f72672f636c617564656a616e7a2f796969322d636f6e746578742d6163636573732d66696c7465722f6c6963656e73652e737667)](https://packagist.org/packages/claudejanz/yii2-context-access-filter)

This shows how to implement Context and Access Filter for Yii2 framework

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
$ php composer.phar require "claudejanz/yii2-context-access-filter": "dev-master"

```

or add

```
"claudejanz/yii2-context-access-filter": "dev-master"

```

to the `require` section of your `composer.json` file.

install rbac as in doc [Role based access control (RBAC) ](http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#rbac)

Usage
-----

[](#usage)

\###in RbacController

```
class RbacController extends Controller {
    public function actionIndex() {
        $auth = Yii::$app->authManager;

        $auth->removeAll();

        // add "view" permission
        $view = $auth->createPermission('view');
        $view->description = 'view';
        $auth->add($view);

        // add "create" permission
        $create = $auth->createPermission('create');
        $create->description = 'create';
        $auth->add($create);

        // add the rule
        $rule = new \claudejanz\contextAccessFilter\rules\OwnRule();
        $auth->add($rule);

        // add "update" permission
        $update = $auth->createPermission('update');
        $update->description = 'update';
        $auth->add($update);

        // add the "updateOwn" permission and associate the rule with it.
        $updateOwn = $auth->createPermission('updateOwn');
        $updateOwn->description = 'update own';
        $updateOwn->ruleName = $rule->name;
        $auth->add($updateOwn);

        // make "updateOwn" child from "update"
        $auth->addChild($update,$updateOwn);

        // add "delete" permission
        $delete = $auth->createPermission('delete');
        $delete->description = 'delete';
        $auth->add($delete);

        // add the "deleteOwn" permission and associate the rule with it.
        $deleteOwn = $auth->createPermission('deleteOwn');
        $deleteOwn->description = 'delete own';
        $deleteOwn->ruleName = $rule->name;
        $auth->add($deleteOwn);

        // make "deleteOwn" child from "delete"
        $auth->addChild($delete,$deleteOwn);

        // add "reader" role and give this role the "view" permission
        $reader = $auth->createRole('reader');
        $auth->add($reader);
        $auth->addChild($reader, $view);

        // add "moderator" role and give this role the "create" permission
        // as well as the permissions of the "updateOwn" and "deleteOwn" role
        // and the permissions of the "reader" role
        $moderator = $auth->createRole('moderator');
        $auth->add($moderator);
        $auth->addChild($moderator, $create);
        $auth->addChild($moderator, $updateOwn);
        $auth->addChild($moderator, $deleteOwn);
        $auth->addChild($moderator, $reader);

        // add "admin" role and give this role the "update" and "delete" permission
        // as well as the permissions of the "moderator" role
        $admin = $auth->createRole('admin');
        $auth->add($admin);
        $auth->addChild($admin, $update);
        $auth->addChild($admin, $delete);
        $auth->addChild($admin, $moderator);

        // Assign roles to users. 1, 2 and 3 are IDs returned by IdentityInterface::getId()
        // usually implemented in your User model.
        $auth->assign($admin, 1);
        $auth->assign($moderator, 2);
        $auth->assign($normal, 3);
    }

}
```

\###in controller

```
    public function behaviors() {
        return [
            'context' =>[
                'class' => \claudejanz\contextAccessFilter\filters\ContextFilter::className(),
                'only' => ['update','delete'],
                // model to load
                'modelName' => Vin::className(),

            ],
            'access' => [
                'class' => \claudejanz\contextAccessFilter\filters\AccessControl::className(),
                'only' => ['create', 'update','delete'],
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['create'],
                        'roles' => ['create'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['update'],
                        'roles' => ['update'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['delete'],
                        'roles' => ['delete'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => \yii\filters\VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    // update function
    public function actionUpdate($id) {
        $model = $this->model;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }
    // delete function
    public function actionDelete($id) {
        $this->model->delete();

        return $this->redirect(['index']);
    }
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

4186d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1180289?v=4)[Claude JANZ](/maintainers/claudejanz)[@claudejanz](https://github.com/claudejanz)

---

Top Contributors

[![claudejanz](https://avatars.githubusercontent.com/u/1180289?v=4)](https://github.com/claudejanz "claudejanz (9 commits)")

### Embed Badge

![Health badge](/badges/claudejanz-yii2-context-access-filter/health.svg)

```
[![Health](https://phpackages.com/badges/claudejanz-yii2-context-access-filter/health.svg)](https://phpackages.com/packages/claudejanz-yii2-context-access-filter)
```

###  Alternatives

[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13825.6k47](/packages/skeeks-cms)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
