2018-11-07 10:10:51 +00:00
|
|
|
package controllers
|
|
|
|
|
2018-11-07 19:13:26 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2018-11-15 17:10:24 +00:00
|
|
|
"multitenantStack/models"
|
2018-11-08 10:21:06 +00:00
|
|
|
companydb "multitenantStack/services/companydb"
|
2018-11-15 17:10:24 +00:00
|
|
|
"time"
|
2018-11-07 19:13:26 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BaseAPIController operations for APIs
|
2018-11-07 10:10:51 +00:00
|
|
|
type BaseAPIController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:10:24 +00:00
|
|
|
type SessionData struct {
|
|
|
|
Email string
|
|
|
|
CompanyName string
|
|
|
|
CompanyUserID int
|
|
|
|
Exp time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
var jwtSession SessionData
|
|
|
|
var currentUser *models.CompanyUser
|
2018-11-07 19:13:26 +00:00
|
|
|
var companyDB *sql.DB
|
|
|
|
var o orm.Ormer
|
|
|
|
|
|
|
|
//var database sql.database
|
|
|
|
|
|
|
|
// Prepare parse all requests that come after this controller for valid auth
|
|
|
|
func (c *BaseAPIController) Prepare() {
|
|
|
|
|
|
|
|
tokenString := c.Ctx.Request.Header.Get("X-JWTtoken")
|
|
|
|
|
|
|
|
if tokenString == "" {
|
|
|
|
c.ServeJSONError("No Token provided")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token, db, err := companydb.GetDatabase(tokenString)
|
|
|
|
if err != nil {
|
|
|
|
c.ServeJSONError("Token invalid")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:10:24 +00:00
|
|
|
jwtSession.Email = token["email"].(string)
|
|
|
|
jwtSession.CompanyName = token["companyName"].(string)
|
|
|
|
jwtSession.CompanyUserID = token["companyUserID"].(int)
|
|
|
|
jwtSession.Exp = token["exp"].(time.Time)
|
|
|
|
|
|
|
|
companyUser, err := models.GetCompanyUserById(o, int(jwtSession.CompanyUserID))
|
|
|
|
if err != nil {
|
|
|
|
c.ServeJSONError("Error retrieving Company User")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
currentUser = companyUser
|
|
|
|
|
2018-11-07 19:13:26 +00:00
|
|
|
companyDB = db
|
|
|
|
o, err = orm.NewOrmWithDB("postgres", "company", companyDB)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
c.ServeJSONError("internal")
|
|
|
|
return
|
|
|
|
}
|
2018-11-07 10:10:51 +00:00
|
|
|
}
|