Browse Source

增加用户登录日志信息

Signed-off-by: zouap <zouap@pcl.ac.cn>
tags/v1.21.12.1
zouap 4 years ago
parent
commit
e52e09cfd5
3 changed files with 40 additions and 6 deletions
  1. +1
    -0
      models/models.go
  2. +31
    -0
      models/user_login_log.go
  3. +8
    -6
      routers/user/auth.go

+ 1
- 0
models/models.go View File

@@ -139,6 +139,7 @@ func init() {
new(RepoStatistic),
new(SummaryStatistic),
new(UserBusinessAnalysis),
new(UserLoginLog),
)

gonicNames := []string{"SSL", "UID"}


+ 31
- 0
models/user_login_log.go View File

@@ -0,0 +1,31 @@
package models

import (
"fmt"
"time"
"net/http"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
)

type UserLoginLog struct {
ID int64 `xorm:"pk autoincr"`
uid int64 `xorm:"NOT NULL"`
ipAddr string `xorm:"default NULL"`
CreatedUnix timeutil.TimeStamp `xorm:"created"`
}

func SaveLoginInfoToDb(r *http.Request,u User){


}


func getIP(r *http.Request) string {    
forwarded := r.Header.Get("X-FORWARDED-FOR")    
if forwarded != "" {        
return forwarded    
}    
return r.RemoteAddr
}

+ 8
- 6
routers/user/auth.go View File

@@ -215,6 +215,7 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) {
}
return
}
log.Info("Go this 1.")
// If this user is enrolled in 2FA, we can't sign the user in just yet.
// Instead, redirect them to the 2FA authentication page.
_, err = models.GetTwoFactorByUID(u.ID)
@@ -226,7 +227,7 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) {
}
return
}
log.Info("Go this 2.")
// User needs to use 2FA, save data and redirect to 2FA page.
if err := ctx.Session.Set("twofaUid", u.ID); err != nil {
ctx.ServerError("UserSignIn: Unable to set twofaUid in session", err)
@@ -240,13 +241,14 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) {
ctx.ServerError("UserSignIn: Unable to save session", err)
return
}
log.Info("Go this 3.")
regs, err := models.GetU2FRegistrationsByUID(u.ID)
if err == nil && len(regs) > 0 {
ctx.Redirect(setting.AppSubURL + "/user/u2f")
return
}

log.Info("Go this 4.")
models.SaveLoginInfoToDb(ctx.Req.Request, u)
ctx.Redirect(setting.AppSubURL + "/user/two_factor")
}

@@ -1168,8 +1170,8 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
log.Trace("Account created: %s", u.Name, ctx.Data["MsgID"])

err := models.AddEmailAddress(&models.EmailAddress{
UID: u.ID,
Email: form.Email,
UID: u.ID,
Email: form.Email,
IsActivated: !setting.Service.RegisterEmailConfirm,
})

@@ -1267,7 +1269,7 @@ func Activate(ctx *context.Context) {
}

email, err := models.GetEmailAddressByIDAndEmail(user.ID, user.Email)
if err != nil || email == nil{
if err != nil || email == nil {
log.Error("GetEmailAddressByIDAndEmail failed", ctx.Data["MsgID"])
} else {
if err := email.Activate(); err != nil {


Loading…
Cancel
Save