Casbin
คำอธิบาย
Casbin เป็นเฟรมเวิร์คการควบคุมการเข้าถึงที่เปิดเผยและมีประสิทธิภาพ ซึ่งกลไกการจัดการสิทธิ์รองรับโมเดลการควบคุมการเข้าถึงหลายประเภท
ที่อยู่โครงการ
https://github.com/teamones-open/casbin
การติดตั้ง
composer require teamones/casbin
เว็บไซต์ Casbin
การใช้งานแบบละเอียดสามารถดูได้จากเอกสารภาษาไทยทางการ ที่นี่จะพูดถึงวิธีการตั้งค่าและใช้ใน webman
https://casbin.org/docs/zh-CN/overview
โครงสร้างไดเรกทอรี
.
├── config ไดเรกทอรีการตั้งค่า
│ ├── casbin-restful-model.conf ไฟล์การกำหนดค่ารูปแบบสิทธิ์ที่ใช้
│ ├── casbin.php การตั้งค่า casbin
......
├── database ไฟล์ฐานข้อมูล
│ ├── migrations ไฟล์การโยกย้าย
│ │ └── 20210218074218_create_rule_table.php
......
ไฟล์โยกย้ายฐานข้อมูล
<?php
use Phinx\Migration\AbstractMigration;
class CreateRuleTable extends AbstractMigration
{
/**
* Change Method.
*
* เขียนการโยกย้ายของคุณที่สามารถย้อนกลับได้โดยใช้วิธีนี้
*
* ข้อมูลเพิ่มเติมเกี่ยวกับการเขียนการโยกย้ายสามารถดูได้จากที่นี่:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* คำสั่งต่อไปนี้สามารถใช้ในวิธีนี้และ Phinx จะ
* ย้อนกลับโดยอัตโนมัติเมื่อทำการคืนค่า:
*
* createTable
* renameTable
* addColumn
* addCustomColumn
* renameColumn
* addIndex
* addForeignKey
*
* การเปลี่ยนแปลงที่ทำลายอื่นๆ จะส่งผลให้เกิดข้อผิดพลาดเมื่อพยายาม
* คืนค่าการโยกย้าย
*
* อย่าลืมเรียกใช้ "create()" หรือ "update()" และไม่ใช่ "save()" เมื่อทำงาน
* กับคลาส Table
*/
public function change()
{
$table = $this->table('rule', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => 'ตารางกฎ']);
// เพิ่มฟิลด์ข้อมูล
$table->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => 'ID หลัก'])
->addColumn('ptype', 'char', ['default' => '', 'limit' => 8, 'comment' => 'ชนิดของกฎ'])
->addColumn('v0', 'string', ['default' => '', 'limit' => 128])
->addColumn('v1', 'string', ['default' => '', 'limit' => 128])
->addColumn('v2', 'string', ['default' => '', 'limit' => 128])
->addColumn('v3', 'string', ['default' => '', 'limit' => 128])
->addColumn('v4', 'string', ['default' => '', 'limit' => 128])
->addColumn('v5', 'string', ['default' => '', 'limit' => 128]);
// ทำการสร้าง
$table->create();
}
}
การตั้งค่า casbin
ไวยากรณ์การกำหนดค่ารูปแบบกฎสิทธิ์สามารถดูได้ที่: https://casbin.org/docs/zh-CN/syntax-for-models
<?php
return [
'default' => [
'model' => [
'config_type' => 'file',
'config_file_path' => config_path() . '/casbin-restful-model.conf', // ไฟล์การกำหนดค่ารูปแบบสิทธิ์
'config_text' => '',
],
'adapter' => [
'type' => 'model', // model หรือ adapter
'class' => \app\model\Rule::class,
],
],
// สามารถกำหนดโมเดลสิทธิ์หลายตัวได้
'rbac' => [
'model' => [
'config_type' => 'file',
'config_file_path' => config_path() . '/casbin-rbac-model.conf', // ไฟล์การกำหนดค่ารูปแบบสิทธิ์
'config_text' => '',
],
'adapter' => [
'type' => 'model', // model หรือ adapter
'class' => \app\model\RBACRule::class,
],
],
];
อะแดปเตอร์
ปัจจุบันอะแดปเตอร์ที่ใช้งานใน composer คือวิธี model ของ think-orm สำหรับ orm อื่นๆ กรุณาอ้างอิงที่ vendor/teamones/src/adapters/DatabaseAdapter.php
จากนั้นปรับเปลี่ยนการตั้งค่า
return [
'default' => [
'model' => [
'config_type' => 'file',
'config_file_path' => config_path() . '/casbin-restful-model.conf', // ไฟล์การกำหนดค่ารูปแบบสิทธิ์
'config_text' => '',
],
'adapter' => [
'type' => 'adapter', // กำหนดประเภทให้เป็นโหมดอะแดปเตอร์
'class' => \app\adapter\DatabaseAdapter::class,
],
],
];
คำแนะนำในการใช้งาน
การนำเข้า
# การนำเข้า
use teamones\casbin\Enforcer;
การใช้งานสองรูปแบบ
# 1. ใช้งานตามค่าปกติใน default
Enforcer::addPermissionForUser('user1', '/user', 'read');
# 1. ใช้งานการตั้งค่า rbac แบบกำหนดเอง
Enforcer::instance('rbac')->addPermissionForUser('user1', '/user', 'read');
แนะนำ API ที่ใช้บ่อย
สำหรับการใช้งาน API เพิ่มเติมกรุณาไปดูที่เอกสารทางการ
- API การจัดการ: https://casbin.org/docs/zh-CN/management-api
- API RBAC: https://casbin.org/docs/zh-CN/rbac-api
# เพิ่มสิทธิ์ให้กับผู้ใช้
Enforcer::addPermissionForUser('user1', '/user', 'read');
# ลบสิทธิ์ของผู้ใช้
Enforcer::deletePermissionForUser('user1', '/user', 'read');
# รับสิทธิ์ทั้งหมดของผู้ใช้
Enforcer::getPermissionsForUser('user1');
# เพิ่มบทบาทให้กับผู้ใช้
Enforcer::addRoleForUser('user1', 'role1');
# เพิ่มสิทธิ์ให้กับบทบาท
Enforcer::addPermissionForUser('role1', '/user', 'edit');
# รับบทบาททั้งหมด
Enforcer::getAllRoles();
# รับบทบาททั้งหมดของผู้ใช้
Enforcer::getRolesForUser('user1');
# รับผู้ตามบทบาท
Enforcer::getUsersForRole('role1');
# ตรวจสอบว่าผู้ใช้เป็นสมาชิกของบทบาทหรือไม่
Enforcer::hasRoleForUser('use1', 'role1');
# ลบบทบาทของผู้ใช้
Enforcer::deleteRoleForUser('use1', 'role1');
# ลบบทบาททั้งหมดของผู้ใช้
Enforcer::deleteRolesForUser('use1');
# ลบบทบาท
Enforcer::deleteRole('role1');
# ลบสิทธิ์
Enforcer::deletePermission('/user', 'read');
# ลบสิทธิ์ทั้งหมดของผู้ใช้หรือบทบาท
Enforcer::deletePermissionsForUser('user1');
Enforcer::deletePermissionsForUser('role1');
# ตรวจสอบสิทธิ์ คืนค่า true หรือ false
Enforcer::enforce("user1", "/user", "edit");