Skip to content

Commit

Permalink
feat: Added Management API for the Enforcer (#51)
Browse files Browse the repository at this point in the history
Signed-off-by: Rushikesh Tote <[email protected]>
  • Loading branch information
rushitote authored Jun 7, 2021
1 parent 59251cd commit 4eee2af
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/main/Enforcer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
--See the License for the specific language governing permissions and
--limitations under the License.

require("src.main.InternalEnforcer")
require("src.main.ManagementEnforcer")

Enforcer = {}
setmetatable(Enforcer, InternalEnforcer)
setmetatable(Enforcer, ManagementEnforcer)

return Enforcer
2 changes: 2 additions & 0 deletions src/main/InternalEnforcer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,5 @@ function InternalEnforcer:removeFilteredPolicy(sec, ptype, fieldIndex, fieldValu
return true
-- TODO: update watcher, add logger
end

return InternalEnforcer
301 changes: 301 additions & 0 deletions src/main/ManagementEnforcer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
--Copyright 2021 The casbin Authors. All Rights Reserved.
--
--Licensed under the Apache License, Version 2.0 (the "License");
--you may not use this file except in compliance with the License.
--You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
--Unless required by applicable law or agreed to in writing, software
--distributed under the License is distributed on an "AS IS" BASIS,
--WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--See the License for the specific language governing permissions and
--limitations under the License.

require("src.main.InternalEnforcer")

-- ManagementEnforcer = InternalEnforcer + Management API.
ManagementEnforcer = {}
setmetatable(ManagementEnforcer, InternalEnforcer)
ManagementEnforcer.__index = ManagementEnforcer

-- GetAllSubjects gets the list of subjects that show up in the current policy.
function ManagementEnforcer:GetAllSubjects()
return self.model:getValuesForFieldInPolicyAllTypes("p", 1)
end

-- GetAllNamedSubjects gets the list of subjects that show up in the current named policy.
function ManagementEnforcer:GetAllNamedSubjects(ptype)
return self.model:getValuesForFieldInPolicy("p", ptype, 1)
end

-- GetAllObjects gets the list of objects that show up in the current policy.
function ManagementEnforcer:GetAllObjects()
return self.model:getValuesForFieldInPolicyAllTypes("p", 2)
end

-- GetAllNamedObjects gets the list of objects that show up in the current named policy.
function ManagementEnforcer:GetAllNamedObjects(ptype)
return self.model:getValuesForFieldInPolicy("p", ptype, 2)
end

-- GetAllActions gets the list of actions that show up in the current policy.
function ManagementEnforcer:GetAllActions()
return self.model:getValuesForFieldInPolicyAllTypes("p", 3)
end

-- GetAllNamedActions gets the list of actions that show up in the current named policy.
function ManagementEnforcer:GetAllNamedActions(ptype)
return self.model:getValuesForFieldInPolicy("p", ptype, 3)
end

-- GetAllRoles gets the list of roles that show up in the current policy.
function ManagementEnforcer:GetAllRoles()
return self.model:getValuesForFieldInPolicyAllTypes("g", 2)
end

-- GetAllNamedRoles gets the list of roles that show up in the current named policy.
function ManagementEnforcer:GetAllNamedRoles(ptype)
return self.model:getValuesForFieldInPolicy("g", ptype, 2)
end

-- GetPolicy gets all the authorization rules in the policy.
function ManagementEnforcer:GetPolicy()
return self:GetNamedPolicy("p")
end

-- GetNamedPolicy gets all the authorization rules in the named policy.
function ManagementEnforcer:GetNamedPolicy(ptype)
return self.model:getPolicy("p", ptype)
end

-- GetFilteredPolicy gets all the authorization rules in the policy, field filters can be specified.
function ManagementEnforcer:GetFilteredPolicy(fieldIndex, ...)
return self:GetFilteredNamedPolicy("p", fieldIndex, ...)
end

-- GetFilteredNamedPolicy gets all the authorization rules in the named policy, field filters can be specified.
function ManagementEnforcer:GetFilteredNamedPolicy(ptype, fieldIndex, ...)
return self.model:getFilteredPolicy("p", ptype, fieldIndex, ...)
end

-- GetGroupingPolicy gets all the role inheritance rules in the policy.
function ManagementEnforcer:GetGroupingPolicy()
return self:GetNamedGroupingPolicy("g")
end
-- GetNamedGroupingPolicy gets all the role inheritance rules in the policy.
function ManagementEnforcer:GetNamedGroupingPolicy(ptype)
return self.model:getPolicy("g", ptype)
end

-- GetFilteredGroupingPolicy gets all the role inheritance rules in the policy, field filters can be specified.
function ManagementEnforcer:GetFilteredGroupingPolicy(fieldIndex, ...)
return self:GetFilteredNamedGroupingPolicy("g", fieldIndex, ...)
end
-- GetFilteredNamedGroupingPolicy gets all the role inheritance rules in the policy, field filters can be specified.
function ManagementEnforcer:GetFilteredNamedGroupingPolicy(ptype, fieldIndex, ...)
return self.model:getFilteredPolicy("g", ptype, fieldIndex, ...)
end

-- HasPolicy determines whether an authorization rule exists.
function ManagementEnforcer:HasPolicy(...)
return self:HasNamedPolicy("p", ...)
end

-- HasNamedPolicy determines whether a named authorization rule exists.
function ManagementEnforcer:HasNamedPolicy(ptype, ...)
local args = {...}
if type(args[1]) == "table" then
return self.model:hasPolicy("p", ptype, args[1])
end
return self.model:hasPolicy("p", ptype, args)
end

--[[
* AddPolicy adds an authorization rule to the current policy.
* If the rule already exists, the function returns false and the rule will not be added.
* Otherwise the function returns true by adding the new rule.
]]
function ManagementEnforcer:AddPolicy(...)
return self:AddNamedPolicy("p", ...)
end

--[[
* AddNamedPolicy adds an authorization rule to the current named policy.
* If the rule already exists, the function returns false and the rule will not be added.
* Otherwise the function returns true by adding the new rule.
]]
function ManagementEnforcer:AddNamedPolicy(ptype, ...)
local args = {...}
if type(args[1]) == "table" then
return self:addPolicy("p", ptype, args[1])
end
return self:addPolicy("p", ptype, args)
end

--[[
* AddPolicies adds authorization rules to the current policy.
* If the rule already exists, the function returns false for the corresponding rule and the rule will not be added.
* Otherwise the function returns true for the corresponding rule by adding the new rule.
]]
function ManagementEnforcer:AddPolicies(rules)
return self:AddNamedPolicies("p", rules)
end

--[[
* AddNamedPolicies adds authorization rules to the current named policy.
* If the rule already exists, the function returns false for the corresponding rule and the rule will not be added.
* Otherwise the function returns true for the corresponding by adding the new rule.
]]
function ManagementEnforcer:AddNamedPolicies(ptype, rules)
return self:addPolicies("p", ptype, rules)
end

-- RemovePolicy removes an authorization rule from the current policy.
function ManagementEnforcer:RemovePolicy(...)
return self:RemoveNamedPolicy("p", ...)
end

-- RemoveNamedPolicy removes an authorization rule from the current named policy.
function ManagementEnforcer:RemoveNamedPolicy(ptype, ...)
local args = {...}
if type(args[1]) == "table" then
return self:removePolicy("p", ptype, args[1])
end
return self:removePolicy("p", ptype, args)
end

-- RemovePolicies removes authorization rules from the current policy.
function ManagementEnforcer:RemovePolicies(rules)
return self:RemoveNamedPolicies("p", rules)
end

-- RemoveNamedPolicy removes an authorization rule from the current named policy.
function ManagementEnforcer:RemoveNamedPolicies(ptype, rules)
return self:removePolicies("p", ptype, rules)
end

-- RemoveFilteredPolicy removes an authorization rule from the current policy, field filters can be specified.
function ManagementEnforcer:RemoveFilteredPolicy(fieldIndex, ...)
return self:RemoveFilteredNamedPolicy("p", fieldIndex, ...)
end

-- RemoveFilteredNamedPolicy removes an authorization rule from the current named policy, field filters can be specified.
function ManagementEnforcer:RemoveFilteredNamedPolicy(ptype, fieldIndex, ...)
return self:removeFilteredPolicy("p", ptype, fieldIndex, {...})
end

-- UpdatePolicy updates an authorization rule from the current policy.
function ManagementEnforcer:UpdatePolicy(oldPolicy, newPolicy)
return self:UpdateNamedPolicy("p", oldPolicy, newPolicy)
end

-- UpdateNamedPolicy updates an authorization rule from the current named policy.
function ManagementEnforcer:UpdateNamedPolicy(ptype, oldPolicy, newPolicy)
return self:updatePolicy("p", ptype, oldPolicy, newPolicy)
end

-- HasGroupingPolicy determines whether a role inheritance rule exists.
function ManagementEnforcer:HasGroupingPolicy(...)
return self:HasNamedGroupingPolicy("g", ...)
end

-- HasNamedGroupingPolicy determines whether a named role inheritance rule exists.
function ManagementEnforcer:HasNamedGroupingPolicy(ptype, ...)
local args = {...}
if type(args[1]) == "table" then
return self.model:hasPolicy("g", ptype, args[1])
end
return self.model:hasPolicy("g", ptype, args)
end

--[[
* AddGroupingPolicy adds a role inheritance rule to the current policy.
* If the rule already exists, the function returns false and the rule will not be added.
* Otherwise the function returns true by adding the new rule.
]]
function ManagementEnforcer:AddGroupingPolicy(...)
return self:AddNamedGroupingPolicy("g", ...)
end

--[[
* AddNamedGroupingPolicy adds a named role inheritance rule to the current policy.
* If the rule already exists, the function returns false and the rule will not be added.
* Otherwise the function returns true by adding the new rule.
]]
function ManagementEnforcer:AddNamedGroupingPolicy(ptype, ...)
local args = {...}
if type(args[1]) == "table" then
return self:addPolicy("g", ptype, args[1])
end
return self:addPolicy("g", ptype, args)
end

--[[
* AddGroupingPolicies adds role inheritance rules to the current policy.
* If the rule already exists, the function returns false for the corresponding policy rule and the rule will not be added.
* Otherwise the function returns true for the corresponding policy rule by adding the new rule.
]]
function ManagementEnforcer:AddGroupingPolicies(rules)
return self:AddNamedGroupingPolicies("g", rules)
end

--[[
* AddNamedGroupingPolicies adds named role inheritance rules to the current policy.
* If the rule already exists, the function returns false for the corresponding policy rule and the rule will not be added.
* Otherwise the function returns true for the corresponding policy rule by adding the new rule.
]]
function ManagementEnforcer:AddNamedGroupingPolicies(ptype, rules)
return self:addPolicies("g", ptype, rules)
end

-- RemoveGroupingPolicy removes a role inheritance rule from the current policy.
function ManagementEnforcer:RemoveGroupingPolicy(...)
return self:RemoveNamedGroupingPolicy("g", ...)
end

-- RemoveNamedGroupingPolicy removes a role inheritance rule from the current named policy.
function ManagementEnforcer:RemoveNamedGroupingPolicy(ptype, ...)
local args = {...}
if type(args[1]) == "table" then
return self:removePolicy("g", ptype, args[1])
end
return self:removePolicy("g", ptype, args)
end

-- RemoveGroupingPolicies removes role inheritance rules from the current policy.
function ManagementEnforcer:RemoveGroupingPolicies(rules)
return self:RemoveNamedGroupingPolicies("g", rules)
end

-- RemoveNamedGroupingPolicies removes role inheritance rules from the current named policy.
function ManagementEnforcer:RemoveNamedGroupingPolicies(ptype, rules)
return self:removePolicies("g", ptype, rules)
end

-- RemoveFilteredGroupingPolicy removes a role inheritance rule from the current policy, field filters can be specified.
function ManagementEnforcer:RemoveFilteredGroupingPolicy(fieldIndex, ...)
return self:RemoveFilteredNamedGroupingPolicy("g", fieldIndex, ...)
end

-- RemoveFilteredNamedGroupingPolicy removes a role inheritance rule from the current named policy, field filters can be specified.
function ManagementEnforcer:RemoveFilteredNamedGroupingPolicy(ptype, fieldIndex, ...)
return self:removeFilteredPolicy("g", ptype, fieldIndex, {...})
end

-- UpdateGroupingPolicy updates a role inheritance rule from the current policy.
function ManagementEnforcer:UpdateGroupingPolicy(oldPolicy, newPolicy)
return self:UpdateNamedGroupingPolicy("g", oldPolicy, newPolicy)
end

-- UpdateNamedGroupingPolicy updates a role inheritance rule from the current named policy.
function ManagementEnforcer:UpdateNamedGroupingPolicy(ptype, oldPolicy, newPolicy)
return self:updatePolicy("g", ptype, oldPolicy, newPolicy)
end

-- AddFunction adds a customized function to the FunctionMap.
function ManagementEnforcer:AddFunction(name, func)
FunctionMap:addFunction(name, func)
end

return ManagementEnforcer
25 changes: 18 additions & 7 deletions src/model/FunctionMap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,27 @@

require("src.util.BuiltInFunctions")

FunctionMap = {}
FunctionMap = {
["keyMatch"] = BuiltInFunctions.keyMatchFunc,
["keyGet"] = BuiltInFunctions.keyGetFunc,
["keyMatch2"] = BuiltInFunctions.keyMatch2Func,
["keyMatch3"] = BuiltInFunctions.keyMatch3Func,
["regexMatch"] = BuiltInFunctions.regexMatchFunc,
["globMatch"] = BuiltInFunctions.globMatch
}

-- FunctionMap provides a set of built in functions
function FunctionMap:new()
local o = {}
o["keyMatch"] = BuiltInFunctions.keyMatchFunc
o["keyGet"] = BuiltInFunctions.keyGetFunc
o["keyMatch2"] = BuiltInFunctions.keyMatch2Func
o["keyMatch3"] = BuiltInFunctions.keyMatch3Func
o["regexMatch"] = BuiltInFunctions.regexMatchFunc
o["globMatch"] = BuiltInFunctions.globMatch
for k, v in pairs(FunctionMap) do
o[k] = v
end
return o
end

-- Add new built-in function to FunctionMap
function FunctionMap:addFunction(key, func)
if not self[key] then
self[key] = func
end
end
16 changes: 15 additions & 1 deletion src/model/Policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ end
* @return the policy rules of section sec and policy type ptype.
]]
function Policy:getPolicy(sec, ptype)
return self.model[sec][ptype].policy
local policy = Util.tableDeepCopy(self.model[sec][ptype].policy)
return policy
end

--[[
Expand Down Expand Up @@ -354,6 +355,19 @@ function Policy:getValuesForFieldInPolicy(sec, ptype, fieldIndex)
return values
end

function Policy:getValuesForFieldInPolicyAllTypes(sec, fieldIndex)
local values = {}

for ptype, _ in pairs(self.model[sec]) do
local tvalues = self:getValuesForFieldInPolicy(sec, ptype, fieldIndex)
for _, v in pairs(tvalues) do
table.insert(values, v)
end
end

return values
end

function Policy:buildIncrementalRoleLinks(rm, op, sec, ptype, rules)
if sec == "g" then
self.model[sec][ptype]:buildIncrementalRoleLinks(rm, op, rules)
Expand Down
Loading

0 comments on commit 4eee2af

Please sign in to comment.