示例#1
0
        public async Task <IActionResult> PostMergeCode([FromBody] AccountMergeModel model)
        {
            await _svc.MergeAccounts(model);

            Audit(AuditId.MergeAccount, "admin", model.DefunctGlobalId, model.ActiveGlobalId);
            return(Ok());
        }
示例#2
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}");
        }
示例#3
0
        public async Task <IActionResult> PostMergeCode([FromBody] AccountMergeModel model)
        {
            string defunctGlobalId = await _cache.GetStringAsync(model.Code);

            if (String.IsNullOrEmpty(defunctGlobalId))
            {
                throw new AccountNotConfirmedException();
            }

            await _cache.RemoveAsync(model.Code);

            await _cache.RemoveAsync(defunctGlobalId + ":mergecode");

            model.DefunctGlobalId = defunctGlobalId;
            model.ActiveGlobalId  = User.GetSubjectId();
            await _svc.MergeAccounts(model);

            Audit(AuditId.MergeAccount, model.DefunctGlobalId, model.ActiveGlobalId);

            return(Ok());
        }