mirror of
https://github.com/h44z/wg-portal.git
synced 2025-12-14 02:26:19 +00:00
WIP: new user management and authentication system, use go 1.16 embed
This commit is contained in:
@@ -15,7 +15,7 @@ func (s *Server) GetHandleError(c *gin.Context, code int, message, details strin
|
||||
"Details": details,
|
||||
},
|
||||
"Route": c.Request.URL.Path,
|
||||
"Session": s.getSessionData(c),
|
||||
"Session": GetSessionData(c),
|
||||
"Static": s.getStaticData(),
|
||||
})
|
||||
}
|
||||
@@ -29,90 +29,88 @@ func (s *Server) GetIndex(c *gin.Context) {
|
||||
Device Device
|
||||
}{
|
||||
Route: c.Request.URL.Path,
|
||||
Alerts: s.getFlashes(c),
|
||||
Session: s.getSessionData(c),
|
||||
Alerts: GetFlashes(c),
|
||||
Session: GetSessionData(c),
|
||||
Static: s.getStaticData(),
|
||||
Device: s.users.GetDevice(),
|
||||
Device: s.peers.GetDevice(),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) GetAdminIndex(c *gin.Context) {
|
||||
currentSession := s.getSessionData(c)
|
||||
currentSession := GetSessionData(c)
|
||||
|
||||
sort := c.Query("sort")
|
||||
if sort != "" {
|
||||
if currentSession.SortedBy != sort {
|
||||
currentSession.SortedBy = sort
|
||||
currentSession.SortDirection = "asc"
|
||||
if currentSession.SortedBy["peers"] != sort {
|
||||
currentSession.SortedBy["peers"] = sort
|
||||
currentSession.SortDirection["peers"] = "asc"
|
||||
} else {
|
||||
if currentSession.SortDirection == "asc" {
|
||||
currentSession.SortDirection = "desc"
|
||||
if currentSession.SortDirection["peers"] == "asc" {
|
||||
currentSession.SortDirection["peers"] = "desc"
|
||||
} else {
|
||||
currentSession.SortDirection = "asc"
|
||||
currentSession.SortDirection["peers"] = "asc"
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.updateSessionData(c, currentSession); err != nil {
|
||||
if err := UpdateSessionData(c, currentSession); err != nil {
|
||||
s.GetHandleError(c, http.StatusInternalServerError, "sort error", "failed to save session")
|
||||
return
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, "/admin")
|
||||
c.Redirect(http.StatusSeeOther, "/admin/")
|
||||
return
|
||||
}
|
||||
|
||||
search, searching := c.GetQuery("search")
|
||||
if searching {
|
||||
currentSession.Search = search
|
||||
currentSession.Search["peers"] = search
|
||||
|
||||
if err := s.updateSessionData(c, currentSession); err != nil {
|
||||
if err := UpdateSessionData(c, currentSession); err != nil {
|
||||
s.GetHandleError(c, http.StatusInternalServerError, "search error", "failed to save session")
|
||||
return
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, "/admin")
|
||||
c.Redirect(http.StatusSeeOther, "/admin/")
|
||||
return
|
||||
}
|
||||
|
||||
device := s.users.GetDevice()
|
||||
users := s.users.GetFilteredAndSortedUsers(currentSession.SortedBy, currentSession.SortDirection, currentSession.Search)
|
||||
device := s.peers.GetDevice()
|
||||
users := s.peers.GetFilteredAndSortedPeers(currentSession.SortedBy["peers"], currentSession.SortDirection["peers"], currentSession.Search["peers"])
|
||||
|
||||
c.HTML(http.StatusOK, "admin_index.html", struct {
|
||||
Route string
|
||||
Alerts []FlashData
|
||||
Session SessionData
|
||||
Static StaticData
|
||||
Peers []Peer
|
||||
TotalPeers int
|
||||
Device Device
|
||||
LdapDisabled bool
|
||||
Route string
|
||||
Alerts []FlashData
|
||||
Session SessionData
|
||||
Static StaticData
|
||||
Peers []Peer
|
||||
TotalPeers int
|
||||
Device Device
|
||||
}{
|
||||
Route: c.Request.URL.Path,
|
||||
Alerts: s.getFlashes(c),
|
||||
Session: currentSession,
|
||||
Static: s.getStaticData(),
|
||||
Peers: users,
|
||||
TotalPeers: len(s.users.GetAllUsers()),
|
||||
Device: device,
|
||||
LdapDisabled: s.ldapDisabled,
|
||||
Route: c.Request.URL.Path,
|
||||
Alerts: GetFlashes(c),
|
||||
Session: currentSession,
|
||||
Static: s.getStaticData(),
|
||||
Peers: users,
|
||||
TotalPeers: len(s.peers.GetAllPeers()),
|
||||
Device: device,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) GetUserIndex(c *gin.Context) {
|
||||
currentSession := s.getSessionData(c)
|
||||
currentSession := GetSessionData(c)
|
||||
|
||||
sort := c.Query("sort")
|
||||
if sort != "" {
|
||||
if currentSession.SortedBy != sort {
|
||||
currentSession.SortedBy = sort
|
||||
currentSession.SortDirection = "asc"
|
||||
if currentSession.SortedBy["userpeers"] != sort {
|
||||
currentSession.SortedBy["userpeers"] = sort
|
||||
currentSession.SortDirection["userpeers"] = "asc"
|
||||
} else {
|
||||
if currentSession.SortDirection == "asc" {
|
||||
currentSession.SortDirection = "desc"
|
||||
if currentSession.SortDirection["userpeers"] == "asc" {
|
||||
currentSession.SortDirection["userpeers"] = "desc"
|
||||
} else {
|
||||
currentSession.SortDirection = "asc"
|
||||
currentSession.SortDirection["userpeers"] = "asc"
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.updateSessionData(c, currentSession); err != nil {
|
||||
if err := UpdateSessionData(c, currentSession); err != nil {
|
||||
s.GetHandleError(c, http.StatusInternalServerError, "sort error", "failed to save session")
|
||||
return
|
||||
}
|
||||
@@ -120,8 +118,8 @@ func (s *Server) GetUserIndex(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
device := s.users.GetDevice()
|
||||
users := s.users.GetSortedUsersForEmail(currentSession.SortedBy, currentSession.SortDirection, currentSession.Email)
|
||||
device := s.peers.GetDevice()
|
||||
users := s.peers.GetSortedPeersForEmail(currentSession.SortedBy["userpeers"], currentSession.SortDirection["userpeers"], currentSession.Email)
|
||||
|
||||
c.HTML(http.StatusOK, "user_index.html", struct {
|
||||
Route string
|
||||
@@ -133,7 +131,7 @@ func (s *Server) GetUserIndex(c *gin.Context) {
|
||||
Device Device
|
||||
}{
|
||||
Route: c.Request.URL.Path,
|
||||
Alerts: s.getFlashes(c),
|
||||
Alerts: GetFlashes(c),
|
||||
Session: currentSession,
|
||||
Static: s.getStaticData(),
|
||||
Peers: users,
|
||||
@@ -143,29 +141,29 @@ func (s *Server) GetUserIndex(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (s *Server) updateFormInSession(c *gin.Context, formData interface{}) error {
|
||||
currentSession := s.getSessionData(c)
|
||||
currentSession := GetSessionData(c)
|
||||
currentSession.FormData = formData
|
||||
|
||||
if err := s.updateSessionData(c, currentSession); err != nil {
|
||||
if err := UpdateSessionData(c, currentSession); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) setNewUserFormInSession(c *gin.Context) (SessionData, error) {
|
||||
currentSession := s.getSessionData(c)
|
||||
// If session does not contain a user form ignore update
|
||||
func (s *Server) setNewPeerFormInSession(c *gin.Context) (SessionData, error) {
|
||||
currentSession := GetSessionData(c)
|
||||
// If session does not contain a peer form ignore update
|
||||
// If url contains a formerr parameter reset the form
|
||||
if currentSession.FormData == nil || c.Query("formerr") == "" {
|
||||
user, err := s.PrepareNewUser()
|
||||
user, err := s.PrepareNewPeer()
|
||||
if err != nil {
|
||||
return currentSession, err
|
||||
}
|
||||
currentSession.FormData = user
|
||||
}
|
||||
|
||||
if err := s.updateSessionData(c, currentSession); err != nil {
|
||||
if err := UpdateSessionData(c, currentSession); err != nil {
|
||||
return currentSession, err
|
||||
}
|
||||
|
||||
@@ -173,14 +171,14 @@ func (s *Server) setNewUserFormInSession(c *gin.Context) (SessionData, error) {
|
||||
}
|
||||
|
||||
func (s *Server) setFormInSession(c *gin.Context, formData interface{}) (SessionData, error) {
|
||||
currentSession := s.getSessionData(c)
|
||||
currentSession := GetSessionData(c)
|
||||
// If session does not contain a form ignore update
|
||||
// If url contains a formerr parameter reset the form
|
||||
if currentSession.FormData == nil || c.Query("formerr") == "" {
|
||||
currentSession.FormData = formData
|
||||
}
|
||||
|
||||
if err := s.updateSessionData(c, currentSession); err != nil {
|
||||
if err := UpdateSessionData(c, currentSession); err != nil {
|
||||
return currentSession, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user