Example #1
0
    protected virtual async Task <SessionInfo[]> GetUserSessions(
        string userId, CancellationToken cancellationToken = default)
    {
        if (!DbUserIdHandler.TryParse(userId).IsSome(out var dbUserId))
        {
            return(Array.Empty <SessionInfo>());
        }

        var dbContext = CreateDbContext();

        await using var _1 = dbContext.ConfigureAwait(false);
        var tx = await dbContext.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);

        await using var _2 = tx.ConfigureAwait(false);

        var dbSessions = await Sessions.ListByUser(dbContext, dbUserId, cancellationToken).ConfigureAwait(false);

        var sessions = new SessionInfo[dbSessions.Length];

        for (var i = 0; i < dbSessions.Length; i++)
        {
            sessions[i] = SessionConverter.ToModel(dbSessions[i]) !;
        }
        return(sessions);
    }
Example #2
0
    // Compute methods

    // [ComputeMethod] inherited
    public override async Task <User?> GetUser(
        string userId, CancellationToken cancellationToken = default)
    {
        var dbUserId = DbUserIdHandler.Parse(userId);
        var dbUser   = await Users.Get(dbUserId, cancellationToken).ConfigureAwait(false);

        return(UserConverter.ToModel(dbUser));
    }
Example #3
0
    // Commands

    // [CommandHandler] inherited
    public override async Task SignIn(
        SignInCommand command, CancellationToken cancellationToken = default)
    {
        var(session, user, authenticatedIdentity) = command;
        var context = CommandContext.GetCurrent();

        if (Computed.IsInvalidating())
        {
            _ = GetAuthInfo(session, default);
            _ = GetSessionInfo(session, default);
            var invSessionInfo = context.Operation().Items.Get <SessionInfo>();
            if (invSessionInfo != null)
            {
                _ = GetUser(invSessionInfo.UserId, default);
                _ = GetUserSessions(invSessionInfo.UserId, default);
            }
            return;
        }

        if (!user.Identities.ContainsKey(authenticatedIdentity))
#pragma warning disable MA0015
        {
            throw new ArgumentOutOfRangeException(
                      $"{nameof(command)}.{nameof(SignInCommand.AuthenticatedIdentity)}");
        }
#pragma warning restore MA0015
        if (await IsSignOutForced(session, cancellationToken).ConfigureAwait(false))
        {
            throw Errors.ForcedSignOut();
        }

        var dbContext = await CreateCommandDbContext(cancellationToken).ConfigureAwait(false);

        await using var _1 = dbContext.ConfigureAwait(false);

        var isNewUser = false;
        var dbUser    = await Users
                        .GetByUserIdentity(dbContext, authenticatedIdentity, cancellationToken)
                        .ConfigureAwait(false);

        if (dbUser == null)
        {
            (dbUser, isNewUser) = await Users
                                  .GetOrCreateOnSignIn(dbContext, user, cancellationToken)
                                  .ConfigureAwait(false);

            if (isNewUser == false)
            {
                UserConverter.UpdateEntity(user, dbUser);
                await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
            }
        }
        else
        {
            user = user with {
                Id = DbUserIdHandler.Format(dbUser.Id)
            };
            UserConverter.UpdateEntity(user, dbUser);
            await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
        }

        var dbSessionInfo = await Sessions.GetOrCreate(dbContext, session.Id, cancellationToken).ConfigureAwait(false);

        var sessionInfo = SessionConverter.ToModel(dbSessionInfo);
        if (sessionInfo !.IsSignOutForced)
        {
            throw Errors.ForcedSignOut();
        }

        sessionInfo = sessionInfo with {
            LastSeenAt            = Clocks.SystemClock.Now,
            AuthenticatedIdentity = authenticatedIdentity,
            UserId = DbUserIdHandler.Format(dbUser.Id)
        };
        context.Operation().Items.Set(sessionInfo);
        context.Operation().Items.Set(isNewUser);
        await Sessions.Upsert(dbContext, sessionInfo, cancellationToken).ConfigureAwait(false);
    }