|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests; |
| 4 | + |
| 5 | +use Casbin\Enforcer; |
| 6 | +use CasbinAdapter\Database\Adapter as DatabaseAdapter; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use TechOne\Database\Manager; |
| 9 | + |
| 10 | +class AdapterTest extends TestCase |
| 11 | +{ |
| 12 | + protected $config = []; |
| 13 | + |
| 14 | + protected function initConfig() |
| 15 | + { |
| 16 | + $this->config = [ |
| 17 | + 'type' => 'mysql', // mysql,pgsql,sqlite,sqlsrv |
| 18 | + 'hostname' => $this->env('DB_PORT', '127.0.0.1'), |
| 19 | + 'database' => $this->env('DB_DATABASE', 'casbin'), |
| 20 | + 'username' => $this->env('DB_USERNAME', 'root'), |
| 21 | + 'password' => $this->env('DB_PASSWORD', ''), |
| 22 | + 'hostport' => $this->env('DB_PORT', 3306), |
| 23 | + ]; |
| 24 | + } |
| 25 | + |
| 26 | + protected function initDb() |
| 27 | + { |
| 28 | + $tableName = 'casbin_rule'; |
| 29 | + $conn = (new Manager($this->config))->getConnection(); |
| 30 | + $conn->execute('DELETE FROM '.$tableName); |
| 31 | + $conn->execute('INSERT INTO '.$tableName.' (ptype, v0, v1, v2) VALUES ( \'p\', \'alice\', \'data1\', \'read\') '); |
| 32 | + $conn->execute('INSERT INTO '.$tableName.' (ptype, v0, v1, v2) VALUES ( \'p\', \'bob\', \'data2\', \'write\') '); |
| 33 | + $conn->execute('INSERT INTO '.$tableName.' (ptype, v0, v1, v2) VALUES ( \'p\', \'data2_admin\', \'data2\', \'read\') '); |
| 34 | + $conn->execute('INSERT INTO '.$tableName.' (ptype, v0, v1, v2) VALUES ( \'p\', \'data2_admin\', \'data2\', \'write\') '); |
| 35 | + $conn->execute('INSERT INTO '.$tableName.' (ptype, v0, v1) VALUES ( \'g\', \'alice\', \'data2_admin\') '); |
| 36 | + } |
| 37 | + |
| 38 | + protected function getEnforcer() |
| 39 | + { |
| 40 | + $this->initConfig(); |
| 41 | + $adapter = DatabaseAdapter::newAdapter($this->config); |
| 42 | + $this->initDb(); |
| 43 | + |
| 44 | + return new Enforcer(__DIR__.'/rbac_model.conf', $adapter); |
| 45 | + } |
| 46 | + |
| 47 | + public function testLoadPolicy() |
| 48 | + { |
| 49 | + $e = $this->getEnforcer(); |
| 50 | + $this->assertTrue($e->enforce('alice', 'data1', 'read')); |
| 51 | + $this->assertFalse($e->enforce('bob', 'data1', 'read')); |
| 52 | + $this->assertTrue($e->enforce('bob', 'data2', 'write')); |
| 53 | + $this->assertTrue($e->enforce('alice', 'data2', 'read')); |
| 54 | + $this->assertTrue($e->enforce('alice', 'data2', 'write')); |
| 55 | + } |
| 56 | + |
| 57 | + public function testAddPolicy() |
| 58 | + { |
| 59 | + $e = $this->getEnforcer(); |
| 60 | + $this->assertFalse($e->enforce('eve', 'data3', 'read')); |
| 61 | + |
| 62 | + $e->addPermissionForUser('eve', 'data3', 'read'); |
| 63 | + $this->assertTrue($e->enforce('eve', 'data3', 'read')); |
| 64 | + } |
| 65 | + |
| 66 | + public function testSavePolicy() |
| 67 | + { |
| 68 | + $e = $this->getEnforcer(); |
| 69 | + $this->assertFalse($e->enforce('alice', 'data4', 'read')); |
| 70 | + |
| 71 | + $model = $e->getModel(); |
| 72 | + $model->clearPolicy(); |
| 73 | + $model->addPolicy('p', 'p', ['alice', 'data4', 'read']); |
| 74 | + |
| 75 | + $adapter = $e->getAdapter(); |
| 76 | + $adapter->savePolicy($model); |
| 77 | + $this->assertTrue($e->enforce('alice', 'data4', 'read')); |
| 78 | + } |
| 79 | + |
| 80 | + public function testRemovePolicy() |
| 81 | + { |
| 82 | + $e = $this->getEnforcer(); |
| 83 | + $this->assertFalse($e->enforce('alice', 'data5', 'read')); |
| 84 | + $e->addPermissionForUser('alice', 'data5', 'read'); |
| 85 | + $this->assertTrue($e->enforce('alice', 'data5', 'read')); |
| 86 | + $e->deletePermissionForUser('alice', 'data5', 'read'); |
| 87 | + $this->assertFalse($e->enforce('alice', 'data5', 'read')); |
| 88 | + } |
| 89 | + |
| 90 | + public function testRemoveFilteredPolicy() |
| 91 | + { |
| 92 | + $e = $this->getEnforcer(); |
| 93 | + $this->assertTrue($e->enforce('alice', 'data1', 'read')); |
| 94 | + $e->removeFilteredPolicy(1, 'data1'); |
| 95 | + $this->assertFalse($e->enforce('alice', 'data1', 'read')); |
| 96 | + |
| 97 | + $this->assertTrue($e->enforce('bob', 'data2', 'write')); |
| 98 | + $this->assertTrue($e->enforce('alice', 'data2', 'read')); |
| 99 | + $this->assertTrue($e->enforce('alice', 'data2', 'write')); |
| 100 | + |
| 101 | + $e->removeFilteredPolicy(1, 'data2', 'read'); |
| 102 | + |
| 103 | + $this->assertTrue($e->enforce('bob', 'data2', 'write')); |
| 104 | + $this->assertFalse($e->enforce('alice', 'data2', 'read')); |
| 105 | + $this->assertTrue($e->enforce('alice', 'data2', 'write')); |
| 106 | + |
| 107 | + $e->removeFilteredPolicy(2, 'write'); |
| 108 | + |
| 109 | + $this->assertFalse($e->enforce('bob', 'data2', 'write')); |
| 110 | + $this->assertFalse($e->enforce('alice', 'data2', 'write')); |
| 111 | + } |
| 112 | + |
| 113 | + protected function env($key, $default = null) |
| 114 | + { |
| 115 | + $value = getenv($key); |
| 116 | + if (is_null($default)) { |
| 117 | + return $value; |
| 118 | + } |
| 119 | + |
| 120 | + return false === $value ? $default : $value; |
| 121 | + } |
| 122 | +} |
0 commit comments