private bool CreateRelationship(string RequestorID, string FriendID) { try { List <AccountAssociations> userAssociations = new List <AccountAssociations>(); var PrimaryUserAssociation = new AccountAssociations() { PrimaryUserID = RequestorID, AssociatedUserID = FriendID }; var SecondaryUserAssociation = new AccountAssociations() { PrimaryUserID = FriendID, AssociatedUserID = RequestorID }; _context.AccountAssociations.Add(PrimaryUserAssociation); _context.AccountAssociations.Add(SecondaryUserAssociation); _context.SaveChanges(); //Copy the associations of the requestor to the reqeste. //Get a list of account associations that are assigned to the primary requestor except the current requeste List <AccountAssociations> requestorAssociations = _context.AccountAssociations.Where(a => a.PrimaryUserID == RequestorID && a.AssociatedUserID != FriendID).ToList <AccountAssociations>(); //Loop through each associated user, store them into a list and save them back to the database. List <AccountAssociations> newUserAssociations = new List <AccountAssociations>(); if (requestorAssociations.Count > 0) { AccountAssociations newAssociation; foreach (AccountAssociations item in requestorAssociations) { newAssociation = new AccountAssociations() { PrimaryUserID = FriendID, AssociatedUserID = item.AssociatedUserID, IsChild = item.IsChild }; newUserAssociations.Add(newAssociation); } //Save it to the database _context.AccountAssociations.AddRange(newUserAssociations); _context.SaveChanges(); } return(true); } catch (Exception) { _logger.LogError($"Failed to save relation for requestor id {RequestorID} with Friend ID {FriendID}."); throw new ApplicationException($"Failed to save relation for requestor id {RequestorID} with Friend ID {FriendID}."); } }
// Child Regiration public async Task <IActionResult> ChildRegistration(ChildrenViewModel model) { try { /* Get the parent profile. */ var parent = await _userManager.GetUserAsync(User); if (parent == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } /* Create user account. */ var child = new ApplicationUser { UserName = model.Username, Email = parent.Email, FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, IsChild = "1" }; var result = await _userManager.CreateAsync(child, model.Password); if (result.Succeeded) { /* Automatically confirm the childs email.*/ child.EmailConfirmed = true; _context.AccountUsers.Update(child); _context.SaveChanges(); /* Associate the child with their parent. */ var PrimaryUserAssociation = new AccountAssociations() { PrimaryUserID = parent.Id, AssociatedUserID = child.Id, IsChild = true }; _context.AccountAssociations.Add(PrimaryUserAssociation); _context.SaveChanges(); } //} return(RedirectToAction(nameof(Children))); } catch (Exception ex) { _logger.LogError(string.Format("{0} - Error Message: {1}", System.Reflection.MethodBase.GetCurrentMethod(), ex.Message)); return(RedirectToAction("Index", "StatusCode", 500)); } }
public async Task <IActionResult> RegisterAssociatedUser(RegisterViewModel model, string returnUrl = null) { try { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { /* Create user account. */ var user = new ApplicationUser { UserName = model.Username, Email = model.Email, FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, PhoneNumber = model.PhoneNumber }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); string websiteurl = Url.Action("Index", "Home"); string userDisplayName = string.Format("{0}", model.FirstName); await _emailSender.SendEmailConfirmationAsync(userDisplayName, model.Email, callbackUrl, websiteurl); /* Create relationship between requestor and requeste */ //if (model.IsLinkUserRegistration) //{ /* Create relationship between requestor and requeste */ string requestorID = model.RequestorID; string requesteID = user.Id; List <AccountAssociations> userAssociations = new List <AccountAssociations>(); var PrimaryUserAssociation = new AccountAssociations() { PrimaryUserID = requestorID, AssociatedUserID = requesteID }; var SecondaryUserAssociation = new AccountAssociations() { PrimaryUserID = requesteID, AssociatedUserID = requestorID }; _context.AccountAssociations.Add(PrimaryUserAssociation); _context.AccountAssociations.Add(SecondaryUserAssociation); _context.SaveChanges(); //} await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); return(RedirectToAction("Index", "Dashboard")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); } catch (Exception ex) { _logger.LogError(string.Format("{0} - Error Message: {1}", System.Reflection.MethodBase.GetCurrentMethod(), ex.Message)); return(RedirectToAction("Index", "StatusCode", 500)); } }
//[ValidateAntiForgeryToken] //[DisableFormValueModelBinding] public async Task <IActionResult> CreateChild(ChildrenViewModel model) { try { /* Get the parent profile. */ var parent = await _userManager.GetUserAsync(User); if (parent == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } /* Create user account. */ var child = new ApplicationUser { UserName = model.Username, Email = parent.Email, FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, IsChild = "1" }; var result = await _userManager.CreateAsync(child, model.Password); if (result.Succeeded) { // Create a blob client instance. CloudBlobClient blobClient = _cloudStorageAccount.CreateCloudBlobClient(); // Retrieve a reference to a container. CloudBlobContainer container = blobClient.GetContainerReference(_blobContainerName); // Create the container if it doesn't already exist. await container.CreateIfNotExistsAsync(); await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container }); // Retrieve reference to a blob named "myblob". CloudBlockBlob blockBlob = container.GetBlockBlobReference(model.ProfilePicture.FileName); // Upload photo to blob await blockBlob.UploadFromStreamAsync(model.ProfilePicture.OpenReadStream()); /* Automatically confirm the childs email and set the profile url.*/ child.EmailConfirmed = true; child.PhotoUrl = string.Format("{0}", blockBlob.Uri.OriginalString); _context.AccountUsers.Update(child); _context.SaveChanges(); /* Associate the child with their parent. */ var PrimaryUserAssociation = new AccountAssociations() { PrimaryUserID = parent.Id, AssociatedUserID = child.Id, IsChild = true }; _context.AccountAssociations.Add(PrimaryUserAssociation); _context.SaveChanges(); } return(RedirectToAction(nameof(Children))); } catch (Exception ex) { _logger.LogError(string.Format("{0} - Error Message: {1}", System.Reflection.MethodBase.GetCurrentMethod(), ex.Message)); return(RedirectToAction("Index", "StatusCode", 500)); } }