oef/orm/usermodifier.go

62 lines
1.1 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.RemoteAddr)
}
func WriteUpdater(r *http.Request, modifier Modifier) {
modifier.SetUpdaterID(getUserIDFromTokenAsUint(r))
modifier.SetUpdaterRole(getUserRole(r))
modifier.SetUpdaterIP(r.RemoteAddr)
}