develop #11
4 changed files with 85 additions and 16 deletions
|
@ -6,8 +6,8 @@ package models
|
|||
|
||||
type UserRole struct {
|
||||
BaseModel
|
||||
UserID string // The user id
|
||||
RoleID uint // The role id
|
||||
UserID uint // The user id
|
||||
RoleID uint // The role id
|
||||
}
|
||||
|
||||
// TableName sets the table name
|
||||
|
|
|
@ -6,8 +6,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.smarteching.com/goffee/core"
|
||||
"git.smarteching.com/goffee/cup/models"
|
||||
"git.smarteching.com/goffee/cup/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func RunAutoMigrations() {
|
||||
|
@ -17,9 +21,18 @@ func RunAutoMigrations() {
|
|||
//##############################
|
||||
|
||||
// Add auto migrations for your models here...
|
||||
db.AutoMigrate(&models.User{})
|
||||
db.AutoMigrate(&models.UserRole{})
|
||||
db.AutoMigrate(&models.Role{})
|
||||
db.AutoMigrate(&models.RolePermission{})
|
||||
db.AutoMigrate(&models.Permission{})
|
||||
|
||||
// End your auto migrations
|
||||
|
||||
// Create seed data data, DO NOT TOUCH
|
||||
if err := db.AutoMigrate(&models.User{}); err == nil && db.Migrator().HasTable(&models.User{}) {
|
||||
if err := db.First(&models.User{}).Error; errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
utils.CreateSeedData()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -111,8 +111,7 @@ func (a *Authority) AssignPermissionsToRole(c *core.Context, roleSlug string, pe
|
|||
}
|
||||
|
||||
// Assigns a role to a given user
|
||||
func (a *Authority) AssignRoleToUser(c *core.Context, userID interface{}, roleSlug string) error {
|
||||
userIDStr := fmt.Sprintf("%v", userID)
|
||||
func (a *Authority) AssignRoleToUser(c *core.Context, userID uint, roleSlug string) error {
|
||||
var role models.Role
|
||||
res := c.GetGorm().Where("slug = ?", roleSlug).First(&role)
|
||||
if res.Error != nil {
|
||||
|
@ -122,9 +121,9 @@ func (a *Authority) AssignRoleToUser(c *core.Context, userID interface{}, roleSl
|
|||
return res.Error
|
||||
}
|
||||
var userRole models.UserRole
|
||||
res = c.GetGorm().Where("user_id = ?", userIDStr).Where("role_id = ?", role.ID).First(&userRole)
|
||||
res = c.GetGorm().Where("user_id = ?", userID).Where("role_id = ?", role.ID).First(&userRole)
|
||||
if res.Error != nil && errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
c.GetGorm().Create(&models.UserRole{UserID: userIDStr, RoleID: role.ID})
|
||||
c.GetGorm().Create(&models.UserRole{UserID: userID, RoleID: role.ID})
|
||||
return nil
|
||||
}
|
||||
if res.Error != nil && !errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
|
@ -135,8 +134,7 @@ func (a *Authority) AssignRoleToUser(c *core.Context, userID interface{}, roleSl
|
|||
}
|
||||
|
||||
// Checks if a role is assigned to a user
|
||||
func (a *Authority) CheckUserRole(c *core.Context, userID interface{}, roleSlug string) (bool, error) {
|
||||
userIDStr := fmt.Sprintf("%v", userID)
|
||||
func (a *Authority) CheckUserRole(c *core.Context, userID uint, roleSlug string) (bool, error) {
|
||||
// find the role
|
||||
var role models.Role
|
||||
res := c.GetGorm().Where("slug = ?", roleSlug).First(&role)
|
||||
|
@ -149,7 +147,7 @@ func (a *Authority) CheckUserRole(c *core.Context, userID interface{}, roleSlug
|
|||
|
||||
// check if the role is a assigned
|
||||
var userRole models.UserRole
|
||||
res = c.GetGorm().Where("user_id = ?", userIDStr).Where("role_id = ?", role.ID).First(&userRole)
|
||||
res = c.GetGorm().Where("user_id = ?", userID).Where("role_id = ?", role.ID).First(&userRole)
|
||||
if res.Error != nil {
|
||||
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
|
@ -161,11 +159,10 @@ func (a *Authority) CheckUserRole(c *core.Context, userID interface{}, roleSlug
|
|||
}
|
||||
|
||||
// Checks if a permission is assigned to a user
|
||||
func (a *Authority) CheckUserPermission(c *core.Context, userID interface{}, permSlug string) (bool, error) {
|
||||
userIDStr := fmt.Sprintf("%v", userID)
|
||||
func (a *Authority) CheckUserPermission(c *core.Context, userID uint, permSlug string) (bool, error) {
|
||||
// the user role
|
||||
var userRoles []models.UserRole
|
||||
res := c.GetGorm().Where("user_id = ?", userIDStr).Find(&userRoles)
|
||||
res := c.GetGorm().Where("user_id = ?", userID).Find(&userRoles)
|
||||
if res.Error != nil {
|
||||
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
|
@ -280,10 +277,9 @@ func (a *Authority) GetAllRoles(c *core.Context) ([]models.Role, error) {
|
|||
}
|
||||
|
||||
// Returns all user assigned roles
|
||||
func (a *Authority) GetUserRoles(c *core.Context, userID interface{}) ([]models.Role, error) {
|
||||
userIDStr := fmt.Sprintf("%v", userID)
|
||||
func (a *Authority) GetUserRoles(c *core.Context, userID uint) ([]models.Role, error) {
|
||||
var userRoles []models.UserRole
|
||||
res := c.GetGorm().Where("user_id = ?", userIDStr).Find(&userRoles)
|
||||
res := c.GetGorm().Where("user_id = ?", userID).Find(&userRoles)
|
||||
if res.Error != nil {
|
||||
return nil, res.Error
|
||||
}
|
||||
|
|
|
@ -8,9 +8,69 @@ 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)
|
||||
|
|
Loading…
Reference in a new issue