Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit f36c016

Browse files
committed
webui: Move code for getting database owner and name to own function
So far the code for getting the database owner and name was included in the function for getting page meta information. Move that code to a separate function that is only called for pages dealing with databases. This makes it more obvious what is happening. Note that in the new function, unlike before, permissions are no longer checked. It only handles data retrieval. This is however ok because in all cases where it is used permissions are checked later anyway, usually by calling com.CheckDBPermissions or com.DBDetails.
1 parent 9a5d9ff commit f36c016

File tree

5 files changed

+153
-77
lines changed

5 files changed

+153
-77
lines changed

webui/main.go

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,33 @@ func checkLogin(r *http.Request) (loggedInUser string, validSession bool, err er
379379
return
380380
}
381381

382-
func collectPageMetaInfo(r *http.Request, pageMeta *PageMetaInfo, meta *com.MetaInfo, getOwnerAndDatabaseFromUrl bool) (errCode int, err error) {
382+
func collectDatabaseInfo(r *http.Request, meta *com.MetaInfo) (errCode int, err error) {
383+
// TODO: Add folder and branch name support
384+
dbOwner, dbName, err := com.GetOD(1, r) // 1 = Ignore "/xxx/" at the start of the URL
385+
if err != nil {
386+
return http.StatusBadRequest, err
387+
}
388+
389+
// Validate the supplied information
390+
if dbOwner == "" || dbName == "" {
391+
return http.StatusBadRequest, fmt.Errorf("Missing database owner or database name")
392+
}
393+
394+
// Retrieve correctly capitalised username for the database owner
395+
usr, err := com.User(dbOwner)
396+
if err != nil {
397+
return http.StatusBadRequest, err
398+
}
399+
400+
// Store information
401+
meta.Database = dbName
402+
meta.Owner = usr.Username
403+
meta.Folder = "/"
404+
405+
return
406+
}
407+
408+
func collectPageMetaInfo(r *http.Request, pageMeta *PageMetaInfo) (errCode int, err error) {
383409
// Auth0 info
384410
pageMeta.Auth0.CallbackURL = "https://" + com.Conf.Web.ServerName + "/x/callback"
385411
pageMeta.Auth0.ClientID = com.Conf.Auth0.ClientID
@@ -415,40 +441,6 @@ func collectPageMetaInfo(r *http.Request, pageMeta *PageMetaInfo, meta *com.Meta
415441
}
416442
}
417443

418-
// Retrieve the database owner & name
419-
if getOwnerAndDatabaseFromUrl {
420-
// TODO: Add folder and branch name support
421-
dbOwner, dbName, err := com.GetOD(1, r) // 1 = Ignore "/xxx/" at the start of the URL
422-
if err != nil {
423-
return http.StatusBadRequest, err
424-
}
425-
426-
// Validate the supplied information
427-
if dbOwner == "" || dbName == "" {
428-
return http.StatusBadRequest, fmt.Errorf("Missing database owner or database name")
429-
}
430-
431-
// Check if the database exists
432-
exists, err := com.CheckDBPermissions(loggedInUser, dbOwner, "/", dbName, false)
433-
if err != nil {
434-
return http.StatusInternalServerError, fmt.Errorf("Database failure when looking up database details")
435-
}
436-
if !exists {
437-
return http.StatusNotFound, fmt.Errorf("That database doesn't seem to exist")
438-
}
439-
440-
// Retrieve correctly capitalised username for the database owner
441-
usr, err := com.User(dbOwner)
442-
if err != nil {
443-
return http.StatusBadRequest, err
444-
}
445-
446-
// Store information
447-
meta.Database = dbName
448-
meta.Owner = usr.Username
449-
meta.Folder = "/"
450-
}
451-
452444
return
453445
}
454446

0 commit comments

Comments
 (0)