login-validator: provide latest IP
This commit is contained in:
parent
f3249cc9ed
commit
87599668b9
|
@ -172,7 +172,7 @@ func (l loginChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (l loginChecker) registerUser(username, remoteAddr string) error {
|
||||
bts, err := json.Marshal(map[string]interface{}{"login": username})
|
||||
bts, err := json.Marshal(map[string]interface{}{"login": username, "lastip": remoteAddr})
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -56,6 +56,7 @@ func DBCreate() (err error) {
|
|||
CREATE TABLE IF NOT EXISTS students(
|
||||
id_student INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
login VARCHAR(255) NOT NULL UNIQUE,
|
||||
lastip VARCHAR(255) NOT NULL,
|
||||
time TIMESTAMP NOT NULL
|
||||
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
||||
`); err != nil {
|
||||
|
|
|
@ -28,20 +28,21 @@ func init() {
|
|||
}
|
||||
|
||||
type Student struct {
|
||||
Id int64 `json:"id"`
|
||||
Login string `json:"login"`
|
||||
Time time.Time `json:"time"`
|
||||
Id int64 `json:"id"`
|
||||
Login string `json:"login"`
|
||||
LastIP string `json:"lastip"`
|
||||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
func getStudents() (students []Student, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_student, login, time FROM students"); errr != nil {
|
||||
if rows, errr := DBQuery("SELECT id_student, login, lastip, time FROM students"); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var s Student
|
||||
if err = rows.Scan(&s.Id, &s.Login, &s.Time); err != nil {
|
||||
if err = rows.Scan(&s.Id, &s.Login, &s.LastIP, &s.Time); err != nil {
|
||||
return
|
||||
}
|
||||
students = append(students, s)
|
||||
|
@ -55,12 +56,12 @@ func getStudents() (students []Student, err error) {
|
|||
}
|
||||
|
||||
func getStudent(id int) (s Student, err error) {
|
||||
err = DBQueryRow("SELECT id_student, login, time FROM students WHERE id_student=?", id).Scan(&s.Id, &s.Login, &s.Time)
|
||||
err = DBQueryRow("SELECT id_student, login, lastip, time FROM students WHERE id_student=?", id).Scan(&s.Id, &s.Login, &s.LastIP, &s.Time)
|
||||
return
|
||||
}
|
||||
|
||||
func getStudentByLogin(login string) (s Student, err error) {
|
||||
err = DBQueryRow("SELECT id_student, login, time FROM students WHERE login=?", login).Scan(&s.Id, &s.Login, &s.Time)
|
||||
err = DBQueryRow("SELECT id_student, login, lastip, time FROM students WHERE login=?", login).Scan(&s.Id, &s.Login, &s.LastIP, &s.Time)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -70,13 +71,13 @@ func studentExists(login string) bool {
|
|||
return err != nil && z == 1
|
||||
}
|
||||
|
||||
func NewStudent(login string) (Student, error) {
|
||||
if res, err := DBExec("INSERT INTO students (login, time) VALUES (?, ?)", login, time.Now()); err != nil {
|
||||
func NewStudent(login string, lastip string) (Student, error) {
|
||||
if res, err := DBExec("INSERT INTO students (login, lastip, time) VALUES (?, ?, ?)", login, lastip, time.Now()); err != nil {
|
||||
return Student{}, err
|
||||
} else if sid, err := res.LastInsertId(); err != nil {
|
||||
return Student{}, err
|
||||
} else {
|
||||
return Student{sid, login, time.Now()}, nil
|
||||
return Student{sid, login, lastip, time.Now()}, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,7 +86,7 @@ func (s Student) GetPKey() []byte {
|
|||
}
|
||||
|
||||
func (s Student) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE students SET login = ?, time = ? WHERE id_student = ?", s.Login, s.Time, s.Id); err != nil {
|
||||
if res, err := DBExec("UPDATE students SET login = ?, lastip = ?, time = ? WHERE id_student = ?", s.Login, s.LastIP, s.Time, s.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
|
@ -121,7 +122,11 @@ func createStudent(_ httprouter.Params, body []byte) (interface{}, error) {
|
|||
}
|
||||
|
||||
if exist, err := getStudentByLogin(strings.TrimSpace(std.Login)); err != nil {
|
||||
return NewStudent(strings.TrimSpace(std.Login))
|
||||
return NewStudent(strings.TrimSpace(std.Login), strings.TrimSpace(std.LastIP))
|
||||
} else if exist.LastIP != std.LastIP {
|
||||
exist.LastIP = std.LastIP
|
||||
exist.Update()
|
||||
return exist, nil
|
||||
} else {
|
||||
return exist, nil
|
||||
}
|
||||
|
|
Reference in New Issue
Block a user