-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsignup.go
91 lines (81 loc) · 2.55 KB
/
signup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package controllers
import (
"log"
"net/http"
"regexp"
"strings"
"github.com/UniversityRadioYork/2016-site/models"
"github.com/UniversityRadioYork/2016-site/structs"
"github.com/UniversityRadioYork/2016-site/utils"
"github.com/UniversityRadioYork/myradio-go"
)
// SignUpController is the controller for processing signup requests.
type SignUpController struct {
Controller
}
// NewSignUpController returns a new SignUpController with the MyRadio
// session s and configuration context c.
func NewSignUpController(s *myradio.Session, c *structs.Config) *SignUpController {
return &SignUpController{Controller{session: s, config: c}}
}
// Post handles the HTTP POST request r for the get involved, writing to w.
func (gic *SignUpController) Post(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
formParams := r.Form
var feedback []string
//Validate that necessary params are present and correct(enough)
_, ok := formParams["fname"]
if !ok || formParams["fname"][0] == "" {
feedback = append(feedback, "You need to provide your First Name")
}
_, ok = formParams["sname"]
if !ok || formParams["sname"][0] == "" {
feedback = append(feedback, "You need to provide your Last Name")
}
// Check an eduroam value is submitted
// If not then the user is signing up using a personal email
if _, ok := formParams["eduroam"]; ok {
eduroam := formParams["eduroam"][0]
if eduroam == "" {
feedback = append(feedback, "You need to provide your York Email")
} else {
// Ignore an added @york.ac.uk (since we assume it)
if strings.HasSuffix(eduroam, "@york.ac.uk"){
eduroam = eduroam[:len(eduroam)-11]
}
match, _ := regexp.MatchString("^[a-z]{1,6}[0-9]{1,6}$", eduroam)
if !match {
feedback = append(feedback, "The @york.ac.uk email you provided seems invalid")
}
formParams["eduroam"][0] = eduroam
}
} else {
if _, ok = formParams["email"]; !ok {
feedback = append(feedback, "You need to provide your email address")
}
}
_, ok = formParams["phone"]
if !ok || formParams["phone"][0] == "" {
delete(formParams, "phone")
}
//If they are then post them off to the API
if len(feedback) == 0 {
sm := models.NewSignUpModel(gic.session)
err := sm.Post(formParams)
if err != nil {
log.Println(err)
feedback = append(feedback, "Oops. Something went wrong on our end.")
feedback = append(feedback, "Please try again later")
}
}
data := struct {
Feedback []string
}{
Feedback: feedback,
}
err := utils.RenderTemplate(w, gic.config.PageContext, data, "signedup.tmpl")
if err != nil {
log.Println(err)
return
}
}