♻️ Move models.Account to sec.UserInfo
This commit is contained in:
@ -5,9 +5,9 @@ import (
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
||||
)
|
||||
|
||||
func GetBotCount(user models.Account) (int64, error) {
|
||||
func GetBotCount(user uint) (int64, error) {
|
||||
var count int64
|
||||
if err := database.C.Where("automated_id = ?", user.ID).Count(&count).Error; err != nil {
|
||||
if err := database.C.Where("automated_id = ?", user).Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
|
@ -21,13 +21,13 @@ func AddEvent(user uint, event, target, ip, ua string) {
|
||||
}
|
||||
|
||||
// AddAuditRecord to keep logs to make administrators' operations clear to query
|
||||
func AddAuditRecord(operator models.Account, act, ip, ua string, metadata map[string]any) {
|
||||
func AddAuditRecord(operator uint, act, ip, ua string, metadata map[string]any) {
|
||||
writeAuditQueue = append(writeAuditQueue, models.AuditRecord{
|
||||
Action: act,
|
||||
Metadata: metadata,
|
||||
IpAddress: ip,
|
||||
UserAgent: ua,
|
||||
AccountID: operator.ID,
|
||||
AccountID: operator,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
||||
@ -9,6 +9,9 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var EReader *sec.JwtReader
|
||||
var EWriter *sec.JwtWriter
|
||||
|
||||
type PayloadClaims struct {
|
||||
jwt.RegisteredClaims
|
||||
|
||||
@ -66,26 +69,5 @@ func EncodeJwt(id string, typ, sub, sed string, nonce *string, aud []string, exp
|
||||
claims.Nonce = *nonce
|
||||
}
|
||||
|
||||
tk := jwt.NewWithClaims(jwt.SigningMethodHS512, claims)
|
||||
|
||||
return tk.SignedString([]byte(viper.GetString("secret")))
|
||||
}
|
||||
|
||||
func DecodeJwt(str string) (PayloadClaims, error) {
|
||||
var claims PayloadClaims
|
||||
tk, err := jwt.ParseWithClaims(str, &claims, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(viper.GetString("secret")), nil
|
||||
})
|
||||
if err != nil {
|
||||
return claims, err
|
||||
}
|
||||
|
||||
if data, ok := tk.Claims.(*PayloadClaims); ok {
|
||||
return *data, nil
|
||||
} else {
|
||||
return claims, fmt.Errorf("unexpected token payload: not payload claims type")
|
||||
}
|
||||
return sec.WriteJwt(EWriter, claims)
|
||||
}
|
||||
|
@ -15,21 +15,21 @@ import (
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
func GetAuthPreference(account models.Account) (models.PreferenceAuth, error) {
|
||||
func GetAuthPreference(account uint) (models.PreferenceAuth, error) {
|
||||
var auth models.PreferenceAuth
|
||||
if err := database.C.Where("account_id = ?", account.ID).First(&auth).Error; err != nil {
|
||||
if err := database.C.Where("account_id = ?", account).First(&auth).Error; err != nil {
|
||||
return auth, err
|
||||
}
|
||||
|
||||
return auth, nil
|
||||
}
|
||||
|
||||
func UpdateAuthPreference(account models.Account, config models.AuthConfig) (models.PreferenceAuth, error) {
|
||||
func UpdateAuthPreference(account uint, config models.AuthConfig) (models.PreferenceAuth, error) {
|
||||
var auth models.PreferenceAuth
|
||||
var err error
|
||||
if auth, err = GetAuthPreference(account); err != nil {
|
||||
auth = models.PreferenceAuth{
|
||||
AccountID: account.ID,
|
||||
AccountID: account,
|
||||
Config: datatypes.NewJSONType(config),
|
||||
}
|
||||
} else {
|
||||
@ -44,16 +44,16 @@ func GetNotificationPreferenceCacheKey(accountId uint) string {
|
||||
return fmt.Sprintf("notification-preference#%d", accountId)
|
||||
}
|
||||
|
||||
func GetNotificationPreference(account models.Account) (models.PreferenceNotification, error) {
|
||||
func GetNotificationPreference(account uint) (models.PreferenceNotification, error) {
|
||||
var notification models.PreferenceNotification
|
||||
cacheManager := cache.New[any](localCache.S)
|
||||
marshal := marshaler.New(cacheManager)
|
||||
contx := context.Background()
|
||||
|
||||
if val, err := marshal.Get(contx, GetNotificationPreferenceCacheKey(account.ID), new(models.PreferenceNotification)); err == nil {
|
||||
if val, err := marshal.Get(contx, GetNotificationPreferenceCacheKey(account), new(models.PreferenceNotification)); err == nil {
|
||||
notification = val.(models.PreferenceNotification)
|
||||
} else {
|
||||
if err := database.C.Where("account_id = ?", account.ID).First(¬ification).Error; err != nil {
|
||||
if err := database.C.Where("account_id = ?", account).First(¬ification).Error; err != nil {
|
||||
return notification, err
|
||||
}
|
||||
CacheNotificationPreference(notification)
|
||||
@ -76,12 +76,12 @@ func CacheNotificationPreference(prefs models.PreferenceNotification) {
|
||||
)
|
||||
}
|
||||
|
||||
func UpdateNotificationPreference(account models.Account, config map[string]bool) (models.PreferenceNotification, error) {
|
||||
func UpdateNotificationPreference(account uint, config map[string]bool) (models.PreferenceNotification, error) {
|
||||
var notification models.PreferenceNotification
|
||||
var err error
|
||||
if notification, err = GetNotificationPreference(account); err != nil {
|
||||
notification = models.PreferenceNotification{
|
||||
AccountID: account.ID,
|
||||
AccountID: account,
|
||||
Config: lo.MapValues(config, func(v bool, k string) any { return v }),
|
||||
}
|
||||
} else {
|
||||
|
@ -18,20 +18,20 @@ func ListCommunityRealm() ([]models.Realm, error) {
|
||||
return realms, nil
|
||||
}
|
||||
|
||||
func ListOwnedRealm(user models.Account) ([]models.Realm, error) {
|
||||
func ListOwnedRealm(user uint) ([]models.Realm, error) {
|
||||
var realms []models.Realm
|
||||
if err := database.C.Where(&models.Realm{AccountID: user.ID}).Find(&realms).Error; err != nil {
|
||||
if err := database.C.Where(&models.Realm{AccountID: user}).Find(&realms).Error; err != nil {
|
||||
return realms, err
|
||||
}
|
||||
|
||||
return realms, nil
|
||||
}
|
||||
|
||||
func ListAvailableRealm(user models.Account) ([]models.Realm, error) {
|
||||
func ListAvailableRealm(user uint) ([]models.Realm, error) {
|
||||
var realms []models.Realm
|
||||
var members []models.RealmMember
|
||||
if err := database.C.Where(&models.RealmMember{
|
||||
AccountID: user.ID,
|
||||
AccountID: user,
|
||||
}).Find(&members).Error; err != nil {
|
||||
return realms, err
|
||||
}
|
||||
@ -57,9 +57,9 @@ func GetRealmWithAlias(alias string) (models.Realm, error) {
|
||||
return realm, nil
|
||||
}
|
||||
|
||||
func NewRealm(realm models.Realm, user models.Account) (models.Realm, error) {
|
||||
func NewRealm(realm models.Realm, user uint) (models.Realm, error) {
|
||||
realm.Members = []models.RealmMember{
|
||||
{AccountID: user.ID, PowerLevel: 100},
|
||||
{AccountID: user, PowerLevel: 100},
|
||||
}
|
||||
|
||||
err := database.C.Save(&realm).Error
|
||||
@ -90,14 +90,14 @@ func GetRealmMember(userId uint, realmId uint) (models.RealmMember, error) {
|
||||
return member, nil
|
||||
}
|
||||
|
||||
func AddRealmMember(user models.Account, affected models.Account, target models.Realm) error {
|
||||
func AddRealmMember(user uint, affected models.Account, target models.Realm) error {
|
||||
if !target.IsPublic && !target.IsCommunity {
|
||||
if member, err := GetRealmMember(user.ID, target.ID); err != nil {
|
||||
if member, err := GetRealmMember(user, target.ID); err != nil {
|
||||
return fmt.Errorf("only realm member can add people: %v", err)
|
||||
} else if member.PowerLevel < 50 {
|
||||
return fmt.Errorf("only realm moderator can add people")
|
||||
}
|
||||
rel, err := GetRelationWithTwoNode(affected.ID, user.ID)
|
||||
rel, err := GetRelationWithTwoNode(affected.ID, user)
|
||||
if err != nil || HasPermNodeWithDefault(
|
||||
rel.PermNodes,
|
||||
"RealmAdd",
|
||||
@ -116,9 +116,9 @@ func AddRealmMember(user models.Account, affected models.Account, target models.
|
||||
return err
|
||||
}
|
||||
|
||||
func RemoveRealmMember(user models.Account, affected models.Account, target models.Realm) error {
|
||||
if user.ID != affected.ID {
|
||||
if member, err := GetRealmMember(user.ID, target.ID); err != nil {
|
||||
func RemoveRealmMember(user uint, affected models.Account, target models.Realm) error {
|
||||
if user != affected.ID {
|
||||
if member, err := GetRealmMember(user, target.ID); err != nil {
|
||||
return fmt.Errorf("only realm member can remove other member: %v", err)
|
||||
} else if member.PowerLevel < 50 {
|
||||
return fmt.Errorf("only realm moderator can invite people")
|
||||
|
@ -9,10 +9,10 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func ListAllRelationship(user models.Account) ([]models.AccountRelationship, error) {
|
||||
func ListAllRelationship(user uint) ([]models.AccountRelationship, error) {
|
||||
var relationships []models.AccountRelationship
|
||||
if err := database.C.
|
||||
Where("account_id = ?", user.ID).
|
||||
Where("account_id = ?", user).
|
||||
Preload("Account").
|
||||
Preload("Related").
|
||||
Find(&relationships).Error; err != nil {
|
||||
@ -22,10 +22,10 @@ func ListAllRelationship(user models.Account) ([]models.AccountRelationship, err
|
||||
return relationships, nil
|
||||
}
|
||||
|
||||
func ListRelationshipWithFilter(user models.Account, status models.RelationshipStatus) ([]models.AccountRelationship, error) {
|
||||
func ListRelationshipWithFilter(user uint, status models.RelationshipStatus) ([]models.AccountRelationship, error) {
|
||||
var relationships []models.AccountRelationship
|
||||
if err := database.C.
|
||||
Where("account_id = ? AND status = ?", user.ID, status).
|
||||
Where("account_id = ? AND status = ?", user, status).
|
||||
Preload("Account").
|
||||
Preload("Related").
|
||||
Find(&relationships).Error; err != nil {
|
||||
|
@ -7,10 +7,10 @@ import (
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
||||
)
|
||||
|
||||
func ListAbuseReport(account models.Account) ([]models.AbuseReport, error) {
|
||||
func ListAbuseReport(account uint) ([]models.AbuseReport, error) {
|
||||
var reports []models.AbuseReport
|
||||
err := database.C.
|
||||
Where("account_id = ?", account.ID).
|
||||
Where("account_id = ?", account).
|
||||
Find(&reports).Error
|
||||
return reports, err
|
||||
}
|
||||
@ -53,13 +53,13 @@ func UpdateAbuseReportStatus(id uint, status, message string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewAbuseReport(resource string, reason string, account models.Account) (models.AbuseReport, error) {
|
||||
func NewAbuseReport(resource string, reason string, account uint) (models.AbuseReport, error) {
|
||||
var report models.AbuseReport
|
||||
if err := database.C.
|
||||
Where(
|
||||
"resource = ? AND account_id = ? AND status IN ?",
|
||||
resource,
|
||||
account.ID,
|
||||
account,
|
||||
[]string{models.ReportStatusPending, models.ReportStatusReviewing},
|
||||
).First(&report).Error; err == nil {
|
||||
return report, fmt.Errorf("you already reported this resource and it still in process")
|
||||
@ -68,7 +68,7 @@ func NewAbuseReport(resource string, reason string, account models.Account) (mod
|
||||
report = models.AbuseReport{
|
||||
Resource: resource,
|
||||
Reason: reason,
|
||||
AccountID: account.ID,
|
||||
AccountID: account,
|
||||
}
|
||||
|
||||
err := database.C.Create(&report).Error
|
||||
|
@ -11,11 +11,11 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func CheckDailyCanSign(user models.Account) error {
|
||||
func CheckDailyCanSign(user uint) error {
|
||||
probe := time.Now().Format("2006-01-02")
|
||||
|
||||
var record models.SignRecord
|
||||
if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil {
|
||||
if err := database.C.Where("account_id = ? AND created_at::date = ?", user, probe).First(&record).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
@ -24,22 +24,22 @@ func CheckDailyCanSign(user models.Account) error {
|
||||
return fmt.Errorf("daliy sign record exists")
|
||||
}
|
||||
|
||||
func GetTodayDailySign(user models.Account) (models.SignRecord, error) {
|
||||
func GetTodayDailySign(user uint) (models.SignRecord, error) {
|
||||
probe := time.Now().Format("2006-01-02")
|
||||
|
||||
var record models.SignRecord
|
||||
if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil {
|
||||
if err := database.C.Where("account_id = ? AND created_at::date = ?", user, probe).First(&record).Error; err != nil {
|
||||
return record, fmt.Errorf("unable get daliy sign record: %v", err)
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
|
||||
func DailySign(user models.Account) (models.SignRecord, error) {
|
||||
func DailySign(user uint) (models.SignRecord, error) {
|
||||
tier := rand.Intn(5)
|
||||
record := models.SignRecord{
|
||||
ResultTier: tier,
|
||||
ResultExperience: rand.Intn(int(math.Max(float64(tier), 1)*100)+1-100) + 100,
|
||||
AccountID: user.ID,
|
||||
AccountID: user,
|
||||
}
|
||||
|
||||
if err := CheckDailyCanSign(user); err != nil {
|
||||
@ -47,7 +47,7 @@ func DailySign(user models.Account) (models.SignRecord, error) {
|
||||
}
|
||||
|
||||
var profile models.AccountProfile
|
||||
if err := database.C.Where("account_id = ?", user.ID).First(&profile).Error; err != nil {
|
||||
if err := database.C.Where("account_id = ?", user).First(&profile).Error; err != nil {
|
||||
return record, fmt.Errorf("unable get account profile: %v", err)
|
||||
} else {
|
||||
profile.Experience += uint64(record.ResultExperience)
|
||||
|
@ -98,27 +98,27 @@ func GetStatusOnline(uid uint) error {
|
||||
}
|
||||
}
|
||||
|
||||
func NewStatus(user models.Account, status models.Status) (models.Status, error) {
|
||||
func NewStatus(user uint, status models.Status) (models.Status, error) {
|
||||
if err := database.C.Save(&status).Error; err != nil {
|
||||
return status, err
|
||||
} else {
|
||||
CacheUserStatus(user.ID, status)
|
||||
CacheUserStatus(user, status)
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func EditStatus(user models.Account, status models.Status) (models.Status, error) {
|
||||
func EditStatus(user uint, status models.Status) (models.Status, error) {
|
||||
if err := database.C.Save(&status).Error; err != nil {
|
||||
return status, err
|
||||
} else {
|
||||
CacheUserStatus(user.ID, status)
|
||||
CacheUserStatus(user, status)
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func ClearStatus(user models.Account) error {
|
||||
func ClearStatus(user uint) error {
|
||||
if err := database.C.
|
||||
Where("account_id = ?", user.ID).
|
||||
Where("account_id = ?", user).
|
||||
Where("clear_at > ?", time.Now()).
|
||||
Updates(models.Status{ClearAt: lo.ToPtr(time.Now())}).Error; err != nil {
|
||||
return err
|
||||
@ -127,7 +127,7 @@ func ClearStatus(user models.Account) error {
|
||||
marshal := marshaler.New(cacheManager)
|
||||
contx := context.Background()
|
||||
|
||||
marshal.Delete(contx, GetStatusCacheKey(user.ID))
|
||||
marshal.Delete(contx, GetStatusCacheKey(user))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -57,7 +57,7 @@ func NewTicket(user models.Account, ip, ua string) (models.AuthTicket, error) {
|
||||
} else {
|
||||
steps = min(steps, int(count))
|
||||
|
||||
cfg, err := GetAuthPreference(user)
|
||||
cfg, err := GetAuthPreference(user.ID)
|
||||
if err == nil && cfg.Config.Data().MaximumAuthSteps >= 1 {
|
||||
steps = min(steps, cfg.Config.Data().MaximumAuthSteps)
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@ -102,7 +103,7 @@ func RefreshToken(token string) (atk, rtk string, err error) {
|
||||
|
||||
var ticket models.AuthTicket
|
||||
var claims PayloadClaims
|
||||
if claims, err = DecodeJwt(token); err != nil {
|
||||
if claims, err = sec.ReadJwt[PayloadClaims](EReader, token); err != nil {
|
||||
return
|
||||
} else if claims.Type != JwtRefreshType {
|
||||
err = fmt.Errorf("invalid token type, expected refresh token")
|
||||
|
Reference in New Issue
Block a user