token-validator: record logins
This commit is contained in:
parent
0fd38832bf
commit
843aac8d54
2 changed files with 38 additions and 7 deletions
|
|
@ -47,6 +47,12 @@ type Student struct {
|
|||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
type uploadedStudent struct {
|
||||
Login string `json:"login"`
|
||||
IP string `json:"ip"`
|
||||
MAC string `json:"mac"`
|
||||
}
|
||||
|
||||
func getStudents() (students []Student, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_student, login, time FROM students"); errr != nil {
|
||||
return nil, errr
|
||||
|
|
@ -129,16 +135,20 @@ func ClearStudents() (int64, error) {
|
|||
}
|
||||
|
||||
func createStudent(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
var std Student
|
||||
if err := json.Unmarshal(body, &std); err != nil {
|
||||
var err error
|
||||
var std uploadedStudent
|
||||
if err = json.Unmarshal(body, &std); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if exist, err := getStudentByLogin(strings.TrimSpace(std.Login)); err != nil {
|
||||
return NewStudent(strings.TrimSpace(std.Login))
|
||||
} else {
|
||||
return exist, nil
|
||||
var exist Student
|
||||
if exist, err = getStudentByLogin(strings.TrimSpace(std.Login)); err != nil {
|
||||
if exist, err = NewStudent(strings.TrimSpace(std.Login)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
exist.registerAccess(std.IP, std.MAC)
|
||||
return exist, nil
|
||||
}
|
||||
|
||||
func updateStudent(current Student, body []byte) (interface{}, error) {
|
||||
|
|
@ -190,3 +200,13 @@ func (s Student) UnlockNewChallenge(challenge int, value string) (UnlockedChalle
|
|||
return UnlockedChallenge{utid, s.Id, challenge, time.Now()}, err
|
||||
}
|
||||
}
|
||||
|
||||
func (s Student) registerAccess(ip, mac string) error {
|
||||
if res, err := DBExec("INSERT INTO student_login (id_student, ip, mac, time) VALUES (?, ?, ?, ?)", s.Id, ip, mac, time.Now()); err != nil {
|
||||
return err
|
||||
} else if _, err := res.LastInsertId(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue