Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
stillasTracker
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Aleksander Aaboen
stillasTracker
Commits
39f03eec
Commit
39f03eec
authored
Apr 8, 2022
by
Martin Iversen
Browse files
Options
Downloads
Patches
Plain Diff
Updated GetQueryScaffolding
parent
58421a79
No related branches found
No related tags found
1 merge request
!53
Martin
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
api/apiTools/basicTools.go
+49
-10
49 additions, 10 deletions
api/apiTools/basicTools.go
api/endpoints/profiles.go
+4
-4
4 additions, 4 deletions
api/endpoints/profiles.go
api/endpoints/scaffolding.go
+1
-2
1 addition, 2 deletions
api/endpoints/scaffolding.go
with
54 additions
and
16 deletions
api/apiTools/basicTools.go
+
49
−
10
View file @
39f03eec
...
@@ -3,8 +3,8 @@ package apiTools
...
@@ -3,8 +3,8 @@ package apiTools
import
(
import
(
"encoding/json"
"encoding/json"
"errors"
"errors"
"github.com/gorilla/mux"
"net/http"
"net/http"
"net/url"
"stillasTracker/api/constants"
"stillasTracker/api/constants"
"strings"
"strings"
)
)
...
@@ -18,30 +18,69 @@ func CreatePath(segments []string) string {
...
@@ -18,30 +18,69 @@ func CreatePath(segments []string) string {
return
finalPath
return
finalPath
}
}
/*
GetQueryScaffolding function checks that the queries are valid in the scaffolding requests
Code inspired by the following stackoverflow issue:
//https://stackoverflow.com/questions/59570978/is-there-a-way-to-check-for-invalid-query-parameters-in-an-http-request
//https://stackoverflow.com/questions/59570978/is-there-a-way-to-check-for-invalid-query-parameters-in-an-http-request
func
GetQueryProject
(
r
*
http
.
Request
)
(
url
.
Values
,
bool
)
{
*/
query
:=
r
.
URL
.
Query
()
func
GetQueryScaffolding
(
r
*
http
.
Request
)
(
map
[
string
]
string
,
bool
)
{
query
:=
mux
.
Vars
(
r
)
allowedQuery
:=
map
[
string
]
bool
{
constants
.
P
_id
URL
:
true
,
constants
.
P_nameURL
:
true
,
constants
.
P_scaffolding
:
true
,
constants
.
P_Stat
e
:
true
}
allowedQuery
:=
map
[
string
]
bool
{
constants
.
S
_id
:
true
,
constants
.
S_typ
e
:
true
}
for
k
:=
range
query
{
for
k
:=
range
query
{
if
_
,
ok
:=
allowedQuery
[
k
];
!
ok
{
if
_
,
ok
:=
allowedQuery
[
k
];
!
ok
{
return
nil
,
false
return
nil
,
false
}
}
}
}
valid
:=
true
if
query
[
constants
.
S_type
]
!=
""
{
for
i
:=
range
constants
.
ScaffoldingTypes
{
if
!
(
query
[
constants
.
S_type
]
==
constants
.
ScaffoldingTypes
[
i
])
{
valid
=
false
}
else
{
valid
=
true
break
}
}
if
valid
==
false
{
return
nil
,
valid
}
}
return
query
,
true
}
/*
GetQueryProject function checks that the queries are valid in the project requests
Code inspired by the following stackoverflow issue:
//https://stackoverflow.com/questions/59570978/is-there-a-way-to-check-for-invalid-query-parameters-in-an-http-request
*/
func
GetQueryProject
(
r
*
http
.
Request
)
(
map
[
string
]
string
,
bool
)
{
query
:=
mux
.
Vars
(
r
)
allowedQuery
:=
map
[
string
]
bool
{
constants
.
P_idURL
:
true
,
constants
.
P_nameURL
:
true
,
constants
.
P_scaffolding
:
true
,
constants
.
P_State
:
true
}
if
query
.
Has
(
constants
.
P_scaffolding
)
{
for
k
:=
range
query
{
if
!
(
query
.
Get
(
constants
.
P_scaffolding
)
==
"true"
||
query
.
Get
(
constants
.
P_scaffolding
)
==
"false"
)
{
if
_
,
ok
:=
allowedQuery
[
k
];
!
ok
{
return
nil
,
false
return
nil
,
false
}
}
}
}
if
query
[
constants
.
P_scaffolding
]
!=
""
{
if
!
(
query
[
constants
.
P_scaffolding
]
==
"true"
||
query
[
constants
.
P_scaffolding
]
==
"false"
)
{
return
nil
,
false
}
}
return
query
,
true
return
query
,
true
}
}
/*
GetQueryProfile function checks that the queries are valid in the profile requests
Code inspired by the following stackoverflow issue:
//https://stackoverflow.com/questions/59570978/is-there-a-way-to-check-for-invalid-query-parameters-in-an-http-request
//https://stackoverflow.com/questions/59570978/is-there-a-way-to-check-for-invalid-query-parameters-in-an-http-request
func
GetQueryProfile
(
r
*
http
.
Request
)
(
url
.
Values
,
bool
)
{
*/
query
:=
r
.
URL
.
Query
()
func
GetQueryProfile
(
r
*
http
.
Request
)
(
map
[
string
]
string
,
bool
)
{
query
:=
mux
.
Vars
(
r
)
//Defines the allowed parts of the url
//Defines the allowed parts of the url
allowedQuery
:=
map
[
string
]
bool
{
constants
.
U_nameURL
:
true
,
constants
.
U_Role
:
true
,
constants
.
U_idURL
:
true
}
allowedQuery
:=
map
[
string
]
bool
{
constants
.
U_nameURL
:
true
,
constants
.
U_Role
:
true
,
constants
.
U_idURL
:
true
}
...
@@ -53,8 +92,8 @@ func GetQueryProfile(r *http.Request) (url.Values, bool) {
...
@@ -53,8 +92,8 @@ func GetQueryProfile(r *http.Request) (url.Values, bool) {
}
}
//Checks that the URL only contains the allowed roles
//Checks that the URL only contains the allowed roles
if
query
.
Has
(
constants
.
U_Role
)
{
if
query
[
constants
.
U_Role
]
!=
""
{
if
!
(
query
.
Get
(
constants
.
U_Role
)
==
constants
.
U_admin
||
query
.
Get
(
constants
.
U_Role
)
==
strings
.
ToLower
(
constants
.
U_Installer
)
||
query
.
Get
(
constants
.
U_Role
)
==
strings
.
ToLower
(
constants
.
U_Storage
))
{
if
!
(
query
[
constants
.
U_Role
]
==
constants
.
U_admin
||
query
[
constants
.
U_Role
]
==
strings
.
ToLower
(
constants
.
U_Installer
)
||
query
[
constants
.
U_Role
]
==
strings
.
ToLower
(
constants
.
U_Storage
))
{
return
nil
,
false
return
nil
,
false
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
api/endpoints/profiles.go
+
4
−
4
View file @
39f03eec
...
@@ -73,9 +73,9 @@ func getProfile(w http.ResponseWriter, r *http.Request) {
...
@@ -73,9 +73,9 @@ func getProfile(w http.ResponseWriter, r *http.Request) {
}
}
switch
true
{
//Forwards the request to the appropriate function based on the passed in query
switch
true
{
//Forwards the request to the appropriate function based on the passed in query
case
query
.
Has
(
constants
.
U_Role
)
:
case
query
[
constants
.
U_Role
]
!=
""
:
getUsersByRole
(
w
,
r
)
getUsersByRole
(
w
,
r
)
case
query
.
Has
(
constants
.
U_idURL
)
||
query
.
Has
(
constants
.
U_nameURL
)
:
case
query
[
constants
.
U_idURL
]
!=
""
||
query
[
constants
.
U_nameURL
]
!=
""
:
getIndividualUser
(
w
,
r
)
getIndividualUser
(
w
,
r
)
default
:
default
:
getAll
(
w
)
getAll
(
w
)
...
@@ -323,9 +323,9 @@ func getIndividualUser(w http.ResponseWriter, r *http.Request) {
...
@@ -323,9 +323,9 @@ func getIndividualUser(w http.ResponseWriter, r *http.Request) {
return
return
}
}
switch
true
{
//Forwards the request based on the passed in constant
switch
true
{
//Forwards the request based on the passed in constant
case
query
.
Has
(
constants
.
U_name
)
:
case
query
[
constants
.
U_name
]
!=
""
:
getUserByName
(
w
,
r
)
getUserByName
(
w
,
r
)
case
query
.
Has
(
constants
.
U_idURL
)
:
case
query
[
constants
.
U_idURL
]
!=
""
:
getIndividualUserByID
(
w
,
r
)
getIndividualUserByID
(
w
,
r
)
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
api/endpoints/scaffolding.go
+
1
−
2
View file @
39f03eec
...
@@ -2,7 +2,6 @@ package endpoints
...
@@ -2,7 +2,6 @@ package endpoints
import
(
import
(
"encoding/json"
"encoding/json"
"github.com/gorilla/mux"
"google.golang.org/api/iterator"
"google.golang.org/api/iterator"
"net/http"
"net/http"
tool
"stillasTracker/api/apiTools"
tool
"stillasTracker/api/apiTools"
...
@@ -48,7 +47,7 @@ a user can search based on projects, id or type
...
@@ -48,7 +47,7 @@ a user can search based on projects, id or type
func
getPart
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
getPart
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Origin"
,
"*"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Origin"
,
"*"
)
queries
:=
mux
.
Vars
(
r
)
queries
,
_
:=
tool
.
GetQueryScaffolding
(
r
)
switch
true
{
switch
true
{
case
queries
[
"type"
]
!=
""
&&
queries
[
"id"
]
!=
""
:
//URL is on the following format: /stillastracking/v1/api/unit?type=""&id=""
case
queries
[
"type"
]
!=
""
&&
queries
[
"id"
]
!=
""
:
//URL is on the following format: /stillastracking/v1/api/unit?type=""&id=""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment