Import sources from Kontor
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/dao"
|
||||
"gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/properties"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
var sessionDao = SessionDAO{Db: dao.KalorienmanagerDb}
|
||||
|
||||
// EnsureLoggedIn ensures that a request will be aborted with an error
|
||||
// if the user is not logged in
|
||||
func EnsureLoggedIn() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// If there's an error or if the token is empty
|
||||
// the user is not logged in
|
||||
sessionInterface, _ := c.Get("session")
|
||||
sessionID := sessionInterface.(string)
|
||||
if session, err := sessionDao.GetSession(sessionID); err != nil || session.Username == "" {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/")
|
||||
//c.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// EnsureAdminStatus ensures that a request will be aborted with an error
|
||||
// if the user is not logged in
|
||||
func EnsureAdminStatus() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// If there's an error or if the token is empty
|
||||
// the user is not logged in
|
||||
sessionInterface, _ := c.Get("session")
|
||||
sessionID := sessionInterface.(string)
|
||||
if session, err := sessionDao.GetSession(sessionID); err != nil || !session.IsAdmin {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/")
|
||||
//c.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// EnsureNotLoggedIn ensures that a request will be aborted with an error
|
||||
// if the user is already logged in
|
||||
func EnsureNotLoggedIn() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// If there's no error or if the token is not empty
|
||||
// the user is already logged in
|
||||
sessionInterface, _ := c.Get("session")
|
||||
sessionID := sessionInterface.(string)
|
||||
if session, err := sessionDao.GetSession(sessionID); err != nil || session.Username != "" {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/")
|
||||
//c.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetSessionStatus reads sessionId from cookie if available or create new session object
|
||||
// and sets cookie accordingly.
|
||||
func SetSessionStatus() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if sessionID, err := c.Cookie("session"); err == nil || sessionID != "" {
|
||||
c.Set("session", sessionID)
|
||||
} else {
|
||||
session, _ := sessionDao.GetSession(bson.NewObjectId().Hex())
|
||||
sessionID := session.ID.Hex()
|
||||
c.Set("session", sessionID)
|
||||
c.SetCookie("session", sessionID, 3600, "", "", false, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetSessionData populates sesion information with username, admin status of user and
|
||||
// application version.
|
||||
func SetSessionData(c *gin.Context, data gin.H) {
|
||||
sessionInterface, _ := c.Get("session")
|
||||
sessionID := sessionInterface.(string)
|
||||
// TODO move PrintDebug("setSessionData(): %v", sessionId)
|
||||
session, _ := sessionDao.GetSession(sessionID)
|
||||
// TODO move PrintDebug("setSessionData(): %v", *session)
|
||||
data["is_logged_in"] = (session.Username != "")
|
||||
data["is_admin"] = session.IsAdmin
|
||||
data["version"] = properties.Version
|
||||
}
|
||||
Reference in New Issue
Block a user