Refactoring usermodifier
This commit is contained in:
parent
d027e84807
commit
f35410e0ae
10 changed files with 136 additions and 25 deletions
|
@ -16,9 +16,11 @@ func (e *Error) Error() string {
|
|||
}
|
||||
|
||||
var (
|
||||
RecordExists = errors.New("Record already exists!")
|
||||
RecordExists = errors.New("Record already exists!")
|
||||
|
||||
NotAuthorized = errors.New(i18n.Authorization["notAuthorized"]["it"])
|
||||
SchoolExists = &Error{
|
||||
|
||||
SchoolExists = &Error{
|
||||
TemplateName: "error_school_exists",
|
||||
Err: errors.New(i18n.Errors["schoolExists"]["it"]),
|
||||
}
|
||||
|
@ -27,8 +29,14 @@ var (
|
|||
TemplateName: "error_category_exists",
|
||||
Err: errors.New(i18n.FlashMessages["categoryExists"]["it"]),
|
||||
}
|
||||
|
||||
OutOfTime = &Error{
|
||||
TemplateName: "error_out_of_time",
|
||||
Err: errors.New(i18n.Errors["outOfTime"]["it"]),
|
||||
}
|
||||
|
||||
ContestHasZeroQuestions = &Error{
|
||||
TemplateName: "error_contest_has_zero_questions",
|
||||
Err: errors.New(i18n.Errors["contestHasZeroQuestions"]["it"]),
|
||||
}
|
||||
)
|
||||
|
|
|
@ -120,10 +120,10 @@ CREATE TABLE `participants` (
|
|||
`created_at` timestamp NULL DEFAULT NULL,
|
||||
`updated_at` timestamp NULL DEFAULT NULL,
|
||||
`deleted_at` timestamp NULL DEFAULT NULL,
|
||||
`creator_id` varchar(255) DEFAULT NULL,
|
||||
`creator_id` int(10) unsigned DEFAULT NULL,
|
||||
`creator_role` varchar(255) DEFAULT NULL,
|
||||
`creator_ip` varchar(255) DEFAULT NULL,
|
||||
`updater_id` varchar(255) DEFAULT NULL,
|
||||
`updater_id` int(10) unsigned DEFAULT NULL,
|
||||
`updater_role` varchar(255) DEFAULT NULL,
|
||||
`updater_ip` varchar(255) DEFAULT NULL,
|
||||
`user_id` int(10) unsigned DEFAULT NULL,
|
||||
|
@ -216,10 +216,10 @@ CREATE TABLE `responses` (
|
|||
`created_at` timestamp NULL DEFAULT NULL,
|
||||
`updated_at` timestamp NULL DEFAULT NULL,
|
||||
`deleted_at` timestamp NULL DEFAULT NULL,
|
||||
`creator_id` varchar(255) DEFAULT NULL,
|
||||
`creator_id` int(10) unsigned DEFAULT NULL,
|
||||
`creator_role` varchar(255) DEFAULT NULL,
|
||||
`creator_ip` varchar(255) DEFAULT NULL,
|
||||
`updater_id` varchar(255) DEFAULT NULL,
|
||||
`updater_id` int(10) unsigned DEFAULT NULL,
|
||||
`updater_role` varchar(255) DEFAULT NULL,
|
||||
`updater_ip` varchar(255) DEFAULT NULL,
|
||||
`name` varchar(255) DEFAULT NULL,
|
||||
|
@ -257,10 +257,10 @@ CREATE TABLE `schools` (
|
|||
`created_at` timestamp NULL DEFAULT NULL,
|
||||
`updated_at` timestamp NULL DEFAULT NULL,
|
||||
`deleted_at` timestamp NULL DEFAULT NULL,
|
||||
`creator_id` varchar(255) DEFAULT NULL,
|
||||
`creator_id` int(10) unsigned DEFAULT NULL,
|
||||
`creator_role` varchar(255) DEFAULT NULL,
|
||||
`creator_ip` varchar(255) DEFAULT NULL,
|
||||
`updater_id` varchar(255) DEFAULT NULL,
|
||||
`updater_id` int(10) unsigned DEFAULT NULL,
|
||||
`updater_role` varchar(255) DEFAULT NULL,
|
||||
`updater_ip` varchar(255) DEFAULT NULL,
|
||||
`name` varchar(255) DEFAULT NULL,
|
||||
|
|
|
@ -371,6 +371,7 @@ func (t *testSuite) TestSchoolSubscription() {
|
|||
|
||||
t.Equal(http.StatusSeeOther, rr.Code)
|
||||
|
||||
log.Println(rr.Header())
|
||||
schoolId, err := getIdFromPath(rr.Header()["Location"][0])
|
||||
if !t.Failed() {
|
||||
doc, err := goquery.NewDocumentFromReader(rr.Body)
|
||||
|
@ -454,13 +455,14 @@ func (t *testSuite) TestUserModifier() {
|
|||
t.Equal(http.StatusOK, rr.Code)
|
||||
|
||||
if !t.Failed() {
|
||||
fmt.Println(rr.Body)
|
||||
doc, err := goquery.NewDocumentFromReader(rr.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
expected := "Creato da"
|
||||
expected := "admin[administrator]"
|
||||
ok := false
|
||||
doc.Find("dt").Each(func(i int, s *goquery.Selection) {
|
||||
doc.Find("dd").Each(func(i int, s *goquery.Selection) {
|
||||
if strings.Contains(s.Text(), expected) {
|
||||
ok = true
|
||||
}
|
||||
|
|
|
@ -47,5 +47,8 @@ var (
|
|||
"schoolExists": map[string]string{
|
||||
"it": "Una scuola con questo codice meccanografico è già presente nella base dati!",
|
||||
},
|
||||
"contestHasZeroQuestions": map[string]string{
|
||||
"it": "La gara a cui il partecipante è iscritto non contiene alcuna domanda.",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.andreafazzi.eu/andrea/oef/errors"
|
||||
"git.andreafazzi.eu/andrea/oef/renderer"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/jinzhu/gorm"
|
||||
|
@ -92,7 +93,7 @@ func (c *Contest) ReadAll(db *Database, args map[string]string, w http.ResponseW
|
|||
var contests []*Contest
|
||||
|
||||
claims := r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
|
||||
|
||||
|
||||
if claims["admin"].(bool) {
|
||||
if err := db._db.Order("created_at").Find(&contests).Error; err != nil {
|
||||
return nil, err
|
||||
|
@ -199,6 +200,10 @@ func (c *Contest) generateQuestionsOrder(db *gorm.DB) (string, error) {
|
|||
return "", err
|
||||
}
|
||||
|
||||
if len(questions) == 0 {
|
||||
return "", errors.ContestHasZeroQuestions
|
||||
}
|
||||
|
||||
count := 0
|
||||
generated := make(map[int]bool, 0)
|
||||
|
||||
|
|
|
@ -78,6 +78,30 @@ func (model *Participant) String() string {
|
|||
return fmt.Sprintf("%s %s", strings.ToUpper(model.Lastname), strings.Title(strings.ToLower(model.Firstname)))
|
||||
}
|
||||
|
||||
func (model *Participant) SetCreatorID(id uint) {
|
||||
model.CreatorID = id
|
||||
}
|
||||
|
||||
func (model *Participant) SetCreatorRole(role string) {
|
||||
model.CreatorRole = role
|
||||
}
|
||||
|
||||
func (model *Participant) SetCreatorIP(addr string) {
|
||||
model.CreatorIP = addr
|
||||
}
|
||||
|
||||
func (model *Participant) SetUpdaterID(id uint) {
|
||||
model.UpdaterID = id
|
||||
}
|
||||
|
||||
func (model *Participant) SetUpdaterRole(role string) {
|
||||
model.UpdaterRole = role
|
||||
}
|
||||
|
||||
func (model *Participant) SetUpdaterIP(addr string) {
|
||||
model.UpdaterIP = addr
|
||||
}
|
||||
|
||||
// func setFlashMessage(w http.ResponseWriter, r *http.Request, key string) error {
|
||||
// session, err := store.Get(r, "flash-session")
|
||||
// if err != nil {
|
||||
|
@ -210,7 +234,7 @@ func (model *Participant) Create(db *Database, args map[string]string, w http.Re
|
|||
return nil, errors.CategoryExists
|
||||
}
|
||||
|
||||
participant.CreatorID = getUserIDFromTokenAsUint(r)
|
||||
WriteCreator(r, participant)
|
||||
|
||||
participant, err = CreateParticipant(db, participant)
|
||||
if err != nil {
|
||||
|
@ -362,11 +386,11 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re
|
|||
return nil, errors.CategoryExists
|
||||
}
|
||||
|
||||
if err := db._db.Where(participant.(*Participant).ContestIDs).Find(&participant.(*Participant).Contests).Error; err != nil {
|
||||
if err := db._db.Where([]uint(participant.(*Participant).ContestIDs)).Find(&participant.(*Participant).Contests).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
participant.(*Participant).UpdaterID = getUserIDFromTokenAsUint(r)
|
||||
WriteUpdater(r, participant.(*Participant))
|
||||
|
||||
_, err = SaveParticipant(db, participant)
|
||||
if err != nil {
|
||||
|
|
|
@ -63,6 +63,30 @@ func (model *Response) IsActive() bool {
|
|||
return !time.Now().After(model.Contest.EndTime) || model.Contest.isAlwaysActive()
|
||||
}
|
||||
|
||||
func (model *Response) SetCreatorID(id uint) {
|
||||
model.CreatorID = id
|
||||
}
|
||||
|
||||
func (model *Response) SetCreatorRole(role string) {
|
||||
model.CreatorRole = role
|
||||
}
|
||||
|
||||
func (model *Response) SetCreatorIP(addr string) {
|
||||
model.CreatorIP = addr
|
||||
}
|
||||
|
||||
func (model *Response) SetUpdaterID(id uint) {
|
||||
model.UpdaterID = id
|
||||
}
|
||||
|
||||
func (model *Response) SetUpdaterRole(role string) {
|
||||
model.UpdaterRole = role
|
||||
}
|
||||
|
||||
func (model *Response) SetUpdaterIP(addr string) {
|
||||
model.UpdaterIP = addr
|
||||
}
|
||||
|
||||
func (model *Response) generateAnswersIDs() string {
|
||||
ids := make([]string, 0)
|
||||
for _, sr := range model.SingleResponses {
|
||||
|
@ -93,7 +117,7 @@ func (model *Response) Create(db *Database, args map[string]string, w http.Respo
|
|||
return nil, err
|
||||
}
|
||||
|
||||
response.CreatorID = getUserIDFromTokenAsUint(r)
|
||||
WriteCreator(r, response)
|
||||
|
||||
response, err = CreateResponse(db, response)
|
||||
if err != nil {
|
||||
|
@ -210,7 +234,7 @@ func (model *Response) Update(db *Database, args map[string]string, w http.Respo
|
|||
return nil, err
|
||||
}
|
||||
|
||||
response.(*Response).UpdaterID = getUserIDFromTokenAsUint(r)
|
||||
WriteUpdater(r, response.(*Response))
|
||||
|
||||
_, err = SaveResponse(db, response)
|
||||
if err != nil {
|
||||
|
|
|
@ -30,6 +30,8 @@ https://iscrizioni.olimpiadi-economiaefinanza.it/
|
|||
|
||||
ed inserire le credenziali riportate sopra (si consiglia di effettuare un copia/incolla).
|
||||
|
||||
Questa mail è stata generata da un sistema automatico. Si prega di non rispondere.
|
||||
|
||||
Cordialmente,
|
||||
Lo Staff delle OEF 2020.
|
||||
`
|
||||
|
@ -91,6 +93,30 @@ func (model *School) To() string {
|
|||
return model.Email
|
||||
}
|
||||
|
||||
func (model *School) SetCreatorID(id uint) {
|
||||
model.CreatorID = id
|
||||
}
|
||||
|
||||
func (model *School) SetCreatorRole(role string) {
|
||||
model.CreatorRole = role
|
||||
}
|
||||
|
||||
func (model *School) SetCreatorIP(addr string) {
|
||||
model.CreatorIP = addr
|
||||
}
|
||||
|
||||
func (model *School) SetUpdaterID(id uint) {
|
||||
model.UpdaterID = id
|
||||
}
|
||||
|
||||
func (model *School) SetUpdaterRole(role string) {
|
||||
model.UpdaterRole = role
|
||||
}
|
||||
|
||||
func (model *School) SetUpdaterIP(addr string) {
|
||||
model.UpdaterIP = addr
|
||||
}
|
||||
|
||||
func (model *School) exists(db *Database) (*User, error) {
|
||||
var user User
|
||||
if err := db._db.First(&user, &User{Username: model.Username()}).Error; err != nil && err != gorm.ErrRecordNotFound {
|
||||
|
@ -165,9 +191,7 @@ func (model *School) Create(db *Database, args map[string]string, w http.Respons
|
|||
return nil, err
|
||||
}
|
||||
|
||||
school.CreatorID = getUserIDFromTokenAsUint(r)
|
||||
school.CreatorRole = getUserRole(r)
|
||||
school.CreatorIP = r.RemoteAddr
|
||||
WriteCreator(r, school)
|
||||
|
||||
school.mailSender = mail.NewMailSender(db.Config, mailBody)
|
||||
school, err = CreateSchool(db, school)
|
||||
|
@ -238,12 +262,7 @@ func (model *School) Update(db *Database, args map[string]string, w http.Respons
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// FIXME
|
||||
// WriteCreator(school.(*School))
|
||||
|
||||
school.(*School).UpdaterID = getUserIDFromTokenAsUint(r)
|
||||
school.(*School).UpdaterRole = getUserRole(r)
|
||||
school.(*School).UpdaterIP = r.RemoteAddr
|
||||
WriteUpdater(r, school.(*School))
|
||||
|
||||
_, err = SaveSchool(db, school)
|
||||
if err != nil {
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package orm
|
||||
|
||||
import "net/http"
|
||||
|
||||
type Modifier interface {
|
||||
SetCreatorID(id uint)
|
||||
SetUpdaterID(id uint)
|
||||
|
@ -45,3 +47,16 @@ func (um UserModifier) UpdatedBy() *User {
|
|||
|
||||
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)
|
||||
}
|
||||
|
|
11
templates/error_contest_has_zero_questions.html.tpl
Normal file
11
templates/error_contest_has_zero_questions.html.tpl
Normal file
|
@ -0,0 +1,11 @@
|
|||
{{ define "content" }}
|
||||
<div class="container">
|
||||
<h1 class="border-bottom">Errore</h1>
|
||||
<p>
|
||||
Si è verificato un errore durante la creazione o l'aggiornamento di un partecipante: <strong>{{.Data}}</strong>
|
||||
</p>
|
||||
<p>
|
||||
Clicca {{all "Contest"|anchor "qui"}} per tornare all'elenco delle gare.
|
||||
</p>
|
||||
</div>
|
||||
{{ end }}
|
Loading…
Reference in a new issue