From 9ed17fe193ad05b73f22ed5fc30fbdfd2bddc649 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 6 Jan 2022 11:49:35 +0100 Subject: [PATCH] Implemented request matching for the rbac system. --- core/string.cpp | 12 ++++++++++++ core/string.h | 2 ++ modules/rbac/rbac_rank.cpp | 22 +++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/core/string.cpp b/core/string.cpp index 4642e6f..180cbbf 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -335,6 +335,18 @@ int String::compare(const String &other) const { } } +int String::first_difference_index(const String &other) const { + int c = size() < other.size() ? size() : other.size(); + + for (int i = 0; i < c; ++i) { + if (_data[i] != other._data[i]) { + return i; + } + } + + return c; +} + void String::to_lower() { for (int i = 0; i < _size; ++i) { char c = _data[i]; diff --git a/core/string.h b/core/string.h index 081aac9..444d777 100644 --- a/core/string.h +++ b/core/string.h @@ -42,6 +42,8 @@ public: int compare(const String &other) const; + int first_difference_index(const String &other) const; + void to_lower(); String as_lower() const; diff --git a/modules/rbac/rbac_rank.cpp b/modules/rbac/rbac_rank.cpp index ae10e26..afaa95d 100644 --- a/modules/rbac/rbac_rank.cpp +++ b/modules/rbac/rbac_rank.cpp @@ -3,7 +3,27 @@ #include "core/http/request.h" Ref RBACRank::match_request(Request *request) { - return Ref(); + const String &full_path = request->get_path_full(); + + Ref perm; + int current_max = 0; + + for (int i = 0; i < permissions.size(); ++i) { + Ref p; + + if (!p.is_valid()) { + continue; + } + + int c = full_path.first_difference_index(p->url); + + if (c > current_max) { + perm = p; + current_max = c; + } + } + + return perm; } bool RBACRank::get_permissions(Request *request) {