Reviewed-on: https://git.openi.org.cn/OpenI/aiforge/pulls/618 Reviewed-by: lewis <747342561@qq.com>pull/622/head
@@ -139,6 +139,7 @@ func init() { | |||||
new(RepoStatistic), | new(RepoStatistic), | ||||
new(SummaryStatistic), | new(SummaryStatistic), | ||||
new(UserBusinessAnalysis), | new(UserBusinessAnalysis), | ||||
new(UserLoginLog), | |||||
) | ) | ||||
gonicNames := []string{"SSL", "UID"} | gonicNames := []string{"SSL", "UID"} | ||||
@@ -153,6 +153,7 @@ func CountData(wikiCountMap map[string]int) { | |||||
CommitDatasetSizeMap := queryDatasetSize(start_unix, end_unix) | CommitDatasetSizeMap := queryDatasetSize(start_unix, end_unix) | ||||
SolveIssueCountMap := querySolveIssue(start_unix, end_unix) | SolveIssueCountMap := querySolveIssue(start_unix, end_unix) | ||||
CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix) | CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix) | ||||
LoginCountMap := queryLoginCount(start_unix, end_unix) | |||||
for i, userRecord := range userList { | for i, userRecord := range userList { | ||||
var dateRecord UserBusinessAnalysis | var dateRecord UserBusinessAnalysis | ||||
@@ -235,6 +236,12 @@ func CountData(wikiCountMap map[string]int) { | |||||
dateRecord.CreateRepoCount = CreateRepoCountMap[dateRecord.ID] | dateRecord.CreateRepoCount = CreateRepoCountMap[dateRecord.ID] | ||||
} | } | ||||
if _, ok := LoginCountMap[dateRecord.ID]; !ok { | |||||
dateRecord.LoginCount = 0 | |||||
} else { | |||||
dateRecord.LoginCount = LoginCountMap[dateRecord.ID] | |||||
} | |||||
dateRecord.CommitModelCount = 0 | dateRecord.CommitModelCount = 0 | ||||
statictisSess := xStatistic.NewSession() | statictisSess := xStatistic.NewSession() | ||||
@@ -421,6 +428,24 @@ func queryUserCreateRepo(start_unix int64, end_unix int64) map[int64]int { | |||||
return resultMap | return resultMap | ||||
} | } | ||||
func queryLoginCount(start_unix int64, end_unix int64) map[int64]int { | |||||
statictisSess := xStatistic.NewSession() | |||||
defer statictisSess.Close() | |||||
statictisSess.Select("id,u_id").Table("user_login_log").Where("created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)) | |||||
userLoginLogList := make([]*UserLoginLog, 0) | |||||
statictisSess.Find(&userLoginLogList) | |||||
resultMap := make(map[int64]int) | |||||
log.Info("query user login size=" + fmt.Sprint(len(userLoginLogList))) | |||||
for _, loginRecord := range userLoginLogList { | |||||
if _, ok := resultMap[loginRecord.UId]; !ok { | |||||
resultMap[loginRecord.UId] = 1 | |||||
} else { | |||||
resultMap[loginRecord.UId] += 1 | |||||
} | |||||
} | |||||
return resultMap | |||||
} | |||||
func subMonth(t1, t2 time.Time) (month int) { | func subMonth(t1, t2 time.Time) (month int) { | ||||
y1 := t1.Year() | y1 := t1.Year() | ||||
y2 := t2.Year() | y2 := t2.Year() | ||||
@@ -0,0 +1,34 @@ | |||||
package models | |||||
import ( | |||||
"net/http" | |||||
"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) { | |||||
statictisSess := xStatistic.NewSession() | |||||
defer statictisSess.Close() | |||||
var dateRecord UserLoginLog | |||||
dateRecord.UId = u.ID | |||||
dateRecord.IpAddr = getIP(r) | |||||
statictisSess.Insert(&dateRecord) | |||||
} | |||||
func getIP(r *http.Request) string { | |||||
forwarded := r.Header.Get("X-FORWARDED-FOR") | |||||
if forwarded != "" { | |||||
return forwarded | |||||
} | |||||
return r.RemoteAddr | |||||
} |
@@ -215,6 +215,7 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) { | |||||
} | } | ||||
return | return | ||||
} | } | ||||
models.SaveLoginInfoToDb(ctx.Req.Request, u) | |||||
// If this user is enrolled in 2FA, we can't sign the user in just yet. | // If this user is enrolled in 2FA, we can't sign the user in just yet. | ||||
// Instead, redirect them to the 2FA authentication page. | // Instead, redirect them to the 2FA authentication page. | ||||
_, err = models.GetTwoFactorByUID(u.ID) | _, err = models.GetTwoFactorByUID(u.ID) | ||||
@@ -226,7 +227,6 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) { | |||||
} | } | ||||
return | return | ||||
} | } | ||||
// User needs to use 2FA, save data and redirect to 2FA page. | // User needs to use 2FA, save data and redirect to 2FA page. | ||||
if err := ctx.Session.Set("twofaUid", u.ID); err != nil { | if err := ctx.Session.Set("twofaUid", u.ID); err != nil { | ||||
ctx.ServerError("UserSignIn: Unable to set twofaUid in session", err) | ctx.ServerError("UserSignIn: Unable to set twofaUid in session", err) | ||||
@@ -240,7 +240,6 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) { | |||||
ctx.ServerError("UserSignIn: Unable to save session", err) | ctx.ServerError("UserSignIn: Unable to save session", err) | ||||
return | return | ||||
} | } | ||||
regs, err := models.GetU2FRegistrationsByUID(u.ID) | regs, err := models.GetU2FRegistrationsByUID(u.ID) | ||||
if err == nil && len(regs) > 0 { | if err == nil && len(regs) > 0 { | ||||
ctx.Redirect(setting.AppSubURL + "/user/u2f") | ctx.Redirect(setting.AppSubURL + "/user/u2f") | ||||
@@ -1168,8 +1167,8 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo | |||||
log.Trace("Account created: %s", u.Name, ctx.Data["MsgID"]) | log.Trace("Account created: %s", u.Name, ctx.Data["MsgID"]) | ||||
err := models.AddEmailAddress(&models.EmailAddress{ | err := models.AddEmailAddress(&models.EmailAddress{ | ||||
UID: u.ID, | |||||
Email: form.Email, | |||||
UID: u.ID, | |||||
Email: form.Email, | |||||
IsActivated: !setting.Service.RegisterEmailConfirm, | IsActivated: !setting.Service.RegisterEmailConfirm, | ||||
}) | }) | ||||
@@ -1267,7 +1266,7 @@ func Activate(ctx *context.Context) { | |||||
} | } | ||||
email, err := models.GetEmailAddressByIDAndEmail(user.ID, user.Email) | 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"]) | log.Error("GetEmailAddressByIDAndEmail failed", ctx.Data["MsgID"]) | ||||
} else { | } else { | ||||
if err := email.Activate(); err != nil { | if err := email.Activate(); err != nil { | ||||