diff --git a/internal/logic/public/user/unbindDeviceLogic.go b/internal/logic/public/user/unbindDeviceLogic.go index 6081fb5..b490ae8 100644 --- a/internal/logic/public/user/unbindDeviceLogic.go +++ b/internal/logic/public/user/unbindDeviceLogic.go @@ -10,7 +10,6 @@ import ( "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/constant" "github.com/perfect-panel/server/pkg/logger" - "github.com/perfect-panel/server/pkg/tool" "github.com/perfect-panel/server/pkg/xerr" "github.com/pkg/errors" "gorm.io/gorm" @@ -32,99 +31,51 @@ func NewUnbindDeviceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Unbi } func (l *UnbindDeviceLogic) UnbindDevice(req *types.UnbindDeviceRequest) error { - userInfo := l.ctx.Value(constant.CtxKeyUser).(*user.User) + userInfo, ok := l.ctx.Value(constant.CtxKeyUser).(*user.User) + if !ok { + return errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access") + } + device, err := l.svcCtx.UserModel.FindOneDevice(l.ctx, req.Id) if err != nil { return errors.Wrapf(xerr.NewErrCode(xerr.DeviceNotExist), "find device") } - scopeHelper := newFamilyScopeHelper(l.ctx, l.svcCtx) - scopeUserIds, err := scopeHelper.resolveScopedUserIds(userInfo.Id) + if device.UserId != userInfo.Id { + return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "device not belong to user") + } + + currentSessionID, _ := l.ctx.Value(constant.CtxKeySessionID).(string) + return l.logoutUnbind(userInfo, device, currentSessionID) +} + +func (l *UnbindDeviceLogic) logoutUnbind(userInfo *user.User, device *user.Device, currentSessionID string) error { + err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error { + exitHelper := newFamilyExitHelper(l.ctx, l.svcCtx) + return exitHelper.removeUserFromActiveFamily(tx, userInfo.Id, true) + }) if err != nil { return err } - if !tool.Contains(scopeUserIds, device.UserId) { - return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "device not belong to user") + l.svcCtx.DeviceManager.KickDevice(device.UserId, device.Identifier) + + if currentSessionID != "" { + sessionIdCacheKey := fmt.Sprintf("%v:%v", config.SessionIdKey, currentSessionID) + _ = l.svcCtx.Redis.Del(l.ctx, sessionIdCacheKey).Err() + + sessionsKey := fmt.Sprintf("%s%v", config.UserSessionsKeyPrefix, userInfo.Id) + _ = l.svcCtx.Redis.ZRem(l.ctx, sessionsKey, currentSessionID).Err() } - return l.svcCtx.DB.Transaction(func(tx *gorm.DB) error { - var deleteDevice user.Device - err = tx.Model(&deleteDevice).Where("id = ?", req.Id).First(&deleteDevice).Error - if err != nil { - return errors.Wrapf(xerr.NewErrCode(xerr.QueueEnqueueError), "find device err: %v", err) - } - err = tx.Delete(deleteDevice).Error - if err != nil { - return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), "delete device err: %v", err) - } - var userAuth user.AuthMethods - err = tx.Model(&userAuth).Where("auth_identifier = ? and auth_type = ?", deleteDevice.Identifier, "device").First(&userAuth).Error - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil - } - return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find device online record err: %v", err) - } + deviceCacheKey := fmt.Sprintf("%v:%v", config.DeviceCacheKeyKey, device.Identifier) + if sessionId, redisErr := l.svcCtx.Redis.Get(l.ctx, deviceCacheKey).Result(); redisErr == nil && sessionId == currentSessionID { + _ = l.svcCtx.Redis.Del(l.ctx, deviceCacheKey).Err() + } - err = tx.Delete(&userAuth).Error - if err != nil { - return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), "delete device online record err: %v", err) - } + if clearErr := l.svcCtx.UserModel.ClearUserCache(l.ctx, userInfo); clearErr != nil { + l.Errorw("clear user cache failed", logger.Field("user_id", userInfo.Id), logger.Field("error", clearErr.Error())) + } - if deleteDevice.UserId == userInfo.Id { - shouldLeaveFamily, leaveCheckErr := l.shouldLeaveFamilyAfterUnbind(tx, userInfo.Id) - if leaveCheckErr != nil { - return leaveCheckErr - } - if shouldLeaveFamily { - exitHelper := newFamilyExitHelper(l.ctx, l.svcCtx) - if leaveErr := exitHelper.removeUserFromActiveFamily(tx, userInfo.Id, true); leaveErr != nil { - return leaveErr - } - } - } - - l.svcCtx.DeviceManager.KickDevice(deleteDevice.UserId, deleteDevice.Identifier) - - //remove device cache - deviceCacheKey := fmt.Sprintf("%v:%v", config.DeviceCacheKeyKey, deleteDevice.Identifier) - if sessionId, err := l.svcCtx.Redis.Get(l.ctx, deviceCacheKey).Result(); err == nil && sessionId != "" { - _ = l.svcCtx.Redis.Del(l.ctx, deviceCacheKey).Err() - sessionIdCacheKey := fmt.Sprintf("%v:%v", config.SessionIdKey, sessionId) - _ = l.svcCtx.Redis.Del(l.ctx, sessionIdCacheKey).Err() - } - return nil - }) -} - -func (l *UnbindDeviceLogic) shouldLeaveFamilyAfterUnbind(tx *gorm.DB, userID int64) (bool, error) { - var nonDeviceAuthCount int64 - if err := tx.Model(&user.AuthMethods{}). - Where("user_id = ? AND auth_type <> ?", userID, "device"). - Count(&nonDeviceAuthCount).Error; err != nil { - return false, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "count non-device auth methods failed") - } - if nonDeviceAuthCount > 0 { - return false, nil - } - - var deviceAuthCount int64 - if err := tx.Model(&user.AuthMethods{}). - Where("user_id = ? AND auth_type = ?", userID, "device"). - Count(&deviceAuthCount).Error; err != nil { - return false, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "count device auth methods failed") - } - if deviceAuthCount > 0 { - return false, nil - } - - var deviceCount int64 - if err := tx.Model(&user.Device{}). - Where("user_id = ?", userID). - Count(&deviceCount).Error; err != nil { - return false, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "count devices failed") - } - - return deviceCount == 0, nil + return nil }