From e52e09cfd59985fa827d1a9b15cb93890eac5230 Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 26 Oct 2021 14:40:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=97=A5=E5=BF=97=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/models.go | 1 + models/user_login_log.go | 31 +++++++++++++++++++++++++++++++ routers/user/auth.go | 14 ++++++++------ 3 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 models/user_login_log.go diff --git a/models/models.go b/models/models.go index 0cdcee4fd..696d0949b 100755 --- a/models/models.go +++ b/models/models.go @@ -139,6 +139,7 @@ func init() { new(RepoStatistic), new(SummaryStatistic), new(UserBusinessAnalysis), + new(UserLoginLog), ) gonicNames := []string{"SSL", "UID"} diff --git a/models/user_login_log.go b/models/user_login_log.go new file mode 100644 index 000000000..849e9af0b --- /dev/null +++ b/models/user_login_log.go @@ -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 +} \ No newline at end of file diff --git a/routers/user/auth.go b/routers/user/auth.go index 13e338565..749386e07 100755 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -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 {