diff --git a/errors/errors.go b/errors/errors.go index 33063a4b..77e44dd3 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -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"]), + } ) diff --git a/handlers/compose/sql/oef_test.sql b/handlers/compose/sql/oef_test.sql index 354456ab..e80c8f08 100644 --- a/handlers/compose/sql/oef_test.sql +++ b/handlers/compose/sql/oef_test.sql @@ -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, diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index 8fa02ca1..a8421c42 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -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 } diff --git a/i18n/i18n.go b/i18n/i18n.go index b805c615..9f22c08e 100644 --- a/i18n/i18n.go +++ b/i18n/i18n.go @@ -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.", + }, } ) diff --git a/orm/contest.go b/orm/contest.go index 8530de45..1e09002c 100644 --- a/orm/contest.go +++ b/orm/contest.go @@ -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) diff --git a/orm/participant.go b/orm/participant.go index fb23ef5f..c9f81b12 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -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 { diff --git a/orm/response.go b/orm/response.go index 4e48457a..3a71d143 100644 --- a/orm/response.go +++ b/orm/response.go @@ -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 { diff --git a/orm/school.go b/orm/school.go index 88801b79..78777406 100644 --- a/orm/school.go +++ b/orm/school.go @@ -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 { diff --git a/orm/usermodifier.go b/orm/usermodifier.go index f90df8c8..c678af32 100644 --- a/orm/usermodifier.go +++ b/orm/usermodifier.go @@ -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) +} diff --git a/templates/error_contest_has_zero_questions.html.tpl b/templates/error_contest_has_zero_questions.html.tpl new file mode 100644 index 00000000..f1c949c4 --- /dev/null +++ b/templates/error_contest_has_zero_questions.html.tpl @@ -0,0 +1,11 @@ +{{ define "content" }} +
+

Errore

+

+ Si è verificato un errore durante la creazione o l'aggiornamento di un partecipante: {{.Data}} +

+

+ Clicca {{all "Contest"|anchor "qui"}} per tornare all'elenco delle gare. +

+
+{{ end }}