nezha/model/common.go

54 lines
1.0 KiB
Go
Raw Normal View History

2019-12-08 03:59:58 -05:00
package model
import (
"time"
2024-12-16 06:40:37 -05:00
"github.com/gin-gonic/gin"
)
2019-12-08 03:59:58 -05:00
2024-11-21 11:19:36 -05:00
const (
CtxKeyAuthorizedUser = "ckau"
CtxKeyRealIPStr = "ckri"
)
type CtxKeyRealIP struct{}
2019-12-08 03:59:58 -05:00
type Common struct {
ID uint64 `gorm:"primaryKey" json:"id,omitempty"`
CreatedAt time.Time `gorm:"index;<-:create" json:"created_at,omitempty"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at,omitempty"`
// Do not use soft deletion
// DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
2024-12-16 06:40:37 -05:00
UserID uint64 `json:"user_id,omitempty"`
}
2024-12-16 10:38:31 -05:00
func (c *Common) GetID() uint64 {
return c.ID
}
2024-12-16 06:40:37 -05:00
func (c *Common) HasPermission(ctx *gin.Context) bool {
auth, ok := ctx.Get(CtxKeyAuthorizedUser)
if !ok {
return false
}
user := *auth.(*User)
if user.Role == RoleAdmin {
return true
}
return user.ID == c.UserID
}
type CommonInterface interface {
2024-12-16 10:38:31 -05:00
GetID() uint64
2024-12-16 06:40:37 -05:00
HasPermission(*gin.Context) bool
2019-12-08 03:59:58 -05:00
}
type Response struct {
2021-08-10 08:13:17 -04:00
Code int `json:"code,omitempty"`
2019-12-08 03:59:58 -05:00
Message string `json:"message,omitempty"`
Result interface{} `json:"result,omitempty"`
}