Skip to content

Commit

Permalink
fixed the role and policy update issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vsumit89 committed Aug 31, 2022
1 parent 66e36c7 commit 703b701
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 24 deletions.
3 changes: 2 additions & 1 deletion server/action/organisation/application/policy/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"github.com/factly/x/slugx"
"github.com/go-chi/chi"
)

Expand Down Expand Up @@ -90,7 +91,7 @@ func create(w http.ResponseWriter, r *http.Request) {
var policy model.ApplicationPolicy
policy.CreatedByID = uint(userID)
policy.Name = reqBody.Name
policy.Slug = reqBody.Slug
policy.Slug = slugx.Make(reqBody.Name)
policy.Description = reqBody.Description
policy.ApplicationID = uint(appID)
policy.Permissions = reqBody.Permissions
Expand Down
22 changes: 20 additions & 2 deletions server/action/organisation/application/policy/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"github.com/factly/x/slugx"
"github.com/go-chi/chi"
)

Expand Down Expand Up @@ -82,8 +83,26 @@ func update(w http.ResponseWriter, r *http.Request) {
policy.Description = reqBody.Description
policy.ApplicationID = uint(appID)
policy.Permissions = reqBody.Permissions
policy.Slug = reqBody.Slug
policy.Slug = slugx.Make(reqBody.Name)
policy.OrganisationID = uint(orgID)
tx := model.DB.Begin()
var count int64
err = tx.Model(&model.ApplicationPolicy{}).Where(&model.ApplicationPolicy{
ApplicationID: uint(appID),
Slug: policy.Slug,
}).Count(&count).Error
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
if count >= 1 {
tx.Rollback()
loggerx.Error(errors.New("slug already exists"))
errorx.Render(w, errorx.Parser(errorx.SameNameExist()))
return
}
roles := make([]model.ApplicationRole, 0)
for _, each := range reqBody.Roles {
appRole := model.ApplicationRole{}
Expand All @@ -103,7 +122,6 @@ func update(w http.ResponseWriter, r *http.Request) {
policy.Roles = roles

// updating the application policy on the kavachDB
tx := model.DB.Begin()
err = tx.Model(&model.ApplicationPolicy{}).Where("id = ?", policyID).Updates(&policy).Error
if err != nil {
tx.Rollback()
Expand Down
17 changes: 17 additions & 0 deletions server/action/organisation/application/roles/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,25 @@ func update(w http.ResponseWriter, r *http.Request) {
return
}

var count int64
err = model.DB.Model(&model.ApplicationRole{}).Where(&model.ApplicationRole{
ApplicationID: uint(appID),
Slug: appRole.Slug,
}).Count(&count).Error
if err != nil || count > 0 {
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
} else {
loggerx.Error(errors.New("slug already exists"))
errorx.Render(w, errorx.Parser(errorx.SameNameExist()))
}
return
}

updateMap := map[string]interface{}{
"name": appRole.Name,
"slug": appRole.Slug,
"description": appRole.Description,
}
//update the application role
Expand Down
21 changes: 11 additions & 10 deletions server/action/organisation/application/space/policy/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"github.com/factly/x/slugx"
"github.com/go-chi/chi"
)

Expand Down Expand Up @@ -100,7 +101,7 @@ func create(w http.ResponseWriter, r *http.Request) {
// binding the policyReq to SpacePolicy model
var policy model.SpacePolicy
policy.CreatedByID = uint(userID)
policy.Slug = reqBody.Slug
policy.Slug = slugx.Make(reqBody.Name)
policy.Name = reqBody.Name
policy.Description = reqBody.Description
policy.SpaceID = uint(spaceID)
Expand All @@ -113,21 +114,21 @@ func create(w http.ResponseWriter, r *http.Request) {
policy.Roles = roles
// inserting space role to the kavachDB
tx := model.DB.Begin()

var count int64
err = tx.Model(&model.SpacePolicy{}).Where(&model.SpacePolicy{
SpaceID: uint(spaceID),
Slug: policy.Slug,
}).Count(&count).Error
if err != nil || count > 0 {
if err != nil {
tx.Rollback()
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
} else {
loggerx.Error(errors.New("slug already exists"))
errorx.Render(w, errorx.Parser(errorx.SameNameExist()))
}
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
if count >= 1 {
tx.Rollback()
loggerx.Error(errors.New("slug already exists"))
errorx.Render(w, errorx.Parser(errorx.SameNameExist()))
return
}

Expand Down
22 changes: 20 additions & 2 deletions server/action/organisation/application/space/policy/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"github.com/factly/x/slugx"
"github.com/go-chi/chi"
)

Expand Down Expand Up @@ -100,9 +101,27 @@ func update(w http.ResponseWriter, r *http.Request) {
policy.ID = uint(policyID)
policy.Name = reqBody.Name
policy.Description = reqBody.Description
policy.Slug = reqBody.Slug
policy.Slug = slugx.Make(reqBody.Name)
policy.SpaceID = uint(spaceID)
policy.Permissions = reqBody.Permissions
tx := model.DB.Begin()
var count int64
err = tx.Model(&model.SpacePolicy{}).Where(&model.SpacePolicy{
SpaceID: uint(spaceID),
Slug: policy.Slug,
}).Count(&count).Error
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
if count >= 1 {
tx.Rollback()
loggerx.Error(errors.New("slug already exists"))
errorx.Render(w, errorx.Parser(errorx.SameNameExist()))
return
}
roles := make([]model.SpaceRole, 0)
for _, each := range reqBody.Roles {
spaceRole := model.SpaceRole{}
Expand All @@ -122,7 +141,6 @@ func update(w http.ResponseWriter, r *http.Request) {
policy.Roles = roles

// updating the policy in the kavachDB
tx := model.DB.Begin()
err = tx.Model(&model.SpacePolicy{}).Where("id = ?", policyID).Updates(&policy).Error
if err != nil {
tx.Rollback()
Expand Down
22 changes: 20 additions & 2 deletions server/action/organisation/application/space/roles/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package roles

import (
"encoding/json"
"errors"
"net/http"
"strconv"

Expand Down Expand Up @@ -94,10 +95,27 @@ func update(w http.ResponseWriter, r *http.Request) {
return
}

var count int64
err = model.DB.Model(&model.SpaceRole{}).Where(&model.SpaceRole{
SpaceID: uint(spaceID),
Slug: spaceRole.Slug,
}).Count(&count).Error
if err != nil || count > 0 {
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
} else {
loggerx.Error(errors.New("slug already exists"))
errorx.Render(w, errorx.Parser(errorx.SameNameExist()))
}
return
}

updateMap := map[string]interface{}{
"name": spaceRole.Name,
"name": spaceRole.Name,
"slug": spaceRole.Slug,
"description": spaceRole.Description,
}
}

//update the application role
err = model.DB.Model(&model.SpaceRole{}).Where("space_id = ? AND id = ?", spaceID, roleIDInt).Updates(updateMap).Error
Expand Down
3 changes: 2 additions & 1 deletion server/action/organisation/policy/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"github.com/factly/x/slugx"
"github.com/go-chi/chi"
)

Expand Down Expand Up @@ -62,7 +63,7 @@ func create(w http.ResponseWriter, r *http.Request) {
// binding policyReq to the model.OrganisationPolicy datamodel
var policy model.OrganisationPolicy
policy.CreatedByID = uint(userID)
policy.Slug = reqBody.Slug
policy.Slug = slugx.Make(reqBody.Name)
policy.Name = reqBody.Name
policy.Description = reqBody.Description
policy.OrganisationID = uint(orgID)
Expand Down
24 changes: 22 additions & 2 deletions server/action/organisation/policy/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package policy

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"github.com/factly/x/slugx"
"github.com/go-chi/chi"
)

Expand Down Expand Up @@ -72,10 +74,28 @@ func update(w http.ResponseWriter, r *http.Request) {
policy := new(model.OrganisationPolicy)
policy.ID = uint(policyID)
policy.Name = reqBody.Name
policy.Slug = reqBody.Slug
policy.Slug = slugx.Make(reqBody.Name)
policy.Description = reqBody.Description
policy.OrganisationID = uint(orgID)
policy.Permissions = reqBody.Permissions
tx := model.DB.Begin()
var count int64
err = tx.Model(&model.OrganisationPolicy{}).Where(&model.OrganisationPolicy{
OrganisationID: uint(orgID),
Slug: policy.Slug,
}).Count(&count).Error
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
if count >= 1 {
tx.Rollback()
loggerx.Error(errors.New("slug already exists"))
errorx.Render(w, errorx.Parser(errorx.SameNameExist()))
return
}
roles := make([]model.OrganisationRole, 0)
for _, each := range reqBody.Roles {
organisationRole := model.OrganisationRole{}
Expand All @@ -93,7 +113,7 @@ func update(w http.ResponseWriter, r *http.Request) {
}

policy.Roles = roles
tx := model.DB.Begin()

err = model.DB.Model(&model.OrganisationPolicy{}).Where("id = ?", policyID).Updates(policy).Error
if err != nil {
loggerx.Error(err)
Expand Down
23 changes: 20 additions & 3 deletions server/action/organisation/roles/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package roles

import (
"encoding/json"
"errors"
"net/http"
"strconv"

Expand Down Expand Up @@ -73,11 +74,27 @@ func update(w http.ResponseWriter, r *http.Request) {
return
}

var count int64
err = model.DB.Model(&model.OrganisationRole{}).Where(&model.OrganisationRole{
OrganisationID: uint(orgID),
Slug: organisationRole.Slug,
}).Count(&count).Error
if err != nil || count > 0 {
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
} else {
loggerx.Error(errors.New("slug already exists"))
errorx.Render(w, errorx.Parser(errorx.SameNameExist()))
}
return
}

updateMap := map[string]interface{}{
"name": organisationRole.Name,
"name": organisationRole.Name,
"slug": organisationRole.Slug,
"description": organisationRole.Description,
}

}
//update the organisation role
err = model.DB.Model(&model.OrganisationRole{}).Where("organisation_id = ? AND id = ?", orgID, roleIDInt).Updates(updateMap).Error
if err != nil {
Expand Down
13 changes: 12 additions & 1 deletion server/action/user/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,23 @@ func checker(w http.ResponseWriter, r *http.Request) {
if err != nil {
user.IsActive = true
// record does not exist so create new user
err = model.DB.Create(&user).Error
var count int64
err = model.DB.Model(&model.User{}).Where(&model.User{
Email: user.Email,
}).Count(&count).Error
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
if count == 0 {
err = model.DB.Create(&user).Error
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
}
}

payload.Header = make(http.Header)
Expand Down

0 comments on commit 703b701

Please sign in to comment.