mirror of
https://github.com/lejianwen/rustdesk-api.git
synced 2025-09-15 14:28:34 +08:00
logout should unbind uuid and uid of peer
This commit is contained in:
parent
5c65edb8fa
commit
853063c59b
@ -2,9 +2,10 @@ package model
|
|||||||
|
|
||||||
type UserToken struct {
|
type UserToken struct {
|
||||||
IdModel
|
IdModel
|
||||||
UserId uint `json:"user_id" gorm:"default:0;not null;index"`
|
UserId uint `json:"user_id" gorm:"default:0;not null;index"`
|
||||||
Token string `json:"token" gorm:"default:'';not null;index"`
|
DeviceUuid string `json:"device_uuid"`
|
||||||
ExpiredAt int64 `json:"expired_at" gorm:"default:0;not null;"`
|
Token string `json:"token" gorm:"default:'';not null;index"`
|
||||||
|
ExpiredAt int64 `json:"expired_at" gorm:"default:0;not null;"`
|
||||||
TimeModel
|
TimeModel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -26,6 +26,13 @@ func (ps *PeerService) InfoByRowId(id uint) *model.Peer {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindByUserIdAndUuid 根据用户id和uuid查找peer
|
||||||
|
func (ps *PeerService) FindByUserIdAndUuid(uuid string,userId uint) *model.Peer {
|
||||||
|
p := &model.Peer{}
|
||||||
|
global.DB.Where("uuid = ? and user_id = ?", uuid, userId).First(p)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
// UuidBindUserId 绑定用户id
|
// UuidBindUserId 绑定用户id
|
||||||
func (ps *PeerService) UuidBindUserId(uuid string, userId uint) {
|
func (ps *PeerService) UuidBindUserId(uuid string, userId uint) {
|
||||||
peer := ps.FindByUuid(uuid)
|
peer := ps.FindByUuid(uuid)
|
||||||
@ -35,6 +42,14 @@ func (ps *PeerService) UuidBindUserId(uuid string, userId uint) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UuidUnbindUserId 解绑用户id, 用于用户注销
|
||||||
|
func (ps *PeerService) UuidUnbindUserId(uuid string, userId uint) {
|
||||||
|
peer := ps.FindByUserIdAndUuid(uuid, userId)
|
||||||
|
if peer.RowId > 0 {
|
||||||
|
global.DB.Model(peer).Update("user_id", 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ListByUserIds 根据用户id取列表
|
// ListByUserIds 根据用户id取列表
|
||||||
func (ps *PeerService) ListByUserIds(userIds []uint, page, pageSize uint) (res *model.PeerList) {
|
func (ps *PeerService) ListByUserIds(userIds []uint, page, pageSize uint) (res *model.PeerList) {
|
||||||
res = &model.PeerList{}
|
res = &model.PeerList{}
|
||||||
|
|||||||
@ -73,9 +73,10 @@ func (us *UserService) GenerateToken(u *model.User) string {
|
|||||||
func (us *UserService) Login(u *model.User, llog *model.LoginLog) *model.UserToken {
|
func (us *UserService) Login(u *model.User, llog *model.LoginLog) *model.UserToken {
|
||||||
token := us.GenerateToken(u)
|
token := us.GenerateToken(u)
|
||||||
ut := &model.UserToken{
|
ut := &model.UserToken{
|
||||||
UserId: u.Id,
|
UserId: u.Id,
|
||||||
Token: token,
|
Token: token,
|
||||||
ExpiredAt: time.Now().Add(time.Hour * 24 * 7).Unix(),
|
DeviceUuid: llog.Uuid,
|
||||||
|
ExpiredAt: time.Now().Add(time.Hour * 24 * 7).Unix(),
|
||||||
}
|
}
|
||||||
global.DB.Create(ut)
|
global.DB.Create(ut)
|
||||||
llog.UserTokenId = ut.UserId
|
llog.UserTokenId = ut.UserId
|
||||||
@ -153,9 +154,27 @@ func (us *UserService) Create(u *model.User) error {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logout 退出登录
|
// GetUuidByToken 根据token和user取uuid
|
||||||
|
func (us *UserService) GetUuidByToken(u *model.User, token string) string {
|
||||||
|
ut := &model.UserToken{}
|
||||||
|
err :=global.DB.Where("user_id = ? and token = ?", u.Id, token).First(ut).Error
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return ut.DeviceUuid
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logout 退出登录 -> 删除token, 解绑uuid
|
||||||
func (us *UserService) Logout(u *model.User, token string) error {
|
func (us *UserService) Logout(u *model.User, token string) error {
|
||||||
return global.DB.Where("user_id = ? and token = ?", u.Id, token).Delete(&model.UserToken{}).Error
|
uuid := us.GetUuidByToken(u, token)
|
||||||
|
err := global.DB.Where("user_id = ? and token = ?", u.Id, token).Delete(&model.UserToken{}).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if uuid != "" {
|
||||||
|
AllService.PeerService.UuidUnbindUserId(uuid, u.Id)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete 删除用户和oauth信息
|
// Delete 删除用户和oauth信息
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user