# 管理 API
提供对Casbin策略管理完全支持的基本API。
## 参考
全局变量`e`是执行者实例。
Go
~~~go
e := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
~~~
Node.js
~~~javascript
const e = await newEnforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv')
~~~
PHP
~~~php
$e = new Enforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv');
~~~
### `GetAllSubjects()`
GetAllSubjects 获取当前策略中显示的主题列表。
For example:
Go
~~~go
allSubjects := e.GetAllSubjects()
~~~
Node.js
~~~javas
const allSubjects = e.getAllSubjects()
~~~
PHP
```php
$allSubjects = $e->getAllSubjects();
```
### `GetAllNamedSubjects()`
GetAllNamedSubjects 获取当前命名策略中显示的主题列表。
For example:
Go
```go
allNamedSubjects := e.GetAllNamedSubjects("p")
```
Node.js
```javascript
const allNamedSubjects = e.getAllNamedSubjects('p')
```
PHP
```php
$allNamedSubjects = $e->getAllNamedSubjects("p");
```
### `GetAllObjects()`
GetAllObjects 获取当前策略中显示的对象列表。
例如:
Go
```go
allObjects := e.GetAllObjects()
```
Node.js
```javascript
const allObjects = e.getAllObjects()
```
PHP
```php
$allObjects = $e->getAllObjects();
```
### `GetAllNamedObjects()`
GetAllNamedObjects 获取当前命名策略中显示的对象列表。
例如:
Go
```go
allNamedObjects := e.GetAllNamedObjects("p")
```
Node.js
```javas
const allNamedObjects = e.getAllNamedObjects('p')
```
PHP
```php
$allNamedObjects = $e->getAllNamedObjects("p");
```
### `GetAllActions()`
GetAllActions 获取当前策略中显示的操作列表。
例如:
Go
```go
allActions := e.GetAllActions()
```
Node.js
```javas
const allActions = e.getAllActions()
```
PHP
```php
$allActions = $e->getAllActions();
```
### `GetAllNamedActions()`
GetAllNamedActions 获取当前命名策略中显示的操作列表。
例如:
Go
```go
allNamedActions := e.GetAllNamedActions("p")
```
Node.js
```javas
const allNamedActions = e.getAllNamedActions('p')
```
PHP
```php
$allNamedActions = $e->getAllNamedActions("p");
```
### `GetAllRoles()`
GetAllRoles获取当前策略中显示的角色列表。
例如:
Go
```go
allRoles = e.GetAllRoles()
```
Node.js
```javas
const allRoles = e.getAllRoles()
```
PHP
```php
$allRoles = $e->getAllRoles();
```
### `GetAllNamedRoles()`
GetAllNamedRoles 获取当前命名策略中显示的角色列表。
例如:
Go
```go
allNamedRoles := e.GetAllNamedRoles("g")
```
Node.js
```javas
const allNamedRoles = e.getAllNamedRoles('g')
```
PHP
```php
$allNamedRoles = $e->getAllNamedRoles('g');
```
### `GetPolicy()`
GetPolicy 获取策略中的所有授权规则。
例如:
Go
```go
policy = e.GetPolicy()
```
Node.js
```javas
const policy = e.getPolicy()
```
PHP
```php
$policy = $e->getPolicy();
```
### `GetFilteredPolicy()`
GetFilteredPolicy 获取策略中的所有授权规则,可以指定字段筛选器。
例如:
Go
```go
filteredPolicy := e.GetFilteredPolicy(0, "alice")
```
Node.js
```javas
const filteredPolicy = e.getFilteredPolicy(0, 'alice')
```
PHP
```php
$filteredPolicy = $e->getFilteredPolicy(0, "alice");
```
### `GetNamedPolicy()`
GetNamedPolicy 获取命名策略中的所有授权规则。
例如:
Go
```go
namedPolicy := e.GetNamedPolicy("p")
```
Node.js
```javas
const namedPolicy = e.getNamedPolicy('p')
```
PHP
```php
$namedPolicy = $e->getNamedPolicy("p");
```
### `GetFilteredNamedPolicy()`
GetFilteredNamedPolicy 获取命名策略中的所有授权规则,可以指定字段过滤器。
例如:
Go
```go
filteredNamedPolicy = e.GetFilteredNamedPolicy("p", 0, "bob")
```
Node.js
```javas
const filteredNamedPolicy = e.getFilteredNamedPolicy('p', 0, 'bob')
```
PHP
```php
$filteredNamedPolicy = $e->getFilteredNamedPolicy("p", 0, "bob");
```
### `GetGroupingPolicy()`
GetGroupingPolicy 获取策略中的所有角色继承规则。
例如:
Go
```go
groupingPolicy := e.GetGroupingPolicy()
```
Node.js
```javas
const groupingPolicy = e.getGroupingPolicy()
```
PHP
```php
$groupingPolicy = $e->getGroupingPolicy();
```
### `GetFilteredGroupingPolicy()`
GetFilteredGroupingPolicy 获取策略中的所有角色继承规则,可以指定字段筛选器。
例如:
Go
```go
filteredGroupingPolicy := e.GetFilteredGroupingPolicy(0, "alice")
```
Node.js
```javas
const filteredGroupingPolicy = e.getFilteredGroupingPolicy(0, 'alice')
```
PHP
```php
$filteredGroupingPolicy = $e->getFilteredGroupingPolicy(0, "alice");
```
### `GetNamedGroupingPolicy()`
GetNamedGroupingPolicy 获取策略中的所有角色继承规则。
例如:
Go
```go
namedGroupingPolicy := e.GetNamedGroupingPolicy("g")
```
Node.js
```javas
const namedGroupingPolicy = e.getNamedGroupingPolicy('g')
```
PHP
```php
$namedGroupingPolicy = $e->getNamedGroupingPolicy("g");
```
### `GetFilteredNamedGroupingPolicy()`
GetFilteredNamedGroupingPolicy 获取策略中的所有角色继承规则。
例如:
Go
```go
namedGroupingPolicy := e.GetFilteredNamedGroupingPolicy("g", 0, "alice")
```
Node.js
```javas
const namedGroupingPolicy = e.getFilteredNamedGroupingPolicy('g', 0, 'alice')
```
PHP
```php
$namedGroupingPolicy = $e->getFilteredNamedGroupingPolicy("g", 0, "alice");
```
### `HasPolicy()`
HasPolicy 确定是否存在授权规则。
例如:
Go
```go
hasPolicy := e.HasPolicy("data2_admin", "data2", "read")
```
Node.js
```javas
const hasPolicy = e.hasPolicy('data2_admin', 'data2', 'read')
```
PHP
```php
$hasPolicy = $e->hasPolicy('data2_admin', 'data2', 'read');
```
### `HasNamedPolicy()`
HasNamedPolicy 确定是否存在命名授权规则。
例如:
Go
```go
hasNamedPolicy := e.HasNamedPolicy("p", "data2_admin", "data2", "read")
```
Node.js
```javas
const hasNamedPolicy = e.hasNamedPolicy('p', 'data2_admin', 'data2', 'read')
```
PHP
```php
$hasNamedPolicy = $e->hasNamedPolicy("p", "data2_admin", "data2", "read");
```
### `AddPolicy()`
AddPolicy 向当前策略添加授权规则。 如果规则已经存在,函数返回false,并且不会添加规则。 否则,函数通过添加新规则并返回true。
例如:
Go
```go
added := e.AddPolicy("eve", "data3", "read")
```
Node.js
```javas
const p = ['eve', 'data3', 'read']
const added = await e.addPolicy(...p)
```
PHP
```php
$added = $e->addPolicy('eve', 'data3', 'read');
```
### `AddNamedPolicy()`
AddNamedPolicy 向当前命名策略添加授权规则。 如果规则已经存在,函数返回false,并且不会添加规则。 否则,函数通过添加新规则并返回true。
例如:
Go
```go
added := e.AddNamedPolicy("p", "eve", "data3", "read")
```
Node.js
```javas
const p = ['eve', 'data3', 'read']
const added = await e.addNamedPolicy('p', ...p)
```
PHP
```php
$added = $e->addNamedPolicy("p", "eve", "data3", "read");
```
### `RemovePolicy()`
RemovePolicy 从当前策略中删除授权规则。
例如:
Go
```go
removed := e.RemovePolicy("alice", "data1", "read")
```
Node.js
```javas
const p = ['alice', 'data1', 'read']
const removed = await e.removePolicy(...p)
```
PHP
```php
$removed = $e->removePolicy("alice", "data1", "read");
```
### `RemoveFilteredPolicy()`
RemoveFilteredPolicy 移除当前策略中的授权规则,可以指定字段筛选器。 RemovePolicy 从当前策略中删除授权规则。
例如:
Go
```go
removed := e.RemoveFilteredPolicy(0, "alice", "data1", "read")
```
Node.js
```javas
const p = ['alice', 'data1', 'read']
const removed = await e.removeFilteredPolicy(0, ...p)
```
PHP
```php
$removed = $e->removeFilteredPolicy(0, "alice", "data1", "read");
```
### `RemoveNamedPolicy()`
RemoveNamedPolicy 从当前命名策略中删除授权规则。
例如:
Go
```go
removed := e.RemoveNamedPolicy("p", "alice", "data1", "read")
```
Node.js
```javas
const p = ['alice', 'data1', 'read']
const removed = await e.removeNamedPolicy('p', ...p)
```
PHP
```php
$removed = $e->removeNamedPolicy("p", "alice", "data1", "read");
```
### `RemoveFilteredNamedPolicy()`
RemoveFilteredNamedPolicy 从当前命名策略中移除授权规则,可以指定字段筛选器。
例如:
Go
```go
removed := e.RemoveFilteredNamedPolicy("p", 0, "alice", "data1", "read")
```
Node.js
```javas
const p = ['alice', 'data1', 'read']
const removed = await e.removeFilteredNamedPolicy('p', 0, ...p)
```
PHP
```php
$removed = $e->removeFilteredNamedPolicy("p", 0, "alice", "data1", "read");
```
### `HasGroupingPolicy()`
HasGroupingPolicy 确定是否存在角色继承规则。
例如:
Go
```go
has := e.HasGroupingPolicy("alice", "data2_admin")
```
Node.js
```javas
const has = e.hasGroupingPolicy('alice', 'data2_admin')
```
PHP
```php
$has = $e->hasGroupingPolicy("alice", "data2_admin");
```
### `HasNamedGroupingPolicy()`
HasNamedGroupingPolicy 确定是否存在命名角色继承规则。
For example:
Go
```go
has := e.HasNamedGroupingPolicy("g", "alice", "data2_admin")
```
Node.js
```javas
const has = e.hasNamedGroupingPolicy('g', 'alice', 'data2_admin')
```
PHP
```php
$has = $e->hasNamedGroupingPolicy("g", "alice", "data2_admin");
```
### `AddGroupingPolicy()`
AddGroupingPolicy 向当前策略添加角色继承规则。 如果规则已经存在,函数返回false,并且不会添加规则。 如果规则已经存在,函数返回false,并且不会添加规则。
例如:
Go
```go
added := e.AddGroupingPolicy("group1", "data2_admin")
```
Node.js
```javas
const added = await e.addGroupingPolicy('group1', 'data2_admin')
```
PHP
```php
$added = $e->addGroupingPolicy("group1", "data2_admin");
```
### `AddNamedGroupingPolicy()`
AddNamedGroupingPolicy 将命名角色继承规则添加到当前策略。 如果规则已经存在,函数返回false,并且不会添加规则。 否则,函数通过添加新规则并返回true。
例如:
Go
```go
added := e.AddNamedGroupingPolicy("g", "group1", "data2_admin")
```
Node.js
```javas
const added = await e.addNamedGroupingPolicy('g', 'group1', 'data2_admin')
```
PHP
```php
$added = $e->addNamedGroupingPolicy("g", "group1", "data2_admin");
```
### `RemoveGroupingPolicy()`
RemoveGroupingPolicy 从当前策略中删除角色继承规则。
例如:
Go
```go
removed := e.RemoveGroupingPolicy("alice", "data2_admin")
```
Node.js
```javas
const removed = await e.removeGroupingPolicy('alice', 'data2_admin')
```
PHP
```php
$removed = $e->removeGroupingPolicy("alice", "data2_admin");
```
### `RemoveFilteredGroupingPolicy()`
RemoveFilteredGroupingPolicy 从当前策略中移除角色继承规则,可以指定字段筛选器。
例如:
Go
```go
removed := e.RemoveFilteredGroupingPolicy(0, "alice")
```
Node.js
```javas
const removed = await e.removeFilteredGroupingPolicy(0, 'alice')
```
PHP
```php
$removed = $e->removeFilteredGroupingPolicy(0, "alice");
```
### `RemoveNamedGroupingPolicy()`
RemoveNamedGroupingPolicy 从当前命名策略中移除角色继承规则。
例如:
Go
```go
removed := e.RemoveNamedGroupingPolicy("g", "alice")
```
Node.js
```javas
const removed = await e.removeNamedGroupingPolicy('g', 'alice')
```
PHP
```php
$removed = $e->removeNamedGroupingPolicy("g", "alice");
```
### `RemoveFilteredNamedGroupingPolicy()`
RemoveFilteredNamedGroupingPolicy 从当前命名策略中移除角色继承规则,可以指定字段筛选器。
例如:
Go
```go
removed := e.RemoveFilteredNamedGroupingPolicy("g", 0, "alice")
```
Node.js
```javas
const removed = await e.removeFilteredNamedGroupingPolicy('g', 0, 'alice')
```
PHP
```php
$removed = $e->removeFilteredNamedGroupingPolicy("g", 0, "alice");
```
### `AddFunction()`
AddFunction 添加自定义函数。
例如:
Go
```go
func CustomFunction(key1 string, key2 string) bool {
if key1 == "/alice_data2/myid/using/res_id" && key2 == "/alice_data/:resource" {
return true
} else if key1 == "/alice_data2/myid/using/res_id" && key2 == "/alice_data2/:id/using/:resId" {
return true
} else {
return false
}
}
func CustomFunctionWrapper(args ...interface{}) (interface{}, error) {
key1 := args[0].(string)
key2 := args[1].(string)
return bool(CustomFunction(key1, key2)), nil
}
e.AddFunction("keyMatchCustom", CustomFunctionWrapper)
```
Node.js
```javas
Method is not implemented
```
PHP
```php
func customFunction($key1, $key2) {
if ($key1 == "/alice_data2/myid/using/res_id" && $key2 == "/alice_data/:resource") {
return true;
} elseif ($key1 == "/alice_data2/myid/using/res_id" && $key2 == "/alice_data2/:id/using/:resId") {
return true;
} else {
return false;
}
}
func customFunctionWrapper(...$args){
$key1 := $args[0];
$key2 := $args[1];
return customFunction($key1, $key2);
}
$e->addFunction("keyMatchCustom", customFunctionWrapper);
```