From a6a7491066893ad414186437488c8c21dfd9eb67 Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Tue, 3 Oct 2023 10:02:29 +0530 Subject: [PATCH] Add #182 Single tenant and multi tenant configuration --- resources/config | 2 +- src/config/config.go | 11 +++++----- src/config/constants.go | 6 ++++++ src/handler/organization_handler.go | 10 +++++++++ src/main/main.go | 4 ++++ src/middleware/middleware.go | 33 ++++++++++++++++++++++++++++- src/org/organizations.go | 9 ++++++++ 7 files changed, 68 insertions(+), 7 deletions(-) diff --git a/resources/config b/resources/config index 743add9..7127a97 160000 --- a/resources/config +++ b/resources/config @@ -1 +1 @@ -Subproject commit 743add9e2fc65b310211cb67832c5697eee862c7 +Subproject commit 7127a9767b3374d257e5fdfd39afef061fc954e3 diff --git a/src/config/config.go b/src/config/config.go index 482cdaf..d3749d6 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -79,11 +79,12 @@ type Configuration struct { UserName string Password string } - Iam Iam - Twilio Twilio - Firebase Firebase - Smtp SmtpConfig - Webhooks WebhooksConfig + ApplicationMode string + Iam Iam + Twilio Twilio + Firebase Firebase + Smtp SmtpConfig + Webhooks WebhooksConfig } // Load the config file diff --git a/src/config/constants.go b/src/config/constants.go index 5ff682b..d0910c4 100644 --- a/src/config/constants.go +++ b/src/config/constants.go @@ -7,3 +7,9 @@ const ( ContentTypeImage = "image/jpeg" ContentTypeFormURLEncoded = "application/x-www-form-urlencoded" ) + +// Application mode +const ( + SingleTenat = "single-tenant" + MultiTenant = "multi-tenant" +) diff --git a/src/handler/organization_handler.go b/src/handler/organization_handler.go index 190d89b..61de00d 100644 --- a/src/handler/organization_handler.go +++ b/src/handler/organization_handler.go @@ -134,6 +134,16 @@ func GetOrganizationByID(w http.ResponseWriter, r *http.Request) { w.Write(response) } +// GetOrganizationId Gets an organization Id. +func GetOrganizationId() (string, error) { + org, err := org.GetOrganization() + if err != nil { + log.Printf("Failed to get organization") + return "", err + } + return org.ID.Hex(), err +} + type orgUpdateReq struct { Name string Location string diff --git a/src/main/main.go b/src/main/main.go index 7959dd3..a7d11f0 100644 --- a/src/main/main.go +++ b/src/main/main.go @@ -13,6 +13,7 @@ import ( "github.com/bb-consent/api/src/firebaseUtils" "github.com/bb-consent/api/src/handler" "github.com/bb-consent/api/src/kafkaUtils" + "github.com/bb-consent/api/src/middleware" "github.com/bb-consent/api/src/notifications" "github.com/bb-consent/api/src/token" "github.com/bb-consent/api/src/webhookdispatcher" @@ -82,6 +83,9 @@ func main() { firebaseUtils.Init(config) log.Println("Firebase initialized") + middleware.ApplicationModeInit(config) + log.Println("Application mode initialized") + // setup casbin auth rules authEnforcer, err := casbin.NewEnforcer("/opt/bb-consent/api/config/auth_model.conf", "/opt/bb-consent/api/config/rbac_policy.csv") if err != nil { diff --git a/src/middleware/middleware.go b/src/middleware/middleware.go index 352c594..8943ec8 100644 --- a/src/middleware/middleware.go +++ b/src/middleware/middleware.go @@ -7,12 +7,13 @@ import ( "time" "github.com/bb-consent/api/src/apikey" + "github.com/bb-consent/api/src/handler" "github.com/bb-consent/api/src/rbac" "github.com/casbin/casbin/v2" "github.com/gorilla/mux" "github.com/bb-consent/api/src/common" - "github.com/bb-consent/api/src/handler" + "github.com/bb-consent/api/src/config" "github.com/bb-consent/api/src/token" "github.com/bb-consent/api/src/user" ) @@ -201,3 +202,33 @@ func Authorize(e *casbin.Enforcer) Middleware { } } } + +var ApplicationMode string + +func ApplicationModeInit(config *config.Configuration) { + ApplicationMode = config.ApplicationMode +} + +// SetApplicationMode sets application modes for routes to either single tenant or multi tenant +func SetApplicationMode() Middleware { + // Create a new Middleware + return func(f http.HandlerFunc) http.HandlerFunc { + + // Define the http.HandlerFunc + return func(w http.ResponseWriter, r *http.Request) { + + if ApplicationMode == config.SingleTenat { + organizationId, err := handler.GetOrganizationId() + if err != nil { + m := "failed to find organization" + common.HandleError(w, http.StatusBadRequest, m, err) + return + } + r.Header.Set("OrganizationID", organizationId) + } + + // Call the next middleware/handler in chain + f(w, r) + } + } +} diff --git a/src/org/organizations.go b/src/org/organizations.go index 2a88f62..972eb2d 100644 --- a/src/org/organizations.go +++ b/src/org/organizations.go @@ -259,6 +259,15 @@ func Get(organizationID string) (Organization, error) { return result, err } +// Get Gets a single organization +func GetOrganization() (Organization, error) { + + var result Organization + err := collection().FindOne(context.TODO(), bson.M{}).Decode(&result) + + return result, err +} + // Update Updates the organization func Update(org Organization) (Organization, error) {