Skip to content

Commit

Permalink
Fix #408: Reorganise folder structure to flat hierarchy
Browse files Browse the repository at this point in the history
Signed-off-by: George J Padayatti <[email protected]>
  • Loading branch information
georgepadayatti committed Oct 31, 2023
1 parent 11aaa06 commit 1963ab4
Show file tree
Hide file tree
Showing 47 changed files with 84 additions and 18 deletions.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package individual

import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"

"github.com/Nerzal/gocloak/v13"
"github.com/asaskevich/govalidator"
"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/config"
"github.com/bb-consent/api/internal/iam"
"github.com/bb-consent/api/internal/individual"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type iamIndividualRegisterReq struct {
Username string `json:"username"`
Firstname string `json:"firstName"`
Email string `json:"email"`
Enabled bool `json:"enabled"`
RequiredActions []string `json:"requiredActions"`
}

func createIamRegisterRequestFromAddRequestBody(requestBody addServiceIndividualReq, iamRegReq iamIndividualRegisterReq) iamIndividualRegisterReq {

iamRegReq.Username = requestBody.Individual.Email
Expand Down Expand Up @@ -43,6 +54,22 @@ type addServiceIndividualResp struct {
Individual individual.Individual `json:"individual"`
}

// registerUser Registers a new user
func registerUser(iamRegReq iamIndividualRegisterReq, adminToken string, client *gocloak.GoCloak) (string, error) {
user := gocloak.User{
FirstName: &iamRegReq.Firstname,
Email: &iamRegReq.Email,
Enabled: gocloak.BoolP(true),
Username: &iamRegReq.Email,
}

iamId, err := client.CreateUser(context.Background(), adminToken, iam.IamConfig.Realm, user)
if err != nil {
return "", err
}
return iamId, err
}

// ServiceCreateIndividual
func ServiceCreateIndividual(w http.ResponseWriter, r *http.Request) {
// Headers
Expand All @@ -66,7 +93,7 @@ func ServiceCreateIndividual(w http.ResponseWriter, r *http.Request) {

iamRegReq = createIamRegisterRequestFromAddRequestBody(individualReq, iamRegReq)

client := getClient()
client := iam.GetClient()

t, err := getAdminToken(client)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package individual

import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"

"github.com/Nerzal/gocloak/v13"
"github.com/asaskevich/govalidator"
"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/config"
"github.com/bb-consent/api/internal/iam"
"github.com/bb-consent/api/internal/individual"
"github.com/gorilla/mux"
)

type iamIndividualUpdateReq struct {
Username string `json:"username"`
Firstname string `json:"firstName"`
Email string `json:"email"`
}

func updateIamUpdateRequestFromUpdateRequestBody(requestBody updateServiceIndividualReq) iamIndividualUpdateReq {
var iamIndividualReq iamIndividualUpdateReq

Expand All @@ -23,6 +33,34 @@ func updateIamUpdateRequestFromUpdateRequestBody(requestBody updateServiceIndivi
return iamIndividualReq
}

func getAdminToken(client *gocloak.GoCloak) (*gocloak.JWT, error) {
t, err := iam.GetToken(iam.IamConfig.AdminUser, iam.IamConfig.AdminPassword, "master", client)
return t, err
}

// updateIamIndividual Update user info on IAM server end.
func updateIamIndividual(iamUpdateReq iamIndividualUpdateReq, iamID string) error {
client := iam.GetClient()

t, err := getAdminToken(client)
if err != nil {
log.Printf("Failed to get admin token, user: %v update err:%v", iamUpdateReq.Firstname, err)
return err
}
user, err := client.GetUserByID(context.Background(), t.AccessToken, iam.IamConfig.Realm, iamID)
if err != nil {
return err
}
user.FirstName = gocloak.StringP(iamUpdateReq.Firstname)
user.Username = gocloak.StringP(iamUpdateReq.Username)
user.Email = gocloak.StringP(iamUpdateReq.Email)
u := *user

err = client.UpdateUser(context.Background(), t.AccessToken, iam.IamConfig.Realm, u)

return err
}

func updateIndividualFromUpdateIndividualServiceRequestBody(requestBody updateServiceIndividualReq, tobeUpdatedIndividual individual.Individual) individual.Individual {
tobeUpdatedIndividual.ExternalId = requestBody.Individual.ExternalId
tobeUpdatedIndividual.ExternalIdType = requestBody.Individual.ExternalIdType
Expand Down
35 changes: 18 additions & 17 deletions internal/http_path/v2/routes.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package http_path

import (
apiKeyHandler "github.com/bb-consent/api/internal/handler/v2/apikey"
auditHandler "github.com/bb-consent/api/internal/handler/v2/audit"
dataAgreementHandler "github.com/bb-consent/api/internal/handler/v2/dataagreement"
dataAttributeHandler "github.com/bb-consent/api/internal/handler/v2/dataattribute"
idpHandler "github.com/bb-consent/api/internal/handler/v2/idp"
individualHandler "github.com/bb-consent/api/internal/handler/v2/individual"
apiKeyHandler "github.com/bb-consent/api/internal/handler/v2/config/apikey"
dataAgreementHandler "github.com/bb-consent/api/internal/handler/v2/config/dataagreement"
dataAttributeHandler "github.com/bb-consent/api/internal/handler/v2/config/dataattribute"
idpHandler "github.com/bb-consent/api/internal/handler/v2/config/idp"
configIndividualHandler "github.com/bb-consent/api/internal/handler/v2/config/individual"
policyHandler "github.com/bb-consent/api/internal/handler/v2/config/policy"
privacyDashboardHandler "github.com/bb-consent/api/internal/handler/v2/config/privacy_dashboard"
webhookHandler "github.com/bb-consent/api/internal/handler/v2/config/webhook"
onboardHandler "github.com/bb-consent/api/internal/handler/v2/onboard"
policyHandler "github.com/bb-consent/api/internal/handler/v2/policy"
privacyDashboardHandler "github.com/bb-consent/api/internal/handler/v2/privacy_dashboard"
serviceHandler "github.com/bb-consent/api/internal/handler/v2/service"
webhookHandler "github.com/bb-consent/api/internal/handler/v2/webhook"
serviceIndividualHandler "github.com/bb-consent/api/internal/handler/v2/service/individual"
m "github.com/bb-consent/api/internal/middleware"
"github.com/casbin/casbin/v2"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -61,18 +62,18 @@ func SetRoutes(r *mux.Router, e *casbin.Enforcer) {
r.Handle(ConfigListIdentityProviders, m.Chain(idpHandler.ConfigListIdps, m.Logger(), m.Authorize(e), m.Authenticate(), m.AddContentType())).Methods("GET")

// Individual related api(s)
r.Handle(ConfigReadIndividual, m.Chain(individualHandler.ConfigReadIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET")
r.Handle(ConfigCreateIndividual, m.Chain(individualHandler.ConfigCreateIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("POST")
r.Handle(ConfigUpdateIndividual, m.Chain(individualHandler.ConfigUpdateIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("PUT")
r.Handle(ConfigListIndividuals, m.Chain(individualHandler.ConfigListIndividuals, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET")
r.Handle(ConfigReadIndividual, m.Chain(configIndividualHandler.ConfigReadIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET")
r.Handle(ConfigCreateIndividual, m.Chain(configIndividualHandler.ConfigCreateIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("POST")
r.Handle(ConfigUpdateIndividual, m.Chain(configIndividualHandler.ConfigUpdateIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("PUT")
r.Handle(ConfigListIndividuals, m.Chain(configIndividualHandler.ConfigListIndividuals, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET")

// Api key related api(s)
r.Handle(ConfigCreateApiKey, m.Chain(apiKeyHandler.ConfigCreateApiKey, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("POST")
r.Handle(ConfigDeleteApiKey, m.Chain(apiKeyHandler.ConfigDeleteApiKey, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("DELETE")
r.Handle(ConfigUpdateApiKey, m.Chain(apiKeyHandler.ConfigUpdateApiKey, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("PUT")
r.Handle(ConfigListApiKey, m.Chain(apiKeyHandler.ConfigListApiKey, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET")

r.Handle(ConfigCreateIndividualsInBulk, m.Chain(individualHandler.ConfigCreateIndividualsInBulk, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("POST")
r.Handle(ConfigCreateIndividualsInBulk, m.Chain(configIndividualHandler.ConfigCreateIndividualsInBulk, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("POST")

r.Handle(ConfigReadPrivacyDashboard, m.Chain(privacyDashboardHandler.ConfigReadPrivacyDashboard, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET")

Expand Down Expand Up @@ -117,10 +118,10 @@ func SetRoutes(r *mux.Router, e *casbin.Enforcer) {
r.Handle(ServiceReadOrganisationCoverImage, m.Chain(serviceHandler.ServiceReadOrganisationCoverImage, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET")

// Individual related api(s)
r.Handle(ServiceReadIndividual, m.Chain(individualHandler.ServiceReadIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET")
r.Handle(ServiceCreateIndividual, m.Chain(individualHandler.ServiceCreateIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("POST")
r.Handle(ServiceUpdateIndividual, m.Chain(individualHandler.ServiceUpdateIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("PUT")
r.Handle(ServiceListIndividuals, m.Chain(individualHandler.ServiceListIndividuals, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET")
r.Handle(ServiceReadIndividual, m.Chain(serviceIndividualHandler.ServiceReadIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET")
r.Handle(ServiceCreateIndividual, m.Chain(serviceIndividualHandler.ServiceCreateIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("POST")
r.Handle(ServiceUpdateIndividual, m.Chain(serviceIndividualHandler.ServiceUpdateIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("PUT")
r.Handle(ServiceListIndividuals, m.Chain(serviceIndividualHandler.ServiceListIndividuals, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET")

// Audit api(s)

Expand Down

0 comments on commit 1963ab4

Please sign in to comment.