From afea03a8e360f733265dd09fd6d2dc701c88c65b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Mon, 29 Aug 2022 08:15:38 +0800 Subject: [PATCH] =?UTF-8?q?enhance(user):=E5=A2=9E=E5=8A=A0=E4=B8=93?= =?UTF-8?q?=E9=97=A8=E7=94=A8=E4=BA=8E=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E6=88=B7=E7=BC=93=E5=AD=98=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/user.go | 14 ++++++++++++++ service/user.go | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/model/user.go b/model/user.go index 4e6944f..0fd1cd9 100644 --- a/model/user.go +++ b/model/user.go @@ -19,3 +19,17 @@ type User struct { func (User) TableName() string { return "user" } + +type UserWithCredentials struct { + Created `xorm:"extends"` + Id string `xorm:"varchar(120) pk not null" json:"id"` + Username string `xorm:"varchar(30) not null" json:"username"` + Password string `xorm:"varchar(256) not null" json:"credential"` + ResetNeeded bool `xorm:"bool not null" json:"resetNeeded"` + Type int8 `xorm:"smallint not null" json:"type"` + Enabled bool `xorm:"bool not null" json:"enabled"` +} + +func (UserWithCredentials) TableName() string { + return "user" +} diff --git a/service/user.go b/service/user.go index f98fc94..538ddd1 100644 --- a/service/user.go +++ b/service/user.go @@ -21,7 +21,7 @@ type _UserService struct{} var UserService _UserService func (u _UserService) ProcessEnterpriseUserLogin(username, password string) (*model.Session, error) { - user, err := u.findUserByUsername(username) + user, err := u.findUserWithCredentialsByUsername(username) if err != nil { return nil, err @@ -63,7 +63,7 @@ func (u _UserService) ProcessEnterpriseUserLogin(username, password string) (*mo } func (u _UserService) ProcessManagementUserLogin(username, password string) (*model.Session, error) { - user, err := u.findUserByUsername(username) + user, err := u.findUserWithCredentialsByUsername(username) if err != nil { return nil, err @@ -266,6 +266,18 @@ func (_UserService) SearchLimitUsers(keyword string, limit int) ([]model.JoinedU return users, nil } +func (_UserService) findUserWithCredentialsByUsername(username string) (*model.UserWithCredentials, error) { + if cachedUser, _ := cache.RetreiveSearch[model.UserWithCredentials]("user_with_credentials", username); cachedUser != nil { + return cachedUser, nil + } + user := new(model.UserWithCredentials) + has, err := global.DBConn.Where(builder.Eq{"username": username}).NoAutoCondition().Get(user) + if has { + cache.CacheSearch(*user, []string{"user"}, "user_with_credentials", username) + } + return _postProcessSingle(user, has, err) +} + func (_UserService) findUserByUsername(username string) (*model.User, error) { if cachedUser, _ := cache.RetreiveSearch[model.User]("user", username); cachedUser != nil { return cachedUser, nil