// Copyright 2023 Harran Ali . All rights reserved. // Copyright (c) 2024 Zeni Kim // 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" "log" "time" "git.smarteching.com/goffee/core" "git.smarteching.com/goffee/cup/models" ) 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") } } // 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 } func FormatUnix(value int64) string { return time.Unix(value, 0).Format("2006-01-02 15:04:05") }