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.",
},
}
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
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"`

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 {
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

View file

@ -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

View file

@ -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 {

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"
"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 ""
}

View file

@ -11,12 +11,18 @@
<dd class="col-sm-9">{{.Data.User.Username}}</dd>
<dt class="col-sm-3">Password</dt>
<dd class="col-sm-9">{{.Data.User.Password}}</dd>
{{/*if $user:=.Data.CreatedBy*/}}
<!-- <dt class="col-sm-3">Creato da</dt> -->
<!-- <dd class="col-sm-9">{{/*$user.Username*/}}</dd> -->
{{/*end*/}}
<dt class="col-sm-3">IP del creatore</dt>
{{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 il partecipante</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 il partecipante</dt>
<dd class="col-sm-9">{{.Data.UpdaterIP}}</dd>
{{end}}
</dl>

View file

@ -39,6 +39,19 @@
<dd class="col-sm-9">{{.Data.User.Username}}</dd>
<dt class="col-sm-3">Password</dt>
<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>
<div class="row">