diff --git a/i18n/i18n.go b/i18n/i18n.go index 7e17f4b8..c893bd22 100644 --- a/i18n/i18n.go +++ b/i18n/i18n.go @@ -12,4 +12,9 @@ var ( "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", + }, + } ) diff --git a/orm/contest.go b/orm/contest.go index 75e449cc..366da332 100644 --- a/orm/contest.go +++ b/orm/contest.go @@ -17,9 +17,9 @@ type Contest struct { Name string Category string - Date *time.Time - StartTime *time.Time - EndTime *time.Time + Date time.Time + StartTime time.Time + EndTime time.Time Questions []*Question Participants []*Participant `gorm:"many2many:subscriptions"` diff --git a/orm/creator.go b/orm/creator.go deleted file mode 100644 index 537c9152..00000000 --- a/orm/creator.go +++ /dev/null @@ -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 -} diff --git a/orm/participant.go b/orm/participant.go index 36e42f7a..2d69f3fe 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -15,7 +15,8 @@ import ( type Participant struct { gorm.Model - *Creator + *UserModifierCreate + *UserModifierUpdate UserID uint @@ -145,7 +146,7 @@ func (model *Participant) Create(args map[string]string, w http.ResponseWriter, return nil, err } - participant.Creator = NewCreator(r) + participant.UserModifierCreate = NewUserModifierCreate(r) participant, err = CreateParticipant(participant) if err != nil { @@ -227,6 +228,8 @@ func (model *Participant) Update(args map[string]string, w http.ResponseWriter, return nil, err } + participant.(*School).UserModifierUpdate = NewUserModifierUpdate(r) + _, err = SaveParticipant(participant) if err != nil { return nil, err diff --git a/orm/school.go b/orm/school.go index 26e9d6e3..2d863c1d 100644 --- a/orm/school.go +++ b/orm/school.go @@ -14,12 +14,13 @@ import ( type School struct { gorm.Model - *Creator + *UserModifierCreate + *UserModifierUpdate Name string Email string Code string - EmailSentDate *time.Time + EmailSentDate time.Time UserID uint @@ -118,7 +119,7 @@ func (model *School) Create(args map[string]string, w http.ResponseWriter, r *ht return nil, err } - school.Creator = NewCreator(r) + school.UserModifierCreate = NewUserModifierCreate(r) school, err = CreateSchool(school) if err != nil { @@ -188,6 +189,8 @@ func (model *School) Update(args map[string]string, w http.ResponseWriter, r *ht return nil, err } + school.(*School).UserModifierUpdate = NewUserModifierUpdate(r) + _, err = SaveSchool(school) if err != nil { return nil, err diff --git a/orm/user.go b/orm/user.go index b99b967b..d646eacb 100644 --- a/orm/user.go +++ b/orm/user.go @@ -17,6 +17,11 @@ type User struct { 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) String() string { diff --git a/orm/useraction.go b/orm/useraction.go new file mode 100644 index 00000000..fe8abb99 --- /dev/null +++ b/orm/useraction.go @@ -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 +} diff --git a/renderer/funcmap.go b/renderer/funcmap.go index 51e0288a..05012702 100644 --- a/renderer/funcmap.go +++ b/renderer/funcmap.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "git.andreafazzi.eu/andrea/oef/i18n" jwt "github.com/dgrijalva/jwt-go" "github.com/jinzhu/inflection" yml "gopkg.in/yaml.v2" @@ -20,39 +21,40 @@ const ( var ( funcMap = template.FuncMap{ - "query": query, - "convertDate": convertDate, - "convertTime": convertTime, - "prettyDate": prettyDate, - "prettyTime": prettyTime, - "modelPath": modelPath, - "dict": dict, - "yaml": yaml, - "create": create, - "update": update, - "delete": delete, - "show": show, - "all": all, - "execute": execute, - "isSlice": isSlice, - "toSlice": toSlice, - "string": callString, - "incr": incr, - "mod2": mod2, - "toLower": toLower, - "anchor": anchor, - "html": html, - "field": field, - "modelName": modelName, - "active": active, - "pluralize": pluralize, - "lower": lower, - "trim": trim, - "username": username, - "isAdmin": isAdmin, - "isParticipant": isParticipant, - "isSubscriber": isSubscriber, - "attr": attr, + "query": query, + "convertDate": convertDate, + "convertTime": convertTime, + "prettyDate": prettyDate, + "prettyTime": prettyTime, + "prettyDateTime": prettyDateTime, + "modelPath": modelPath, + "dict": dict, + "yaml": yaml, + "create": create, + "update": update, + "delete": delete, + "show": show, + "all": all, + "execute": execute, + "isSlice": isSlice, + "toSlice": toSlice, + "string": callString, + "incr": incr, + "mod2": mod2, + "toLower": toLower, + "anchor": anchor, + "html": html, + "field": field, + "modelName": modelName, + "active": active, + "pluralize": pluralize, + "lower": lower, + "trim": trim, + "username": username, + "isAdmin": isAdmin, + "isParticipant": isParticipant, + "isSubscriber": isSubscriber, + "attr": attr, } ) @@ -216,7 +218,7 @@ func query(values ...string) template.URL { } func convertDate(value interface{}) string { - t, ok := value.(*time.Time) + t, ok := value.(time.Time) if !ok { return "" } @@ -224,15 +226,23 @@ func convertDate(value interface{}) string { } func prettyDate(value interface{}) string { - t, ok := value.(*time.Time) + t, ok := value.(time.Time) if !ok { return "" } 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 { - t, ok := value.(*time.Time) + t, ok := value.(time.Time) if !ok { return "" } diff --git a/templates/participants_show.html.tpl b/templates/participants_show.html.tpl index b56e50e3..efdc2858 100644 --- a/templates/participants_show.html.tpl +++ b/templates/participants_show.html.tpl @@ -11,12 +11,18 @@
{{.Data.User.Username}}
Password
{{.Data.User.Password}}
- {{/*if $user:=.Data.CreatedBy*/}} - - - {{/*end*/}} -
IP del creatore
+ {{if $creatorUser:=.Data.CreatedBy}} +
Creato da
+
{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}}
+
IP di chi ha creato il partecipante
{{.Data.CreatorIP}}
+ {{end}} + {{if $updaterUser:=.Data.UpdatedBy}} +
Modificato da
+
{{$updaterUser.Username}}[{{$updaterUser.Role}}] {{$.Data.UpdatedAt|prettyDateTime}}
+
IP di chi ha modificato il partecipante
+
{{.Data.UpdaterIP}}
+ {{end}} diff --git a/templates/schools_show.html.tpl b/templates/schools_show.html.tpl index 8a2be46e..ec54f34a 100644 --- a/templates/schools_show.html.tpl +++ b/templates/schools_show.html.tpl @@ -39,6 +39,19 @@
{{.Data.User.Username}}
Password
{{.Data.User.Password}}
+ {{if $creatorUser:=.Data.CreatedBy}} +
Creato da
+
{{$creatorUser.Username}}[{{$creatorUser.Role}}] {{$.Data.CreatedAt|prettyDateTime}}
+
IP di chi ha creato la scuola
+
{{.Data.CreatorIP}}
+ {{end}} + {{if $updaterUser:=.Data.UpdatedBy}} +
Modificato da
+
{{$updaterUser.Username}}[{{$updaterUser.Role}}] {{$.Data.UpdatedAt|prettyDateTime}}
+
IP di chi ha modificato la scuola
+
{{.Data.UpdaterIP}}
+ {{end}} +