2020-01-27 08:35:37 +01:00
|
|
|
package orm
|
|
|
|
|
2020-02-03 14:13:01 +01:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
)
|
2020-01-27 11:43:56 +01:00
|
|
|
|
2020-01-27 08:35:37 +01:00
|
|
|
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
|
|
|
|
}
|
2020-01-27 11:43:56 +01:00
|
|
|
|
|
|
|
func WriteCreator(r *http.Request, modifier Modifier) {
|
|
|
|
modifier.SetCreatorID(getUserIDFromTokenAsUint(r))
|
|
|
|
modifier.SetCreatorRole(getUserRole(r))
|
2021-01-10 18:04:34 +01:00
|
|
|
modifier.SetCreatorIP(r.Header.Get("X-Forwarded-For"))
|
2020-01-27 11:43:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func WriteUpdater(r *http.Request, modifier Modifier) {
|
|
|
|
modifier.SetUpdaterID(getUserIDFromTokenAsUint(r))
|
|
|
|
modifier.SetUpdaterRole(getUserRole(r))
|
2021-01-10 18:04:34 +01:00
|
|
|
modifier.SetUpdaterIP(r.Header.Get("X-Forwarded-For"))
|
2020-01-27 11:43:56 +01:00
|
|
|
}
|