DB Caching mechanism working and final service structure

This commit is contained in:
2018-11-08 11:21:06 +01:00
parent 11eed92c15
commit df822d6ff9
6 changed files with 35 additions and 47 deletions

View File

@ -2,9 +2,9 @@ package controllers
import (
"multitenantStack/models"
companydb "multitenantStack/services/companydbservice"
companydb "multitenantStack/services/companydb"
jwtservice "multitenantStack/services/jwtservice"
tokenTools "multitenantStack/services/tokenTools"
"time"
"github.com/astaxie/beego/orm"
@ -36,8 +36,9 @@ func (c *AuthController) URLMapping() {
func (c *AuthController) Login() {
type AuthResponse struct {
Status int
Jwt string
Status int `json:"status"`
Jwt string `json:"jwt"`
User models.CompanyUser `json:"user"`
}
if c.Ctx.Input.Method() != "POST" {
@ -47,7 +48,7 @@ func (c *AuthController) Login() {
tokenHeader := c.Ctx.Request.Header.Get("X-JWTtoken")
if tokenHeader != "" {
valid, _ := jwtservice.Validate(tokenHeader)
valid, _ := tokenTools.Validate(tokenHeader)
if valid {
c.ServeJSONError("You are already logged in")
return
@ -61,21 +62,18 @@ func (c *AuthController) Login() {
c.ServeJSONError("Email/Password missing")
return
}
/*
systemdb := companydb.GetSystemDatabase()
if systemdb == nil {
c.ServeJSONError("Error retrieving User")
return
}
o, err := orm.NewOrmWithDB("postgres", "default", systemdb)
if err != nil {
c.ServeJSONError("Error retrieving User")
return
}
*/
o := orm.NewOrm()
o.Using("system") //TODO: Replace this with something cleverer (manager) once implemented
systemdb := companydb.GetSystemDatabase()
if systemdb == nil {
c.ServeJSONError("Error retrieving User")
return
}
o, err := orm.NewOrmWithDB("postgres", "default", systemdb)
if err != nil {
c.ServeJSONError("Error retrieving User")
return
}
userCompanyMapping, err := models.GetUserCompanyMapByEmail(o, email)
if err != nil {
@ -109,20 +107,13 @@ func (c *AuthController) Login() {
return
}
//TODO: if found query the company database to get roleID, and name
name := companyUser.Name
roleID := companyUser.Role
tokenString := ""
if email == "admin@admin.at" && password == "my password" {
// The jwtClaims are our trusted clientside session
tokenString = jwtservice.CreateToken(jwt.MapClaims{
tokenString = tokenTools.CreateToken(jwt.MapClaims{
"email": email,
"companyName": companyName,
"companyUserID": companyUserID,
"name": name,
"roleID": roleID,
"exp": time.Now().Unix() + 3600,
})
} else {
@ -130,7 +121,7 @@ func (c *AuthController) Login() {
return
}
json := AuthResponse{200, tokenString}
json := AuthResponse{200, tokenString, *companyUser}
c.Data["json"] = &json
c.ServeJSON()

View File

@ -3,7 +3,7 @@ package controllers
import (
"database/sql"
"fmt"
companydb "multitenantStack/services/companydbservice"
companydb "multitenantStack/services/companydb"
"github.com/astaxie/beego/orm"
jwt "github.com/dgrijalva/jwt-go"