Add user modifier actions

This commit is contained in:
Andrea Fazzi 2019-12-07 08:58:30 +01:00
parent 768c8b84ca
commit 4b752f2e50
10 changed files with 186 additions and 83 deletions

View file

@ -12,4 +12,9 @@ var (
"it": "L'utente non dispone delle autorizzazioni necessarie a visualizzare questa pagina.", "it": "L'utente non dispone delle autorizzazioni necessarie a visualizzare questa pagina.",
}, },
} }
Formats = map[string]map[string]string{
"dateTime": map[string]string{
"it": "il %02d/%02d/%d alle ore %02d:%02d",
},
}
) )

View file

@ -17,9 +17,9 @@ type Contest struct {
Name string Name string
Category string Category string
Date *time.Time Date time.Time
StartTime *time.Time StartTime time.Time
EndTime *time.Time EndTime time.Time
Questions []*Question Questions []*Question
Participants []*Participant `gorm:"many2many:subscriptions"` Participants []*Participant `gorm:"many2many:subscriptions"`

View file

@ -1,34 +0,0 @@
package orm
import (
"net/http"
"github.com/dgrijalva/jwt-go"
)
type Creator struct {
CreatorID string
CreatorRole string
CreatorIP string
}
func NewCreator(r *http.Request) *Creator {
var claims jwt.MapClaims
if r.Context().Value("user") != nil {
claims = r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
}
return &Creator{
CreatorID: claims["user_id"].(string),
CreatorRole: claims["role"].(string),
CreatorIP: r.RemoteAddr,
}
}
func (c *Creator) CreatedBy() (*User, error) {
var user User
if err := DB().First(&user, c.CreatorID).Error; err != nil {
return nil, err
}
return &user, nil
}

View file

@ -15,7 +15,8 @@ import (
type Participant struct { type Participant struct {
gorm.Model gorm.Model
*Creator *UserModifierCreate
*UserModifierUpdate
UserID uint UserID uint
@ -145,7 +146,7 @@ func (model *Participant) Create(args map[string]string, w http.ResponseWriter,
return nil, err return nil, err
} }
participant.Creator = NewCreator(r) participant.UserModifierCreate = NewUserModifierCreate(r)
participant, err = CreateParticipant(participant) participant, err = CreateParticipant(participant)
if err != nil { if err != nil {
@ -227,6 +228,8 @@ func (model *Participant) Update(args map[string]string, w http.ResponseWriter,
return nil, err return nil, err
} }
participant.(*School).UserModifierUpdate = NewUserModifierUpdate(r)
_, err = SaveParticipant(participant) _, err = SaveParticipant(participant)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -14,12 +14,13 @@ import (
type School struct { type School struct {
gorm.Model gorm.Model
*Creator *UserModifierCreate
*UserModifierUpdate
Name string Name string
Email string Email string
Code string Code string
EmailSentDate *time.Time EmailSentDate time.Time
UserID uint UserID uint
@ -118,7 +119,7 @@ func (model *School) Create(args map[string]string, w http.ResponseWriter, r *ht
return nil, err return nil, err
} }
school.Creator = NewCreator(r) school.UserModifierCreate = NewUserModifierCreate(r)
school, err = CreateSchool(school) school, err = CreateSchool(school)
if err != nil { if err != nil {
@ -188,6 +189,8 @@ func (model *School) Update(args map[string]string, w http.ResponseWriter, r *ht
return nil, err return nil, err
} }
school.(*School).UserModifierUpdate = NewUserModifierUpdate(r)
_, err = SaveSchool(school) _, err = SaveSchool(school)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -17,6 +17,11 @@ type User struct {
Role string Role string
} }
var (
SubscriberUser = User{Username: "subscriber", Role: "subscriber"}
AdministratorUser = User{Username: "admin", Role: "administrator"}
)
func (model *User) GetID() uint { return model.ID } func (model *User) GetID() uint { return model.ID }
func (model *User) String() string { func (model *User) String() string {

92
orm/useraction.go Normal file
View file

@ -0,0 +1,92 @@
package orm
import (
"net/http"
"github.com/dgrijalva/jwt-go"
)
type UserAction struct {
User
UserModifierCreate
UserModifierUpdate
}
type UserModifierCreate struct {
CreatorID string
CreatorRole string
CreatorIP string
}
type UserModifierUpdate struct {
UpdaterID string
UpdaterRole string
UpdaterIP string
}
func NewUserModifierCreate(r *http.Request) *UserModifierCreate {
var claims jwt.MapClaims
if r.Context().Value("user") != nil {
claims = r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
}
return &UserModifierCreate{
CreatorID: claims["user_id"].(string),
CreatorRole: claims["role"].(string),
CreatorIP: r.RemoteAddr,
}
}
func (um *UserModifierCreate) CreatedBy() (*UserAction, error) {
action := new(UserAction)
switch {
case (um.CreatorRole == "participant") || (um.CreatorRole == "school"):
if err := DB().First(&action.User, um.CreatorID).Error; err != nil {
return nil, err
}
case um.CreatorRole == "subscriber":
action.User = SubscriberUser
case um.CreatorRole == "administrator":
action.User = AdministratorUser
default:
return nil, nil
}
return action, nil
}
func NewUserModifierUpdate(r *http.Request) *UserModifierUpdate {
var claims jwt.MapClaims
if r.Context().Value("user") != nil {
claims = r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
}
return &UserModifierUpdate{
UpdaterID: claims["user_id"].(string),
UpdaterRole: claims["role"].(string),
UpdaterIP: r.RemoteAddr,
}
}
func (um *UserModifierUpdate) UpdatedBy() (*User, error) {
var user User
switch {
case (um.UpdaterRole == "participant") || (um.UpdaterRole == "school"):
if err := DB().First(&user, um.UpdaterID).Error; err != nil {
return nil, err
}
case um.UpdaterRole == "subscriber":
user = SubscriberUser
case um.UpdaterRole == "administrator":
user = AdministratorUser
default:
return nil, nil
}
return &user, nil
}

View file

@ -9,6 +9,7 @@ import (
"strings" "strings"
"time" "time"
"git.andreafazzi.eu/andrea/oef/i18n"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
"github.com/jinzhu/inflection" "github.com/jinzhu/inflection"
yml "gopkg.in/yaml.v2" yml "gopkg.in/yaml.v2"
@ -20,39 +21,40 @@ const (
var ( var (
funcMap = template.FuncMap{ funcMap = template.FuncMap{
"query": query, "query": query,
"convertDate": convertDate, "convertDate": convertDate,
"convertTime": convertTime, "convertTime": convertTime,
"prettyDate": prettyDate, "prettyDate": prettyDate,
"prettyTime": prettyTime, "prettyTime": prettyTime,
"modelPath": modelPath, "prettyDateTime": prettyDateTime,
"dict": dict, "modelPath": modelPath,
"yaml": yaml, "dict": dict,
"create": create, "yaml": yaml,
"update": update, "create": create,
"delete": delete, "update": update,
"show": show, "delete": delete,
"all": all, "show": show,
"execute": execute, "all": all,
"isSlice": isSlice, "execute": execute,
"toSlice": toSlice, "isSlice": isSlice,
"string": callString, "toSlice": toSlice,
"incr": incr, "string": callString,
"mod2": mod2, "incr": incr,
"toLower": toLower, "mod2": mod2,
"anchor": anchor, "toLower": toLower,
"html": html, "anchor": anchor,
"field": field, "html": html,
"modelName": modelName, "field": field,
"active": active, "modelName": modelName,
"pluralize": pluralize, "active": active,
"lower": lower, "pluralize": pluralize,
"trim": trim, "lower": lower,
"username": username, "trim": trim,
"isAdmin": isAdmin, "username": username,
"isParticipant": isParticipant, "isAdmin": isAdmin,
"isSubscriber": isSubscriber, "isParticipant": isParticipant,
"attr": attr, "isSubscriber": isSubscriber,
"attr": attr,
} }
) )
@ -216,7 +218,7 @@ func query(values ...string) template.URL {
} }
func convertDate(value interface{}) string { func convertDate(value interface{}) string {
t, ok := value.(*time.Time) t, ok := value.(time.Time)
if !ok { if !ok {
return "" return ""
} }
@ -224,15 +226,23 @@ func convertDate(value interface{}) string {
} }
func prettyDate(value interface{}) string { func prettyDate(value interface{}) string {
t, ok := value.(*time.Time) t, ok := value.(time.Time)
if !ok { if !ok {
return "" return ""
} }
return fmt.Sprintf("%02d/%02d/%d", t.Day(), t.Month(), t.Year()) return fmt.Sprintf("%02d/%02d/%d", t.Day(), t.Month(), t.Year())
} }
func prettyDateTime(value interface{}) string {
t, ok := value.(time.Time)
if !ok {
return ""
}
return fmt.Sprintf(i18n.Formats["dateTime"]["it"], t.Day(), t.Month(), t.Year(), t.Hour(), t.Minute())
}
func convertTime(value interface{}) string { func convertTime(value interface{}) string {
t, ok := value.(*time.Time) t, ok := value.(time.Time)
if !ok { if !ok {
return "" return ""
} }

View file

@ -11,12 +11,18 @@
<dd class="col-sm-9">{{.Data.User.Username}}</dd> <dd class="col-sm-9">{{.Data.User.Username}}</dd>
<dt class="col-sm-3">Password</dt> <dt class="col-sm-3">Password</dt>
<dd class="col-sm-9">{{.Data.User.Password}}</dd> <dd class="col-sm-9">{{.Data.User.Password}}</dd>
{{/*if $user:=.Data.CreatedBy*/}} {{if $creatorUser:=.Data.CreatedBy}}
<!-- <dt class="col-sm-3">Creato da</dt> --> <dt class="col-sm-3">Creato da</dt>
<!-- <dd class="col-sm-9">{{/*$user.Username*/}}</dd> --> <dd class="col-sm-9">{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}}</dd>
{{/*end*/}} <dt class="col-sm-3">IP di chi ha creato il partecipante</dt>
<dt class="col-sm-3">IP del creatore</dt>
<dd class="col-sm-9">{{.Data.CreatorIP}}</dd> <dd class="col-sm-9">{{.Data.CreatorIP}}</dd>
{{end}}
{{if $updaterUser:=.Data.UpdatedBy}}
<dt class="col-sm-3">Modificato da</dt>
<dd class="col-sm-9">{{$updaterUser.Username}}[{{$updaterUser.Role}}] {{$.Data.UpdatedAt|prettyDateTime}}</dd>
<dt class="col-sm-3">IP di chi ha modificato il partecipante</dt>
<dd class="col-sm-9">{{.Data.UpdaterIP}}</dd>
{{end}}
</dl> </dl>

View file

@ -39,6 +39,19 @@
<dd class="col-sm-9">{{.Data.User.Username}}</dd> <dd class="col-sm-9">{{.Data.User.Username}}</dd>
<dt class="col-sm-3">Password</dt> <dt class="col-sm-3">Password</dt>
<dd class="col-sm-9">{{.Data.User.Password}}</dd> <dd class="col-sm-9">{{.Data.User.Password}}</dd>
{{if $creatorUser:=.Data.CreatedBy}}
<dt class="col-sm-3">Creato da</dt>
<dd class="col-sm-9">{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}}</dd>
<dt class="col-sm-3">IP di chi ha creato la scuola</dt>
<dd class="col-sm-9">{{.Data.CreatorIP}}</dd>
{{end}}
{{if $updaterUser:=.Data.UpdatedBy}}
<dt class="col-sm-3">Modificato da</dt>
<dd class="col-sm-9">{{$updaterUser.Username}}[{{$updaterUser.Role}}] {{$.Data.UpdatedAt|prettyDateTime}}</dd>
<dt class="col-sm-3">IP di chi ha modificato la scuola</dt>
<dd class="col-sm-9">{{.Data.UpdaterIP}}</dd>
{{end}}
</dl> </dl>
<div class="row"> <div class="row">