Skip to content

Commit

Permalink
Add #182 Single tenant and multi tenant configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa committed Oct 3, 2023
1 parent d48eaf8 commit a6a7491
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 7 deletions.
2 changes: 1 addition & 1 deletion resources/config
11 changes: 6 additions & 5 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ const (
ContentTypeImage = "image/jpeg"
ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
)

// Application mode
const (
SingleTenat = "single-tenant"
MultiTenant = "multi-tenant"
)
10 changes: 10 additions & 0 deletions src/handler/organization_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
33 changes: 32 additions & 1 deletion src/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
}
}
9 changes: 9 additions & 0 deletions src/org/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down

0 comments on commit a6a7491

Please sign in to comment.