token-validator: record logins
This commit is contained in:
parent
0fd38832bf
commit
843aac8d54
@ -58,12 +58,23 @@ CREATE TABLE IF NOT EXISTS students(
|
|||||||
login VARCHAR(255) NOT NULL UNIQUE,
|
login VARCHAR(255) NOT NULL UNIQUE,
|
||||||
time TIMESTAMP NOT NULL
|
time TIMESTAMP NOT NULL
|
||||||
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
||||||
|
`); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err = db.Exec(`
|
||||||
|
CREATE TABLE IF NOT EXISTS student_keys(
|
||||||
|
id_key INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
id_student INTEGER NOT NULL,
|
||||||
|
sshkey TEXT,
|
||||||
|
time TIMESTAMP NOT NULL,
|
||||||
|
FOREIGN KEY(id_student) REFERENCES students(id_student)
|
||||||
|
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
||||||
`); err != nil {
|
`); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, err := db.Exec(`
|
if _, err := db.Exec(`
|
||||||
CREATE TABLE IF NOT EXISTS student_login(
|
CREATE TABLE IF NOT EXISTS student_login(
|
||||||
id_student INTEGER,
|
id_student INTEGER NOT NULL,
|
||||||
ip VARCHAR(255) NOT NULL,
|
ip VARCHAR(255) NOT NULL,
|
||||||
mac VARCHAR(255) NOT NULL,
|
mac VARCHAR(255) NOT NULL,
|
||||||
time TIMESTAMP NOT NULL,
|
time TIMESTAMP NOT NULL,
|
||||||
|
@ -47,6 +47,12 @@ type Student struct {
|
|||||||
Time time.Time `json:"time"`
|
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) {
|
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, time FROM students"); errr != nil {
|
||||||
return nil, errr
|
return nil, errr
|
||||||
@ -129,16 +135,20 @@ func ClearStudents() (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createStudent(_ httprouter.Params, body []byte) (interface{}, error) {
|
func createStudent(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||||
var std Student
|
var err error
|
||||||
if err := json.Unmarshal(body, &std); err != nil {
|
var std uploadedStudent
|
||||||
|
if err = json.Unmarshal(body, &std); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if exist, err := getStudentByLogin(strings.TrimSpace(std.Login)); err != nil {
|
var exist Student
|
||||||
return NewStudent(strings.TrimSpace(std.Login))
|
if exist, err = getStudentByLogin(strings.TrimSpace(std.Login)); err != nil {
|
||||||
} else {
|
if exist, err = NewStudent(strings.TrimSpace(std.Login)); err != nil {
|
||||||
return exist, nil
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
exist.registerAccess(std.IP, std.MAC)
|
||||||
|
return exist, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateStudent(current Student, body []byte) (interface{}, error) {
|
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
|
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 New Issue
Block a user