Files
kontor/kontor-gin/internal/schema/admin.go
T
Thomas Peetz f0f1819a1f
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
umstellung auf Service und Repository Verwendung - WIP
2026-09-25 16:57:23 +02:00

122 lines
3.6 KiB
Go

package schema
import (
"kontor-api-gin/internal/helpers"
"time"
"golang.org/x/crypto/bcrypt"
)
func VerifyPassword(password, hashedPassword string) error {
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
}
func LoginCheck(email string, password string) (string, error) {
var err error
profile := Profile{}
err = DB.Model(Profile{}).Where("email = ?", email).Take(&profile).Error
if err != nil {
return "", err
}
err = VerifyPassword(password, profile.Password)
if err != nil && err == bcrypt.ErrMismatchedHashAndPassword {
return "", err
}
token, err := helpers.GenerateToken(profile.ID)
if err != nil {
return "", err
}
return token, nil
}
type Profile struct {
ID string `gorm:"type:string;primary_key;"`
Created_Date time.Time `json:"created_date"`
Last_Modified_date time.Time `json:"last_modified_date"`
Deleted_Date *time.Time `json:"deleted_date"`
Version int `gorm:"default:0" json:"version"`
First_Name *string `json:"first_name"`
Last_Name *string `json:"last_name"`
User_Name string `json:"user_name"`
Email string `json:"email"`
Password string `json:"password"`
Enabled bool `json:"enabled"`
Assignments []Assignment `gorm:"foreignKey:ProfileID" json:"assignments"`
Tokens []Token `gorm:"foreignKey:ProfileID" json:"tokens"`
}
func (profile *Profile) TableName() string {
return "profile"
}
func (profile *Profile) GetFullName() string {
full_name := ""
if profile.First_Name != nil {
full_name += *profile.First_Name
}
if profile.Last_Name != nil {
full_name += " "
full_name += *profile.Last_Name
}
return full_name
}
type Token struct {
ID string `gorm:"type:string;primary_key;"`
Created_Date time.Time `json:"created_date"`
Last_Modified_date time.Time `json:"last_modified_date"`
Deleted_Date *time.Time `json:"deleted_date"`
Version int `gorm:"default:0" json:"version"`
Token string `gorm:"uniqueIndex" json:"token"`
Name string `json:"name"`
Last_Used_date time.Time `json:"last_used_date"`
Enabled bool `json:"enabled"`
ProfileID *string `json:"profile_id"`
Profile *Profile `gorm:"foreignKey:ProfileID" json:"profile"`
}
func (token *Token) TableName() string {
return "token"
}
type Permission struct {
ID string `gorm:"type:string;primary_key;"`
Created_Date time.Time `json:"created_date"`
Last_Modified_date time.Time `json:"last_modified_date"`
Deleted_Date *time.Time `json:"deleted_date"`
Version int `gorm:"default:0" json:"version"`
Name string `json:"name"`
Assignments []Assignment `gorm:"foreignKey:PermissionID" json:"assignments"`
}
func (permission *Permission) TableName() string {
return "permission"
}
type Assignment struct {
ID string `gorm:"type:string;primary_key;"`
Created_Date time.Time `json:"created_date"`
Last_Modified_date time.Time `json:"last_modified_date"`
Deleted_Date *time.Time `json:"deleted_date"`
Version int `gorm:"default:0" json:"version"`
ProfileID *string `json:"profile_id"`
Profile *Profile `gorm:"foreignKey:ProfileID" json:"profile"`
PermissionID *string `json:"permission_id"`
Permission *Profile `gorm:"foreignKey:PermissionID" json:"permission"`
}
func (assignment *Assignment) TableName() string {
return "assignment"
}