Skip to content

Commit

Permalink
update user last_login_at when user login from casdoor
Browse files Browse the repository at this point in the history
  • Loading branch information
Lei Da committed Oct 12, 2024
1 parent 677cd4c commit 1325ca3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
11 changes: 7 additions & 4 deletions builder/store/database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,13 @@ func (s *UserStore) FindByGitAccessToken(ctx context.Context, token string) (*Us
return &user, nil
}

func (s *UserStore) FindByUUID(ctx context.Context, uuid string) (user User, err error) {
user.UUID = uuid
err = s.db.Operator.Core.NewSelect().Model(&user).Where("uuid = ?", uuid).Scan(ctx)
return
func (s *UserStore) FindByUUID(ctx context.Context, uuid string) (*User, error) {
var user User
err := s.db.Operator.Core.NewSelect().Model(&user).Where("uuid = ?", uuid).Scan(ctx)
if err != nil {
return nil, err
}
return &user, nil
}

func (s *UserStore) GetActiveUserCount(ctx context.Context) (int, error) {
Expand Down
23 changes: 17 additions & 6 deletions user/component/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/slog"
"os"
"sync"
"time"

"github.com/bwmarrin/snowflake"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
Expand Down Expand Up @@ -415,16 +416,26 @@ func (c *UserComponent) Signin(ctx context.Context, code, state string) (*types.
return nil, "", fmt.Errorf("failed to check user existance by name in db,error:%w", err)
}

var dbu *database.User
if !exists {
_, err = c.createFromCasdoorUser(ctx, cu)
dbu, err = c.createFromCasdoorUser(ctx, cu)
if err != nil {
return nil, "", fmt.Errorf("failed to create user,error:%w", err)
}
}
// get user from db for username, as casdoor may have different username
dbu, err := c.us.FindByUUID(ctx, cu.Id)
if err != nil {
return nil, "", fmt.Errorf("failed to find user by uuid in db, uuid:%s, error:%w", cu.Id, err)
} else {
// get user from db for username, as casdoor may have different username
dbu, err = c.us.FindByUUID(ctx, cu.Id)
if err != nil {
return nil, "", fmt.Errorf("failed to find user by uuid in db, uuid:%s, error:%w", cu.Id, err)
}
// update user login time asynchronously
go func() {
dbu.LastLoginAt = time.Now().Format("2006-01-02 15:04:05")
err := c.us.Update(ctx, dbu)
if err != nil {
slog.Error("failed to update user login time", "error", err, "username", dbu.Username)
}
}()
}
hubToken, signed, err := c.jwtc.GenerateToken(ctx, types.CreateJWTReq{
UUID: dbu.UUID,
Expand Down

0 comments on commit 1325ca3

Please sign in to comment.