casbin / lua-casbin

An authorization library that supports access control models like ACL, RBAC, ABAC in Lua (Kong, APISIX, OpenResty)
Apache License 2.0
55 stars 18 forks source link

add keymatch5 #153

Open transtone opened 9 months ago

transtone commented 9 months ago

is there a plan to add keymatch5 ?

keymatch5 casbin support: https://github.com/casbin/casbin/blob/6703d2f87e113696f2f613cde1597fcec6afcf31/util/builtin_operators.go#L290

casbin-bot commented 9 months ago

@Edmond-J-A @rushitote @techoner

hsluoyz commented 9 months ago

@transtone hi, can you make a PR?

transtone commented 9 months ago

@transtone hi, can you make a PR?

-- // KeyMatch5Func is the wrapper for KeyMatch5.
function BuiltInFunctions.keyMatch5Func(args)
    BuiltInFunctions.validateVariadicArgs(2, args)
    return BuiltInFunctions.keyMatch5(args[1], args[2])
end

-- KeyMatch5 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *
-- For example,
-- - "/foo/bar?status=1&type=2" matches "/foo/bar"
-- - "/parent/child1" and "/parent/child1" matches "/parent/*"
-- - "/parent/child1?status=1" matches "/parent/*"
function BuiltInFunctions.keyMatch5(key1, key2)
    local i, _ = string.find(key1, "?")

    if i then
        key1 = string.sub(key1, 1, i)
    end

    key2 = string.gsub(key2, "/%*", "/.*")
    local key = rex.gsub(key2, "{[^/]+}", "[^/]+")
    return BuiltInFunctions.regexMatch(key1, "^" .. key .. "$")
end
hsluoyz commented 9 months ago

@transtone it's good to see the patch. Can you make a PR?