From 7fcd38fff7e52ec2036bc4b78c470f57b9d03cc2 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 2 Nov 2021 12:26:26 +0100 Subject: [PATCH] Simple permission sorting. --- modules/rbac/rbac_permission.cpp | 8 ++++++++ modules/rbac/rbac_permission.h | 2 ++ modules/rbac/rbac_rank.cpp | 10 ++++++++++ modules/rbac/rbac_rank.h | 2 ++ 4 files changed, 22 insertions(+) diff --git a/modules/rbac/rbac_permission.cpp b/modules/rbac/rbac_permission.cpp index 7294041..ca78cec 100644 --- a/modules/rbac/rbac_permission.cpp +++ b/modules/rbac/rbac_permission.cpp @@ -1,5 +1,13 @@ #include "rbac_permission.h" +bool RBACPermission::is_smaller(const Ref &b) const { + if (!b.is_valid()) { + return true; + } + + return sort_order < b->sort_order; +} + RBACPermission::RBACPermission() : Resource() { diff --git a/modules/rbac/rbac_permission.h b/modules/rbac/rbac_permission.h index f91f23c..9c15a73 100644 --- a/modules/rbac/rbac_permission.h +++ b/modules/rbac/rbac_permission.h @@ -17,6 +17,8 @@ public: int sort_order; int permissions; + bool is_smaller(const Ref &b) const; + RBACPermission(); ~RBACPermission(); }; diff --git a/modules/rbac/rbac_rank.cpp b/modules/rbac/rbac_rank.cpp index bb60533..80694b1 100644 --- a/modules/rbac/rbac_rank.cpp +++ b/modules/rbac/rbac_rank.cpp @@ -1,5 +1,15 @@ #include "rbac_rank.h" +void RBACRank::sort_permissions() { + for (int i = 0; i < permissions.size(); ++i) { + for (int j = i + 1; j < permissions.size(); ++j) { + if (permissions[j]->is_smaller(permissions[i])) { + permissions.swap(i, j); + } + } + } +} + RBACRank::RBACRank() : Resource() { diff --git a/modules/rbac/rbac_rank.h b/modules/rbac/rbac_rank.h index af21f00..5a62c2c 100644 --- a/modules/rbac/rbac_rank.h +++ b/modules/rbac/rbac_rank.h @@ -22,6 +22,8 @@ public: Vector > permissions; + void sort_permissions(); + RBACRank(); ~RBACRank(); };