Skip to content

Commit

Permalink
code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
stremovsky committed Aug 13, 2024
1 parent cd5794e commit a6c1f5e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 31 deletions.
3 changes: 1 addition & 2 deletions src/audit_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func (e mainEnv) getAuditEvents(w http.ResponseWriter, r *http.Request, ps httpr
}

func (e mainEnv) getAdminAuditEvents(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
var offset int32
Expand Down
9 changes: 3 additions & 6 deletions src/lbasis_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (

func (e mainEnv) createLegalBasis(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
brief := ps.ByName("brief")
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
brief = normalizeBrief(brief)
Expand Down Expand Up @@ -83,8 +82,7 @@ func (e mainEnv) createLegalBasis(w http.ResponseWriter, r *http.Request, ps htt

func (e mainEnv) deleteLegalBasis(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
brief := ps.ByName("brief")
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
brief = normalizeBrief(brief)
Expand All @@ -100,8 +98,7 @@ func (e mainEnv) deleteLegalBasis(w http.ResponseWriter, r *http.Request, ps htt
}

func (e mainEnv) listLegalBasisRecords(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
resultJSON, numRecords, err := e.db.getLegalBasisRecords()
Expand Down
15 changes: 5 additions & 10 deletions src/pactivities_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (

func (e mainEnv) pactivityCreate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
activity := ps.ByName("activity")
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
activity = normalizeBrief(activity)
Expand Down Expand Up @@ -71,8 +70,7 @@ func (e mainEnv) pactivityCreate(w http.ResponseWriter, r *http.Request, ps http

func (e mainEnv) pactivityDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
activity := ps.ByName("activity")
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
activity = normalizeBrief(activity)
Expand All @@ -89,8 +87,7 @@ func (e mainEnv) pactivityDelete(w http.ResponseWriter, r *http.Request, ps http
func (e mainEnv) pactivityLink(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
activity := ps.ByName("activity")
brief := ps.ByName("brief")
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
activity = normalizeBrief(activity)
Expand Down Expand Up @@ -125,8 +122,7 @@ func (e mainEnv) pactivityLink(w http.ResponseWriter, r *http.Request, ps httpro
func (e mainEnv) pactivityUnlink(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
activity := ps.ByName("activity")
brief := ps.ByName("brief")
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
activity = normalizeBrief(activity)
Expand All @@ -150,8 +146,7 @@ func (e mainEnv) pactivityUnlink(w http.ResponseWriter, r *http.Request, ps http
}

func (e mainEnv) pactivityList(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
resultJSON, numRecords, err := e.db.listProcessingActivities()
Expand Down
3 changes: 1 addition & 2 deletions src/requests_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ func (e mainEnv) getUserRequest(w http.ResponseWriter, r *http.Request, ps httpr
if len(userTOKEN) != 0 {
event.Record = userTOKEN
}
authResult := e.enforceAuth(w, r, event)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
change := getStringValue(requestInfo["change"])
Expand Down
14 changes: 5 additions & 9 deletions src/sessions_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"

uuid "github.com/hashicorp/go-uuid"
"github.com/julienschmidt/httprouter"
"github.com/securitybunker/databunker/src/storage"
"go.mongodb.org/mongo-driver/bson"
"net/http"
"strings"
)

func (e mainEnv) createSession(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Expand All @@ -23,8 +24,7 @@ func (e mainEnv) createSession(w http.ResponseWriter, r *http.Request, ps httpro
//returnError(w, r, "bad session format", nil, event)
return
}
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
expiration := e.conf.Policy.MaxSessionRetentionPeriod
Expand Down Expand Up @@ -77,8 +77,7 @@ func (e mainEnv) deleteSession(w http.ResponseWriter, r *http.Request, ps httpro
//returnError(w, r, "bad session format", nil, event)
return
}
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
e.db.deleteSession(session)
Expand Down Expand Up @@ -147,7 +146,6 @@ func (e mainEnv) newUserSession(w http.ResponseWriter, r *http.Request, ps httpr
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
fmt.Fprintf(w, `{"status":"ok","session":"%s"}`, sessionID)
return
}

func (e mainEnv) getUserSessions(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Expand Down Expand Up @@ -201,7 +199,6 @@ func (e mainEnv) getUserSessions(w http.ResponseWriter, r *http.Request, ps http
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
fmt.Fprintf(w, `{"status":"ok","total":%d,"rows":[%s]}`, count, data)
return
}

func (e mainEnv) getSession(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Expand All @@ -227,5 +224,4 @@ func (e mainEnv) getSession(w http.ResponseWriter, r *http.Request, ps httproute
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
fmt.Fprintf(w, `{"status":"ok","session":"%s","when":%d,"data":%s}`, session, when, record)
return
}
4 changes: 2 additions & 2 deletions src/users_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (e mainEnv) userGet(w http.ResponseWriter, r *http.Request, ps httprouter.P
mode := ps.ByName("mode")
event := audit("get user record by "+mode, identity, mode, identity)
defer func() { event.submit(e.db, e.conf) }()

if validateMode(mode) == false {
returnError(w, r, "bad mode", 405, nil, event)
return
Expand Down Expand Up @@ -165,8 +166,7 @@ func (e mainEnv) userGet(w http.ResponseWriter, r *http.Request, ps httprouter.P
}

func (e mainEnv) userList(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
authResult := e.enforceAdmin(w, r)
if authResult == "" {
if e.enforceAdmin(w, r) == "" {
return
}
if e.conf.Generic.ListUsers == false {
Expand Down

0 comments on commit a6c1f5e

Please sign in to comment.