feat/sso-auth: wip
This commit is contained in:
No files matched your search
@@ -6,11 +6,13 @@ import (
|
|||||||
|
|
||||||
d "github.com/karincake/dodol"
|
d "github.com/karincake/dodol"
|
||||||
rw "github.com/karincake/risoles"
|
rw "github.com/karincake/risoles"
|
||||||
|
sp "github.com/karincake/semprit"
|
||||||
|
sr "github.com/karincake/serabi"
|
||||||
|
|
||||||
m "simrs-vx/internal/domain/main-entities/user"
|
m "simrs-vx/internal/domain/main-entities/user"
|
||||||
s "simrs-vx/internal/use-case/main-use-case/authentication"
|
mf "simrs-vx/internal/domain/main-entities/user-fes"
|
||||||
|
|
||||||
pa "simrs-vx/internal/lib/auth"
|
pa "simrs-vx/internal/lib/auth"
|
||||||
|
s "simrs-vx/internal/use-case/main-use-case/authentication"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Login(w http.ResponseWriter, r *http.Request) {
|
func Login(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -50,3 +52,25 @@ func GuardMW(next http.Handler) http.Handler {
|
|||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoginFes(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var input mf.LoginDto
|
||||||
|
err := sp.IOReaderJson(input, r.Body)
|
||||||
|
if err != nil {
|
||||||
|
rw.WriteJSON(w, http.StatusUnauthorized, d.II{"errors": err}, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
input.AuthPartner_Code = r.Header.Get("X-AuthPartner-Code")
|
||||||
|
input.AuthPartner_SecretKey = r.Header.Get("X-AuthPartner-SecretKey")
|
||||||
|
if err = (sr.Validate(input)); err != nil {
|
||||||
|
rw.WriteJSON(w, http.StatusUnauthorized, d.II{"errors": err}, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// input.Position = Position
|
||||||
|
res, err := s.GenTokenFes(input)
|
||||||
|
if err != nil {
|
||||||
|
rw.WriteJSON(w, http.StatusUnauthorized, d.II{"errors": err}, nil)
|
||||||
|
} else {
|
||||||
|
rw.DataResponse(w, res, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,16 +19,9 @@ import (
|
|||||||
pl "simrs-vx/pkg/logger"
|
pl "simrs-vx/pkg/logger"
|
||||||
p "simrs-vx/pkg/password"
|
p "simrs-vx/pkg/password"
|
||||||
|
|
||||||
ed "simrs-vx/internal/domain/main-entities/doctor"
|
|
||||||
ee "simrs-vx/internal/domain/main-entities/employee"
|
|
||||||
"simrs-vx/internal/domain/main-entities/intern"
|
|
||||||
em "simrs-vx/internal/domain/main-entities/midwife"
|
|
||||||
en "simrs-vx/internal/domain/main-entities/nurse"
|
|
||||||
ep "simrs-vx/internal/domain/main-entities/pharmacist"
|
|
||||||
eu "simrs-vx/internal/domain/main-entities/user"
|
eu "simrs-vx/internal/domain/main-entities/user"
|
||||||
|
euf "simrs-vx/internal/domain/main-entities/user-fes"
|
||||||
erc "simrs-vx/internal/domain/references/common"
|
erc "simrs-vx/internal/domain/references/common"
|
||||||
erg "simrs-vx/internal/domain/references/organization"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const source = "authentication"
|
const source = "authentication"
|
||||||
@@ -115,121 +108,150 @@ func GenToken(input eu.LoginDto) (*d.Data, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// extra
|
// extra
|
||||||
role := []string{}
|
// role := []string{}
|
||||||
switch user.ContractPosition_Code {
|
// switch user.ContractPosition_Code {
|
||||||
case erg.CSCEmp:
|
// case erg.CSCEmp:
|
||||||
// employee
|
// // employee
|
||||||
employee := ee.Employee{}
|
// employee := ee.Employee{}
|
||||||
dg.I.Where("\"User_Id\" = ?", user.Id).First(&employee)
|
// dg.I.Where("\"User_Id\" = ?", user.Id).First(&employee)
|
||||||
if employee.Id == 0 {
|
// if employee.Id == 0 {
|
||||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noEmployee", Message: pl.GenMessage("auth-noEmployee")}}
|
// return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noEmployee", Message: pl.GenMessage("auth-noEmployee")}}
|
||||||
}
|
// }
|
||||||
atClaims["employee_id"] = employee.Id
|
// atClaims["employee_id"] = employee.Id
|
||||||
outputData["employee_id"] = employee.Id
|
// outputData["employee_id"] = employee.Id
|
||||||
role = append(role, "emp-"+string(*employee.Position_Code))
|
// role = append(role, "emp-"+string(*employee.Position_Code))
|
||||||
|
|
||||||
//if employee.Division_Code != nil {
|
// //if employee.Division_Code != nil {
|
||||||
// atClaims["employee_division_code"] = employee.Division_Code
|
// // atClaims["employee_division_code"] = employee.Division_Code
|
||||||
// outputData["employee_division_code"] = employee.Division_Code
|
// // outputData["employee_division_code"] = employee.Division_Code
|
||||||
//}
|
// //}
|
||||||
|
|
||||||
// employee position
|
// // employee position
|
||||||
if employee.Id > 0 && employee.Position_Code != nil {
|
// if employee.Id > 0 && employee.Position_Code != nil {
|
||||||
atClaims["employee_position_code"] = *employee.Position_Code
|
// atClaims["employee_position_code"] = *employee.Position_Code
|
||||||
switch *employee.Position_Code {
|
// switch *employee.Position_Code {
|
||||||
case erg.EPCDoc:
|
// case erg.EPCDoc:
|
||||||
doctor := ed.Doctor{}
|
// doctor := ed.Doctor{}
|
||||||
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&doctor)
|
// dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&doctor)
|
||||||
if doctor.Id == 0 {
|
// if doctor.Id == 0 {
|
||||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noDoctor", Message: pl.GenMessage("auth-noDoctor")}}
|
// return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noDoctor", Message: pl.GenMessage("auth-noDoctor")}}
|
||||||
}
|
// }
|
||||||
atClaims["doctor_code"] = doctor.Code
|
// atClaims["doctor_code"] = doctor.Code
|
||||||
outputData["doctor_code"] = doctor.Code
|
// outputData["doctor_code"] = doctor.Code
|
||||||
|
|
||||||
// specialist
|
// // specialist
|
||||||
if doctor.Specialist_Code != nil {
|
// if doctor.Specialist_Code != nil {
|
||||||
atClaims["specialist_code"] = doctor.Specialist_Code
|
// atClaims["specialist_code"] = doctor.Specialist_Code
|
||||||
outputData["specialist_code"] = doctor.Specialist_Code
|
// outputData["specialist_code"] = doctor.Specialist_Code
|
||||||
}
|
// }
|
||||||
if doctor.Subspecialist_Code != nil {
|
// if doctor.Subspecialist_Code != nil {
|
||||||
atClaims["subspecialist_code"] = doctor.Subspecialist_Code
|
// atClaims["subspecialist_code"] = doctor.Subspecialist_Code
|
||||||
outputData["subspecialist_code"] = doctor.Subspecialist_Code
|
// outputData["subspecialist_code"] = doctor.Subspecialist_Code
|
||||||
}
|
// }
|
||||||
case erg.EPCNur:
|
// case erg.EPCNur:
|
||||||
empData := en.Nurse{}
|
// empData := en.Nurse{}
|
||||||
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
// dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
||||||
if empData.Id == 0 {
|
// if empData.Id == 0 {
|
||||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noNurse", Message: pl.GenMessage("auth-noNurse")}}
|
// return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noNurse", Message: pl.GenMessage("auth-noNurse")}}
|
||||||
}
|
// }
|
||||||
atClaims["nurse_code"] = empData.Code
|
// atClaims["nurse_code"] = empData.Code
|
||||||
outputData["nurse_code"] = empData.Code
|
// outputData["nurse_code"] = empData.Code
|
||||||
case erg.EPCMwi:
|
// case erg.EPCMwi:
|
||||||
empData := em.Midwife{}
|
// empData := em.Midwife{}
|
||||||
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
// dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
||||||
if empData.Id == 0 {
|
// if empData.Id == 0 {
|
||||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noMidwife", Message: pl.GenMessage("auth-noMidwife")}}
|
// return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noMidwife", Message: pl.GenMessage("auth-noMidwife")}}
|
||||||
}
|
// }
|
||||||
atClaims["midwife_code"] = empData.Code
|
// atClaims["midwife_code"] = empData.Code
|
||||||
outputData["midwife_code"] = empData.Code
|
// outputData["midwife_code"] = empData.Code
|
||||||
case erg.EPCPha:
|
// case erg.EPCPha:
|
||||||
empData := ep.Pharmacist{}
|
// empData := ep.Pharmacist{}
|
||||||
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
// dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
||||||
if empData.Id == 0 {
|
// if empData.Id == 0 {
|
||||||
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noPharmacist", Message: pl.GenMessage("auth-noPharmacist")}}
|
// return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noPharmacist", Message: pl.GenMessage("auth-noPharmacist")}}
|
||||||
}
|
// }
|
||||||
atClaims["pharmacist_code"] = empData.Code
|
// atClaims["pharmacist_code"] = empData.Code
|
||||||
outputData["pharmacist_code"] = empData.Code
|
// outputData["pharmacist_code"] = empData.Code
|
||||||
}
|
// }
|
||||||
|
// // specialist
|
||||||
|
// if doctor.Specialist_Code != nil {
|
||||||
|
// atClaims["specialist_code"] = doctor.Specialist_Code
|
||||||
|
// outputData["specialist_code"] = doctor.Specialist_Code
|
||||||
|
// }
|
||||||
|
// if doctor.Subspecialist_Code != nil {
|
||||||
|
// atClaims["subspecialist_code"] = doctor.Subspecialist_Code
|
||||||
|
// outputData["subspecialist_code"] = doctor.Subspecialist_Code
|
||||||
|
// }
|
||||||
|
// case erg.EPCNur:
|
||||||
|
// empData := en.Nurse{}
|
||||||
|
// dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
||||||
|
// if empData.Id == 0 {
|
||||||
|
// return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noNurse", Message: pl.GenMessage("auth-noNurse")}}
|
||||||
|
// }
|
||||||
|
// atClaims["nurse_code"] = empData.Code
|
||||||
|
// outputData["nurse_code"] = empData.Code
|
||||||
|
// case erg.EPCMwi:
|
||||||
|
// empData := em.Midwife{}
|
||||||
|
// dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
||||||
|
// if empData.Id == 0 {
|
||||||
|
// return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-noMidwife", Message: pl.GenMessage("auth-noMidwife")}}
|
||||||
|
// }
|
||||||
|
// atClaims["midwife_code"] = empData.Code
|
||||||
|
// outputData["midwife_code"] = empData.Code
|
||||||
|
// }
|
||||||
|
|
||||||
errorGetPosition := d.FieldErrors{"authentication": d.FieldError{Code: "auth-getData-failed", Message: pl.GenMessage("auth-getData-failed")}}
|
// errorGetPosition := d.FieldErrors{"authentication": d.FieldError{Code: "auth-getData-failed", Message: pl.GenMessage("auth-getData-failed")}}
|
||||||
|
|
||||||
// division position
|
// // division position
|
||||||
divisionPositions, err := getDivisionPosition(employee.Id, &event)
|
// divisionPositions, err := getDivisionPosition(employee.Id, &event)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, errorGetPosition
|
// return nil, errorGetPosition
|
||||||
}
|
// }
|
||||||
|
|
||||||
// installation position
|
// // installation position
|
||||||
installationPositions, err := getInstallationPosition(employee.Id, &event)
|
// installationPositions, err := getInstallationPosition(employee.Id, &event)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, errorGetPosition
|
// return nil, errorGetPosition
|
||||||
}
|
// }
|
||||||
|
|
||||||
// unit position
|
// // unit position
|
||||||
unitPositions, err := getUnitPosition(employee.Id, &event)
|
// unitPositions, err := getUnitPosition(employee.Id, &event)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, errorGetPosition
|
// return nil, errorGetPosition
|
||||||
}
|
// }
|
||||||
|
|
||||||
// specialist position
|
// // specialist position
|
||||||
specialistPositions, err := getSpecialistPosition(employee.Id, &event)
|
// specialistPositions, err := getSpecialistPosition(employee.Id, &event)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, errorGetPosition
|
// return nil, errorGetPosition
|
||||||
}
|
// }
|
||||||
|
|
||||||
// subspecialist position
|
// // subspecialist position
|
||||||
subspecialistPositions, err := getSubspecialistPosition(employee.Id, &event)
|
// subspecialistPositions, err := getSubspecialistPosition(employee.Id, &event)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, errorGetPosition
|
// return nil, errorGetPosition
|
||||||
}
|
// }
|
||||||
|
|
||||||
role = append(role, divisionPositions...)
|
// role = append(role, divisionPositions...)
|
||||||
role = append(role, installationPositions...)
|
// role = append(role, installationPositions...)
|
||||||
role = append(role, unitPositions...)
|
// role = append(role, unitPositions...)
|
||||||
role = append(role, specialistPositions...)
|
// role = append(role, specialistPositions...)
|
||||||
role = append(role, subspecialistPositions...)
|
// role = append(role, subspecialistPositions...)
|
||||||
// atClaims["division_positions"] = divsionPositions
|
// // atClaims["division_positions"] = divsionPositions
|
||||||
// outputData["division_positions"] = divsionPositions
|
// // outputData["division_positions"] = divsionPositions
|
||||||
}
|
// }
|
||||||
case erg.CSCInt:
|
// case erg.CSCInt:
|
||||||
intern := intern.Intern{}
|
// intern := intern.Intern{}
|
||||||
dg.I.Where("\"User_Id\" = ?", user.Id).First(&intern)
|
// dg.I.Where("\"User_Id\" = ?", user.Id).First(&intern)
|
||||||
role = append(role, "int-"+string(*intern.Position_Code))
|
// role = append(role, "int-"+string(*intern.Position_Code))
|
||||||
case erg.CSCSys:
|
// case erg.CSCSys:
|
||||||
role = append(role, "system")
|
// role = append(role, "system")
|
||||||
|
// }
|
||||||
|
// atClaims["roles"] = role
|
||||||
|
// outputData["roles"] = role
|
||||||
|
if err := populateRoles(user, atClaims, outputData, event); err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
atClaims["roles"] = role
|
|
||||||
outputData["roles"] = role
|
|
||||||
|
|
||||||
// Generate jwt
|
// Generate jwt
|
||||||
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
|
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
|
||||||
@@ -339,3 +361,89 @@ func ExtractToken(r *http.Request, tokenType TokenType) (data *pa.AuthInfo, err
|
|||||||
func GetConfig() {
|
func GetConfig() {
|
||||||
a.ParseCfg(&authCfg)
|
a.ParseCfg(&authCfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GenTokenFes(input euf.LoginDto) (*d.Data, error) {
|
||||||
|
event := pl.Event{
|
||||||
|
Feature: "Create",
|
||||||
|
Source: source,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get User Fes
|
||||||
|
userFes := &euf.UserFes{Name: input.Name, AuthPartner_Code: input.AuthPartner_Code}
|
||||||
|
if errCode := getAndCheck(userFes, userFes); errCode != "" {
|
||||||
|
return nil, d.FieldErrors{"authentication": d.FieldError{Code: errCode, Message: pl.GenMessage(errCode)}}
|
||||||
|
}
|
||||||
|
if userFes.AuthPartner.SecretKey != input.AuthPartner_SecretKey {
|
||||||
|
return nil, d.FieldErrors{"authentication": d.FieldError{Code: "auth-secretKey-invalid", Message: pl.GenMessage("auth-secretKey-invalid")}}
|
||||||
|
}
|
||||||
|
|
||||||
|
user := &eu.User{Name: userFes.User_Name}
|
||||||
|
if errCode := getAndCheck(user, user); errCode != "" {
|
||||||
|
return nil, d.FieldErrors{"authentication": d.FieldError{Code: errCode, Message: pl.GenMessage(errCode)}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Access token prep
|
||||||
|
id, err := uuid.NewRandom()
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf(l.I.Msg("uuid-gen-fail"), err))
|
||||||
|
}
|
||||||
|
if input.Duration == 0 {
|
||||||
|
input.Duration = 24 * 60
|
||||||
|
}
|
||||||
|
duration := time.Minute * time.Duration(input.Duration)
|
||||||
|
aUuid := id.String()
|
||||||
|
atExpires := time.Now().Add(duration).Unix()
|
||||||
|
atSecretKey := authCfg.AtSecretKey
|
||||||
|
|
||||||
|
// Create Claim
|
||||||
|
atClaims := jwt.MapClaims{}
|
||||||
|
atClaims["user_id"] = user.Id
|
||||||
|
atClaims["user_name"] = user.Name
|
||||||
|
atClaims["user_contractPosition_code"] = user.ContractPosition_Code
|
||||||
|
atClaims["uuid"] = aUuid
|
||||||
|
atClaims["exp"] = atExpires
|
||||||
|
|
||||||
|
// Create output
|
||||||
|
outputData := d.II{
|
||||||
|
"user_id": strconv.Itoa(int(user.Id)),
|
||||||
|
"user_name": user.Name,
|
||||||
|
"user_contractPosition_code": user.ContractPosition_Code,
|
||||||
|
}
|
||||||
|
|
||||||
|
// extra
|
||||||
|
if err := populateRoles(user, atClaims, outputData, event); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate jwt
|
||||||
|
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
|
||||||
|
ats, err := at.SignedString([]byte(atSecretKey))
|
||||||
|
if err != nil {
|
||||||
|
return nil, d.FieldErrors{"user": d.FieldError{Code: "token-sign-err", Message: pl.GenMessage("token-sign-err")}}
|
||||||
|
}
|
||||||
|
outputData["accessToken"] = ats
|
||||||
|
|
||||||
|
// Save to redis
|
||||||
|
now := time.Now()
|
||||||
|
atx := time.Unix(atExpires, 0) //converting Unix to UTC(to Time object)
|
||||||
|
err = ms.I.Set(aUuid, strconv.Itoa(int(user.Id)), atx.Sub(now)).Err()
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf(l.I.Msg("redis-store-fail"), err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
|
tn := time.Now()
|
||||||
|
user.LoginAttemptCount = 0
|
||||||
|
user.LastSuccessLogin = &tn
|
||||||
|
user.LastAllowdLogin = &tn
|
||||||
|
dg.I.Save(&user)
|
||||||
|
|
||||||
|
// Current data
|
||||||
|
return &d.Data{
|
||||||
|
Meta: d.IS{
|
||||||
|
"source": "authentication",
|
||||||
|
"structure": "single-data",
|
||||||
|
"status": "verified",
|
||||||
|
},
|
||||||
|
Data: outputData,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
@@ -3,13 +3,23 @@ package authentication
|
|||||||
import (
|
import (
|
||||||
dg "github.com/karincake/apem/db-gorm-pg"
|
dg "github.com/karincake/apem/db-gorm-pg"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt"
|
||||||
|
d "github.com/karincake/dodol"
|
||||||
|
|
||||||
pl "simrs-vx/pkg/logger"
|
pl "simrs-vx/pkg/logger"
|
||||||
|
|
||||||
edp "simrs-vx/internal/domain/main-entities/division-position"
|
edp "simrs-vx/internal/domain/main-entities/division-position"
|
||||||
|
ed "simrs-vx/internal/domain/main-entities/doctor"
|
||||||
|
ee "simrs-vx/internal/domain/main-entities/employee"
|
||||||
eip "simrs-vx/internal/domain/main-entities/installation-position"
|
eip "simrs-vx/internal/domain/main-entities/installation-position"
|
||||||
|
"simrs-vx/internal/domain/main-entities/intern"
|
||||||
|
em "simrs-vx/internal/domain/main-entities/midwife"
|
||||||
|
en "simrs-vx/internal/domain/main-entities/nurse"
|
||||||
esp "simrs-vx/internal/domain/main-entities/specialist-position"
|
esp "simrs-vx/internal/domain/main-entities/specialist-position"
|
||||||
essp "simrs-vx/internal/domain/main-entities/subspecialist-position"
|
essp "simrs-vx/internal/domain/main-entities/subspecialist-position"
|
||||||
eup "simrs-vx/internal/domain/main-entities/unit-position"
|
eup "simrs-vx/internal/domain/main-entities/unit-position"
|
||||||
|
eu "simrs-vx/internal/domain/main-entities/user"
|
||||||
|
erg "simrs-vx/internal/domain/references/organization"
|
||||||
|
|
||||||
udp "simrs-vx/internal/use-case/main-use-case/division-position"
|
udp "simrs-vx/internal/use-case/main-use-case/division-position"
|
||||||
uip "simrs-vx/internal/use-case/main-use-case/installation-position"
|
uip "simrs-vx/internal/use-case/main-use-case/installation-position"
|
||||||
@@ -162,3 +172,115 @@ func checkUntPtrClaims(claim map[string]interface{}, key string) *uint {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func populateRoles(user *eu.User, atClaims jwt.MapClaims, outputData d.II, event pl.Event) error {
|
||||||
|
roles := []string{}
|
||||||
|
switch user.ContractPosition_Code {
|
||||||
|
case erg.CSCEmp:
|
||||||
|
// employee
|
||||||
|
employee := ee.Employee{}
|
||||||
|
dg.I.Where("\"User_Id\" = ?", user.Id).First(&employee)
|
||||||
|
if employee.Id == 0 {
|
||||||
|
return d.FieldErrors{"authentication": d.FieldError{Code: "auth-noEmployee", Message: pl.GenMessage("auth-noEmployee")}}
|
||||||
|
}
|
||||||
|
atClaims["employee_id"] = employee.Id
|
||||||
|
outputData["employee_id"] = employee.Id
|
||||||
|
roles = append(roles, "emp-"+string(*employee.Position_Code))
|
||||||
|
|
||||||
|
//if employee.Division_Code != nil {
|
||||||
|
// atClaims["employee_division_code"] = employee.Division_Code
|
||||||
|
// outputData["employee_division_code"] = employee.Division_Code
|
||||||
|
//}
|
||||||
|
|
||||||
|
// employee position
|
||||||
|
if employee.Id > 0 && employee.Position_Code != nil {
|
||||||
|
atClaims["employee_position_code"] = *employee.Position_Code
|
||||||
|
switch *employee.Position_Code {
|
||||||
|
case erg.EPCDoc:
|
||||||
|
doctor := ed.Doctor{}
|
||||||
|
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&doctor)
|
||||||
|
if doctor.Id == 0 {
|
||||||
|
return d.FieldErrors{"authentication": d.FieldError{Code: "auth-noDoctor", Message: pl.GenMessage("auth-noDoctor")}}
|
||||||
|
}
|
||||||
|
atClaims["doctor_code"] = doctor.Code
|
||||||
|
outputData["doctor_code"] = doctor.Code
|
||||||
|
|
||||||
|
// specialist
|
||||||
|
if doctor.Specialist_Code != nil {
|
||||||
|
atClaims["specialist_code"] = doctor.Specialist_Code
|
||||||
|
outputData["specialist_code"] = doctor.Specialist_Code
|
||||||
|
}
|
||||||
|
if doctor.Subspecialist_Code != nil {
|
||||||
|
atClaims["subspecialist_code"] = doctor.Subspecialist_Code
|
||||||
|
outputData["subspecialist_code"] = doctor.Subspecialist_Code
|
||||||
|
}
|
||||||
|
case erg.EPCNur:
|
||||||
|
empData := en.Nurse{}
|
||||||
|
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
||||||
|
if empData.Id == 0 {
|
||||||
|
return d.FieldErrors{"authentication": d.FieldError{Code: "auth-noNurse", Message: pl.GenMessage("auth-noNurse")}}
|
||||||
|
}
|
||||||
|
atClaims["nurse_code"] = empData.Code
|
||||||
|
outputData["nurse_code"] = empData.Code
|
||||||
|
case erg.EPCMwi:
|
||||||
|
empData := em.Midwife{}
|
||||||
|
dg.I.Where("\"Employee_Id\" = ?", employee.Id).First(&empData)
|
||||||
|
if empData.Id == 0 {
|
||||||
|
return d.FieldErrors{"authentication": d.FieldError{Code: "auth-noMidwife", Message: pl.GenMessage("auth-noMidwife")}}
|
||||||
|
}
|
||||||
|
atClaims["midwife_code"] = empData.Code
|
||||||
|
outputData["midwife_code"] = empData.Code
|
||||||
|
}
|
||||||
|
|
||||||
|
errorGetPosition := d.FieldErrors{"authentication": d.FieldError{Code: "auth-getData-failed", Message: pl.GenMessage("auth-getData-failed")}}
|
||||||
|
|
||||||
|
// division position
|
||||||
|
divisionPositions, err := getDivisionPosition(employee.Id, &event)
|
||||||
|
if err != nil {
|
||||||
|
return errorGetPosition
|
||||||
|
}
|
||||||
|
|
||||||
|
// installation position
|
||||||
|
installationPositions, err := getInstallationPosition(employee.Id, &event)
|
||||||
|
if err != nil {
|
||||||
|
return errorGetPosition
|
||||||
|
}
|
||||||
|
|
||||||
|
// unit position
|
||||||
|
unitPositions, err := getUnitPosition(employee.Id, &event)
|
||||||
|
if err != nil {
|
||||||
|
return errorGetPosition
|
||||||
|
}
|
||||||
|
|
||||||
|
// specialist position
|
||||||
|
specialistPositions, err := getSpecialistPosition(employee.Id, &event)
|
||||||
|
if err != nil {
|
||||||
|
return errorGetPosition
|
||||||
|
}
|
||||||
|
|
||||||
|
// subspecialist position
|
||||||
|
subspecialistPositions, err := getSubspecialistPosition(employee.Id, &event)
|
||||||
|
if err != nil {
|
||||||
|
return errorGetPosition
|
||||||
|
}
|
||||||
|
|
||||||
|
roles = append(roles, divisionPositions...)
|
||||||
|
roles = append(roles, installationPositions...)
|
||||||
|
roles = append(roles, unitPositions...)
|
||||||
|
roles = append(roles, specialistPositions...)
|
||||||
|
roles = append(roles, subspecialistPositions...)
|
||||||
|
// atClaims["division_positions"] = divsionPositions
|
||||||
|
// outputData["division_positions"] = divsionPositions
|
||||||
|
}
|
||||||
|
case erg.CSCInt:
|
||||||
|
intern := intern.Intern{}
|
||||||
|
dg.I.Where("\"User_Id\" = ?", user.Id).First(&intern)
|
||||||
|
roles = append(roles, "int-"+string(*intern.Position_Code))
|
||||||
|
case erg.CSCSys:
|
||||||
|
roles = append(roles, "system")
|
||||||
|
}
|
||||||
|
|
||||||
|
atClaims["roles"] = roles
|
||||||
|
outputData["roles"] = roles
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user