lua-casbin icon indicating copy to clipboard operation
lua-casbin copied to clipboard

add keymatch5

Open transtone opened this issue 6 months ago • 4 comments

is there a plan to add keymatch5 ?

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

transtone avatar Dec 12 '23 01:12 transtone

@Edmond-J-A @rushitote @techoner

casbin-bot avatar Dec 12 '23 01:12 casbin-bot

@transtone hi, can you make a PR?

hsluoyz avatar Dec 12 '23 01:12 hsluoyz

@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

transtone avatar Dec 15 '23 09:12 transtone

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

hsluoyz avatar Dec 15 '23 11:12 hsluoyz