Skip to content

Commit

Permalink
support org level policies
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes committed Jan 11, 2024
1 parent b5931e5 commit 1c7deec
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 49 deletions.
25 changes: 2 additions & 23 deletions backend/controllers/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,29 +151,8 @@ func upsertPolicyForOrg(c *gin.Context, policyType string) {
return
}

policy := models.Policy{}

policyResult := models.DB.GormDB.Where("organisation_id = ? AND (repo_id IS NULL AND project_id IS NULL) AND type = ?", org.ID, policyType).Take(&policy)

if policyResult.RowsAffected == 0 {
err := models.DB.GormDB.Create(&models.Policy{
OrganisationID: org.ID,
Type: policyType,
Policy: string(policyData),
}).Error

if err != nil {
log.Printf("Error creating policy: %v", err)
c.String(http.StatusInternalServerError, "Error creating policy")
return
}
} else {
err := policyResult.Update("policy", string(policyData)).Error
if err != nil {
log.Printf("Error updating policy: %v", err)
c.String(http.StatusInternalServerError, "Error updating policy")
return
}
if err = models.DB.UpsertPolicyForOrg(policyType, org, string(policyData)); err != nil {
c.String(http.StatusInternalServerError, "Error creating policy for organisation: %v", org)
}

c.JSON(http.StatusOK, gin.H{"success": true})
Expand Down
62 changes: 37 additions & 25 deletions backend/controllers/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ func (web *WebController) PoliciesPage(c *gin.Context) {
}

func (web *WebController) AddPolicyPage(c *gin.Context) {
organisationId, exists := c.Get(middleware.ORGANISATION_ID_KEY)
if !exists {
c.String(http.StatusForbidden, "Not allowed to access this resource")
return
}

if c.Request.Method == "GET" {
message := ""
projects, done := models.DB.GetProjectsFromContext(c, middleware.ORGANISATION_ID_KEY)
Expand Down Expand Up @@ -137,32 +143,38 @@ func (web *WebController) AddPolicyPage(c *gin.Context) {

policyType := c.PostForm("policytype")
projectIdStr := c.PostForm("projectid")
projectId64, err := strconv.ParseUint(projectIdStr, 10, 32)
if err != nil {
c.String(http.StatusInternalServerError, "Failed to parse policy id")
return
}
projectId := uint(projectId64)
project, ok := models.DB.GetProjectByProjectId(c, projectId, middleware.ORGANISATION_ID_KEY)
if !ok {
log.Printf("Failed to fetch specified project by id: %v, %v\n", projectIdStr, err)
message := "Failed to create a policy"
services.AddError(c, message)
pageContext := services.GetMessages(c)
c.HTML(http.StatusOK, "policy_add.tmpl", pageContext)
}

log.Printf("repo: %v\n", project.Repo)

policy := models.Policy{Project: project, Policy: policyText, Type: policyType, Organisation: project.Organisation, Repo: project.Repo}
if projectIdStr != "" {
projectId64, err := strconv.ParseUint(projectIdStr, 10, 32)
if err != nil {
c.String(http.StatusInternalServerError, "Failed to parse project id")
return
}
projectIdPtr := uint(projectId64)
projectId := &projectIdPtr
project, ok := models.DB.GetProjectByProjectId(c, *projectId, middleware.ORGANISATION_ID_KEY)
if !ok {
log.Printf("Failed to fetch specified project by id: %v, %v\n", projectIdStr, err)
message := "Failed to create a policy"
services.AddError(c, message)
pageContext := services.GetMessages(c)
c.HTML(http.StatusOK, "policy_add.tmpl", pageContext)
}
log.Printf("repo: %v\n", project.Repo)
policy := models.Policy{ProjectID: projectId, Policy: policyText, Type: policyType, Organisation: project.Organisation, Repo: project.Repo}
err = models.DB.GormDB.Create(&policy).Error
if err != nil {
log.Printf("Failed to create a new policy, %v\n", err)
message := "Failed to create a policy"
services.AddError(c, message)
pageContext := services.GetMessages(c)
c.HTML(http.StatusOK, "policy_add.tmpl", pageContext)
}

err = models.DB.GormDB.Create(&policy).Error
if err != nil {
log.Printf("Failed to create a new policy, %v\n", err)
message := "Failed to create a policy"
services.AddError(c, message)
pageContext := services.GetMessages(c)
c.HTML(http.StatusOK, "policy_add.tmpl", pageContext)
} else {
org, err := models.DB.GetOrganisationById(organisationId)
if err = models.DB.UpsertPolicyForOrg(policyType, *org, policyText); err != nil {
c.String(http.StatusInternalServerError, "Error creating policy for organisation: %v", org)
}
}

c.Redirect(http.StatusFound, "/policies")
Expand Down
1 change: 0 additions & 1 deletion backend/models/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ var DEFAULT_ORG_NAME = "digger"
var DB *Database

func ConnectDatabase() {

database, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL")), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})
Expand Down
26 changes: 26 additions & 0 deletions backend/models/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,32 @@ func (db *Database) GetReposFromContext(c *gin.Context, orgIdKey string) ([]Repo
return repos, true
}

func (db *Database) UpsertPolicyForOrg(policyType string, org Organisation, policyContent string) error {
policy := Policy{}

policyResult := db.GormDB.Where("organisation_id = ? AND (repo_id IS NULL AND project_id IS NULL) AND type = ?", org.ID, policyType).Take(&policy)

if policyResult.RowsAffected == 0 {
err := db.GormDB.Create(&Policy{
OrganisationID: org.ID,
Type: policyType,
Policy: policyContent,
}).Error

if err != nil {
log.Printf("Error creating policy: %v", err)
return fmt.Errorf("error creating policy: %v", err)
}
} else {
err := policyResult.Update("policy", policyContent).Error
if err != nil {
log.Printf("Error updating policy: %v", err)
return fmt.Errorf("error updating policy: %v", err)
}
}
return nil
}

func (db *Database) GetPoliciesFromContext(c *gin.Context, orgIdKey string) ([]Policy, bool) {
loggedInOrganisationId, exists := c.Get(orgIdKey)

Expand Down
4 changes: 4 additions & 0 deletions backend/templates/policy_add.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

{{template "notifications" . }}

<div class="alert alert-info" role="alert">
Leave project field empty to add organisation level policies
</div>

<form method="POST">
<div class="row">
<div class="col">
Expand Down

0 comments on commit 1c7deec

Please sign in to comment.