claudejanz/yii2-context-access-filter
Composer 安装命令:
composer require claudejanz/yii2-context-access-filter
包简介
This shows how to implement Context and Access Filter for Yii2 framework
README 文档
README
This shows how to implement Context and Access Filter for Yii2 framework
Installation
The preferred way to install this extension is through composer.
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)
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']); }
claudejanz/yii2-context-access-filter 适用场景与选型建议
claudejanz/yii2-context-access-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 301 次下载、GitHub Stars 达 3, 最近一次更新时间为 2014 年 08 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 claudejanz/yii2-context-access-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 claudejanz/yii2-context-access-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 301
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-4-Clause
- 更新时间: 2014-08-11