2024-09-12 19:15:38 -04:00
|
|
|
// Copyright 2023 Harran Ali <harran.m@gmail.com>. All rights reserved.
|
|
|
|
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
|
|
|
// Use of this source code is governed by MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
2024-12-16 20:05:37 -05:00
|
|
|
"log"
|
2024-12-04 08:39:08 -05:00
|
|
|
"time"
|
2024-12-16 20:05:37 -05:00
|
|
|
|
|
|
|
"git.smarteching.com/goffee/core"
|
|
|
|
"git.smarteching.com/goffee/cup/models"
|
2024-09-12 19:15:38 -04:00
|
|
|
)
|
|
|
|
|
2024-12-16 20:05:37 -05:00
|
|
|
func CreateSeedData() {
|
|
|
|
|
|
|
|
db := core.ResolveGorm()
|
|
|
|
var hashing = new(core.Hashing)
|
|
|
|
var role models.Role
|
|
|
|
|
|
|
|
// seed user
|
|
|
|
password := "goffee"
|
|
|
|
name := "admin"
|
|
|
|
fullname := "Goffee administrator"
|
|
|
|
email := "change@me.com"
|
|
|
|
passwordHashed, _ := hashing.HashPassword(password)
|
|
|
|
|
|
|
|
user := models.User{
|
|
|
|
Name: name,
|
|
|
|
Fullname: fullname,
|
|
|
|
Email: email,
|
|
|
|
Password: passwordHashed,
|
|
|
|
}
|
|
|
|
result := db.Create(&user)
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Fatal("Can't create seed user in database")
|
|
|
|
}
|
|
|
|
// seed roles
|
|
|
|
roles := []models.Role{
|
|
|
|
{Name: "Administrator", Slug: "admin"},
|
|
|
|
{Name: "Authenticated", Slug: "authenticated"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, role := range roles {
|
|
|
|
result = db.Create(&role)
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Fatal("Can't create seed role in database")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// seed permission
|
|
|
|
permission := models.Permission{Name: "Users administration", Slug: "admin-users"}
|
|
|
|
result = db.Create(&permission)
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Fatal("Can't create seed permission in database")
|
|
|
|
}
|
|
|
|
result = db.Where("slug = ?", "admin").First(&role)
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Fatal("Can't find user admin in database")
|
|
|
|
}
|
|
|
|
result = db.Create(&models.RolePermission{RoleID: role.ID, PermissionID: permission.ID})
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Fatal("Can't register permission role in database")
|
|
|
|
}
|
|
|
|
result = db.Create(&models.UserRole{UserID: user.ID, RoleID: role.ID})
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Fatal("Can't assign role administrator to user in database")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 19:15:38 -04:00
|
|
|
// generate a hashed string to be used as key for caching auth jwt token
|
|
|
|
func CreateAuthTokenHashedCacheKey(userID uint, userAgent string) string {
|
|
|
|
cacheKey := fmt.Sprintf("userid:_%v_useragent:_%v_jwt_token", userID, userAgent)
|
|
|
|
hashedCacheKey := fmt.Sprintf("%v", fmt.Sprintf("%x", md5.Sum([]byte(cacheKey))))
|
|
|
|
|
|
|
|
return hashedCacheKey
|
|
|
|
}
|
2024-12-04 08:39:08 -05:00
|
|
|
|
|
|
|
func FormatUnix(value int64) string {
|
|
|
|
return time.Unix(value, 0).Format("2006-01-02 15:04:05")
|
|
|
|
}
|