public static ExternalLoginTokenDto BuildDto(int externalLoginId, IExternalLoginToken token, int?id = null)
    {
        var dto = new ExternalLoginTokenDto
        {
            Id = id ?? default,
            ExternalLoginId = externalLoginId,
            Name            = token.Name,
            Value           = token.Value,
            CreateDate      = DateTime.Now,
        };

        return(dto);
    }
Beispiel #2
0
        /// <inheritdoc />
        public void Save(Guid userOrMemberKey, IEnumerable <IExternalLoginToken> tokens)
        {
            // get the existing logins (provider + id)
            var existingUserLogins = Database
                                     .Fetch <ExternalLoginDto>(GetBaseQuery(false).Where <ExternalLoginDto>(x => x.UserOrMemberKey == userOrMemberKey))
                                     .ToDictionary(x => x.LoginProvider, x => x.Id);

            // deduplicate the tokens
            tokens = tokens.DistinctBy(x => x.LoginProvider + x.Name).ToList();

            var providers = tokens.Select(x => x.LoginProvider).Distinct().ToList();

            Sql <ISqlContext> sql = GetBaseTokenQuery(true)
                                    .WhereIn <ExternalLoginDto>(x => x.LoginProvider, providers)
                                    .Where <ExternalLoginDto>(x => x.UserOrMemberKey == userOrMemberKey);

            var toUpdate = new Dictionary <int, (IExternalLoginToken externalLoginToken, int externalLoginId)>();
            var toDelete = new List <int>();
            var toInsert = new List <IExternalLoginToken>(tokens);

            var existingTokens = Database.Fetch <ExternalLoginTokenDto>(sql);

            foreach (ExternalLoginTokenDto existing in existingTokens)
            {
                IExternalLoginToken found = tokens.FirstOrDefault(x =>
                                                                  x.LoginProvider.InvariantEquals(existing.ExternalLoginDto.LoginProvider) &&
                                                                  x.Name.InvariantEquals(existing.Name));

                if (found != null)
                {
                    toUpdate.Add(existing.Id, (found, existing.ExternalLoginId));
                    // if it's an update then it's not an insert
                    toInsert.RemoveAll(x => x.LoginProvider.InvariantEquals(found.LoginProvider) && x.Name.InvariantEquals(found.Name));
                }
                else
                {
                    toDelete.Add(existing.Id);
                }
            }

            // do the deletes, updates and inserts
            if (toDelete.Count > 0)
            {
                Database.DeleteMany <ExternalLoginTokenDto>().Where(x => toDelete.Contains(x.Id)).Execute();
            }

            foreach (KeyValuePair <int, (IExternalLoginToken externalLoginToken, int externalLoginId)> u in toUpdate)
            {
                Database.Update(ExternalLoginFactory.BuildDto(u.Value.externalLoginId, u.Value.externalLoginToken, u.Key));
            }

            var insertDtos = new List <ExternalLoginTokenDto>();

            foreach (IExternalLoginToken t in toInsert)
            {
                if (!existingUserLogins.TryGetValue(t.LoginProvider, out int externalLoginId))
                {
                    throw new InvalidOperationException($"A token was attempted to be saved for login provider {t.LoginProvider} which is not assigned to this user");
                }
                insertDtos.Add(ExternalLoginFactory.BuildDto(externalLoginId, t));
            }
            Database.InsertBulk(insertDtos);
        }