Beispiel #1
0
        /// <summary>
        /// Merge two accounts.
        /// </summary>
        /// <remarks>
        /// Useful when somebody registers a new certificate
        /// that doesn't match the previous cert's key. Merging the new account
        /// into the old account brings the new certificate token under the old
        /// account id.
        /// </remarks>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task MergeAccounts(AccountMergeModel model)
        {
            _logger.LogDebug($"Attempting merge of {model.DefunctGlobalId} into {model.ActiveGlobalId}");

            if (!model.DefunctGlobalId.HasValue() ||
                !model.ActiveGlobalId.HasValue() ||
                model.DefunctGlobalId == model.ActiveGlobalId
                )
            {
                throw new AccountNotFoundException();
            }

            var defunct = await _store.LoadByGuid(model.DefunctGlobalId);

            var active = await _store.LoadByGuid(model.ActiveGlobalId);

            if (defunct == null || active == null)
            {
                throw new AccountNotFoundException();
            }

            var tokens = defunct.Tokens
                         .Where(t =>
                                t.Type == AccountTokenType.Certificate ||
                                t.Type == AccountTokenType.Credential)
                         .ToArray();

            foreach (var token in tokens)
            {
                defunct.Tokens.Remove(token);
                token.UserId = active.Id;
                active.Tokens.Add(token);
            }

            foreach (var property in defunct.Properties
                     .Where(p => p.Key == ClaimTypes.Email))
            {
                defunct.Properties.Remove(property);
                property.AccountId = active.Id;
                active.Properties.Add(property);
            }

            await _store.Update(active);

            await _store.Delete(defunct.Id);

            _logger.LogInformation($"Merged account tokens from {model.DefunctGlobalId} into {model.ActiveGlobalId}");
        }
 public void Delete(Account account, string serviceId)
 {
     xAccountStore.Delete(account, serviceId);
 }