|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CasbinAdapter\ZendDb; |
| 4 | + |
| 5 | +use Casbin\Persist\Adapter as AdapterContract; |
| 6 | +use Casbin\Persist\AdapterHelper; |
| 7 | +use Zend\Db\Adapter\AdapterInterface as ZendDbAdapterInterface; |
| 8 | +use Zend\Db\Adapter\Adapter as ZendDbAdapter; |
| 9 | +use Zend\Db\TableGateway\TableGateway; |
| 10 | +use Zend\Db\TableGateway\TableGatewayInterface; |
| 11 | +use Zend\Db\Sql\Select; |
| 12 | + |
| 13 | +/** |
| 14 | + * Zend Db Adapter for Casbin. |
| 15 | + * |
| 16 | + * @author techlee@qq.com |
| 17 | + */ |
| 18 | +class Adapter implements AdapterContract |
| 19 | +{ |
| 20 | + use AdapterHelper; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var TableGatewayInterface |
| 24 | + */ |
| 25 | + protected $tableGateway; |
| 26 | + |
| 27 | + /** |
| 28 | + * default table name. |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + public $casbinRuleTableName = 'casbin_rule'; |
| 33 | + |
| 34 | + /** |
| 35 | + * the Adapter constructor. |
| 36 | + * |
| 37 | + * @param TableGatewayInterface|ZendDbAdapterInterface|array $config |
| 38 | + */ |
| 39 | + public function __construct($config) |
| 40 | + { |
| 41 | + if ($config instanceof TableGatewayInterface) { |
| 42 | + $this->tableGateway = $config; |
| 43 | + } else { |
| 44 | + if ($config instanceof ZendDbAdapterInterface) { |
| 45 | + $dbAdapter = $config; |
| 46 | + } else { |
| 47 | + $dbAdapter = new ZendDbAdapter($config); |
| 48 | + } |
| 49 | + |
| 50 | + $this->tableGateway = new TableGateway($this->casbinRuleTableName, $dbAdapter); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Initialize a new Adapter. |
| 56 | + * |
| 57 | + * @param TableGatewayInterface|ZendDbAdapterInterface|array $config |
| 58 | + */ |
| 59 | + public static function newAdapter($config) |
| 60 | + { |
| 61 | + return new static($config); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * gets TableGateway. |
| 66 | + * |
| 67 | + * @return TableGatewayInterface |
| 68 | + */ |
| 69 | + public function getTableGateway() |
| 70 | + { |
| 71 | + return $this->tableGateway; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * savePolicyLine function. |
| 76 | + * |
| 77 | + * @param string $ptype |
| 78 | + * @param array $rule |
| 79 | + */ |
| 80 | + public function savePolicyLine($ptype, array $rule) |
| 81 | + { |
| 82 | + $col['ptype'] = $ptype; |
| 83 | + foreach ($rule as $key => $value) { |
| 84 | + $col['v'.strval($key).''] = $value; |
| 85 | + } |
| 86 | + |
| 87 | + $this->tableGateway->insert($col); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * loads all policy rules from the storage. |
| 92 | + * |
| 93 | + * @param Model $model |
| 94 | + */ |
| 95 | + public function loadPolicy($model) |
| 96 | + { |
| 97 | + $rows = $this->tableGateway->select(function (Select $select) { |
| 98 | + $select->columns(['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5']); |
| 99 | + })->toArray(); |
| 100 | + |
| 101 | + foreach ($rows as $row) { |
| 102 | + $line = implode(', ', array_filter($row, function ($val) { |
| 103 | + return '' != $val && !is_null($val); |
| 104 | + })); |
| 105 | + $this->loadPolicyLine(trim($line), $model); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * saves all policy rules to the storage. |
| 111 | + * |
| 112 | + * @param Model $model |
| 113 | + * |
| 114 | + * @return bool |
| 115 | + */ |
| 116 | + public function savePolicy($model) |
| 117 | + { |
| 118 | + foreach ($model->model['p'] as $ptype => $ast) { |
| 119 | + foreach ($ast->policy as $rule) { |
| 120 | + $this->savePolicyLine($ptype, $rule); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + foreach ($model->model['g'] as $ptype => $ast) { |
| 125 | + foreach ($ast->policy as $rule) { |
| 126 | + $this->savePolicyLine($ptype, $rule); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Adds a policy rule to the storage. |
| 135 | + * This is part of the Auto-Save feature. |
| 136 | + * |
| 137 | + * @param string $sec |
| 138 | + * @param string $ptype |
| 139 | + * @param array $rule |
| 140 | + * |
| 141 | + * @return mixed |
| 142 | + */ |
| 143 | + public function addPolicy($sec, $ptype, $rule) |
| 144 | + { |
| 145 | + return $this->savePolicyLine($ptype, $rule); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * This is part of the Auto-Save feature. |
| 150 | + * |
| 151 | + * @param string $sec |
| 152 | + * @param string $ptype |
| 153 | + * @param array $rule |
| 154 | + */ |
| 155 | + public function removePolicy($sec, $ptype, $rule) |
| 156 | + { |
| 157 | + $where['ptype'] = $ptype; |
| 158 | + foreach ($rule as $key => $value) { |
| 159 | + $where['v'.strval($key)] = $value; |
| 160 | + } |
| 161 | + |
| 162 | + $this->tableGateway->delete($where); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * RemoveFilteredPolicy removes policy rules that match the filter from the storage. |
| 167 | + * This is part of the Auto-Save feature. |
| 168 | + * |
| 169 | + * @param string $sec |
| 170 | + * @param string $ptype |
| 171 | + * @param int $fieldIndex |
| 172 | + * @param mixed ...$fieldValues |
| 173 | + */ |
| 174 | + public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues) |
| 175 | + { |
| 176 | + $where['ptype'] = $ptype; |
| 177 | + foreach (range(0, 5) as $value) { |
| 178 | + if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) { |
| 179 | + if ('' != $fieldValues[$value - $fieldIndex]) { |
| 180 | + $where['v'.strval($value)] = $fieldValues[$value - $fieldIndex]; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + $this->tableGateway->delete($where); |
| 186 | + } |
| 187 | +} |
0 commit comments