oef/orm/usermodifier.go

63 lines
1.2 KiB
Go

package orm
import (
"net/http"
)
type Modifier interface {
SetCreatorID(id uint)
SetUpdaterID(id uint)
SetCreatorRole(role string)
SetUpdaterRole(role string)
SetCreatorIP(addr string)
SetUpdaterIP(addr string)
}
type UserModifier struct {
CreatorID uint
UpdaterID uint
CreatorRole string
UpdaterRole string
CreatorIP string
UpdaterIP string
Creator *User
Updater *User
}
func (um UserModifier) CreatedBy() *User {
switch um.CreatorRole {
case "administrator":
return &AdministratorUser
case "subscriber":
return &SubscriberUser
}
return um.Creator
}
func (um UserModifier) UpdatedBy() *User {
switch um.UpdaterRole {
case "administrator":
return &AdministratorUser
case "subscriber":
return &SubscriberUser
}
return um.Updater
}
func WriteCreator(r *http.Request, modifier Modifier) {
modifier.SetCreatorID(getUserIDFromTokenAsUint(r))
modifier.SetCreatorRole(getUserRole(r))
modifier.SetCreatorIP(r.Header.Get("X-Forwarded-For"))
}
func WriteUpdater(r *http.Request, modifier Modifier) {
modifier.SetUpdaterID(getUserIDFromTokenAsUint(r))
modifier.SetUpdaterRole(getUserRole(r))
modifier.SetUpdaterIP(r.Header.Get("X-Forwarded-For"))
}