public async Task <SaveUserAccountResponse> SaveUserAccount([FromBody] SaveUserAccountRequest request)
        {
            SaveUserAccountResponse response = new SaveUserAccountResponse();

            if (response.InitializeFromModelStateIfInvalid(ModelState))
            {
                return(response);
            }

            if (request.ShouldUpdateAllProps)
            {
                await _userAccountUpsert.ExecuteAsync(request.UserAccount);
            }
            else
            {
                var existingUser = await _userAccountGetByID.ExecuteAsync(request.UserAccount.ID, true);

                request.UserAccount.CopyProperties(existingUser,
                                                   (propInfo, source, target) => request.PropsToUpdate.Contains(propInfo.Name));

                foreach (var saua in request.SiteAccountUserAccounts)
                {
                    existingUser.SiteAccountUserAccounts.Add(saua);
                }

                await _userAccountUpsert.ExecuteAsync(existingUser);
            }

            return(response);
        }
Exemple #2
0
        public static Func <OAuthCreatingTicketContext, Task> New(AuthenticationProvider provider)
        {
            async Task CreateTask(OAuthCreatingTicketContext context)
            {
                // Create the request for user data
                var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);

                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
                // Send Request and get response
                var response = await context.Backchannel.SendAsync(request,
                                                                   HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);

                JObject contentJObject = JObject.Parse(await response.Content.ReadAsStringAsync());

                UserAccount userAccount = new UserAccount();

                // Parse the response for the email, names, and avatar url
                switch (provider)
                {
                case AuthenticationProvider.Github:
                    userAccount.AddGitHubEmailAddress(contentJObject);
                    break;

                case AuthenticationProvider.Google:
                    userAccount.AddGoogleEmail(contentJObject)
                    .AddGoogleNames(contentJObject)
                    .AddGoogleAvatarLink(contentJObject);
                    break;
                }
                // If an Email wasnt parsed fail out
                if (string.IsNullOrEmpty(userAccount.EmailAddress))
                {
                    context.Fail("No Email Provided.");
                    return;
                }
                // Check for existing user. Create if not exists. update.
                UserAccountGetByEmail userAccountGetByEmail = new UserAccountGetByEmail(new IPManDataContext(ConfigurationService.Configuration));
                UserAccountUpsert     userAccountUpsert     = new UserAccountUpsert(new IPManDataContext(ConfigurationService.Configuration));

                UserAccount preExistingUser = await userAccountGetByEmail.ExecuteAsync(userAccount.EmailAddress, true);

                if (preExistingUser == null)
                {
                    userAccount.AddCreatedData();
                }
                else
                {
                    userAccount = preExistingUser;
                }

                userAccount.AddLoginData(provider);

                await userAccountUpsert.ExecuteAsync(userAccount, preExistingUser == null);

                context.Identity.AddClaim(new Claim("TempSalt", userAccount.UserAccountSalt));

                context.Success();
            }

            return(CreateTask);
        }