public async Task <GroupAccessCodeModel> NewGroupAccessCode(GroupAccessCodeModel info) { info.Type = InvitationType.AddMeToGroup; info.Code = Guid.NewGuid().ToString(); info.WhenCreated = DateTime.UtcNow; info.WhenExpires = DateTime.UtcNow.AddDays(1); await _dbContext.GroupAccessCodes.AddAsync(info); await _dbContext.SaveChangesAsync(); return(info); }
public async Task ConsumeAccessCodeAsync(UserProfileModel user, GroupAccessCodeModel c) { if (c.WhenExpires >= DateTime.UtcNow) { if (c.Type == InvitationType.AddMeToGroup) { await _userManager.AddToRoleAsync(user, c.GroupName); } c.WhenConsumed = DateTime.UtcNow; if (c.ForUserId == 0) { c.ForUserId = user.Id; } await _dbContext.SaveChangesAsync(); } }
public async Task <GroupAccessCodeModel> NewInvitation(int createdByUserId, InvitationType type, int forUserId, string message, int daysValidFor) { var info = new GroupAccessCodeModel() { Type = InvitationType.Register, CreatedByUserId = createdByUserId, ForUserId = forUserId, GroupName = message, Code = Guid.NewGuid().ToString(), WhenCreated = DateTime.UtcNow, WhenExpires = DateTime.UtcNow.AddDays(daysValidFor) }; await _dbContext.GroupAccessCodes.AddAsync(info); await _dbContext.SaveChangesAsync(); return(info); }
public async Task <IActionResult> Register(string returnUrl, string code, [Bind] RegisterInputModel input) { GroupAccessCodeModel accessCode = null; var isInvitationOnly = await _featureManager.IsEnabledAsync(nameof(FeatureFlags.FunctionUserRegisterInvitationOnly)); if (!string.IsNullOrWhiteSpace(code)) { accessCode = await _invitationService.GetAccessCodeAsync(code, null, InvitationType.Register); if (accessCode == null && isInvitationOnly) { return(Redirect("/Error/404")); } } else if (isInvitationOnly) { return(Redirect("/Error/404")); } returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = await _localProfiles.CreateUser(input, ModelState); if (ModelState.IsValid) { var result = await _userManager.CreateAsync(user, input.Password); if (result.Succeeded) { await _localProfiles.MakeFirstUserDev(user); if (accessCode != null) { // TODO: send notification that the invite was claimed await _invitationService.ConsumeAccessCodeAsync(user, accessCode); } // await eventLog.Log(user.Id, EventNames.Account.Register.Success, null); var emailCode = await _userManager.GenerateEmailConfirmationTokenAsync(user); if (_environment.IsProduction()) { var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = emailCode }, protocol: Request.Scheme); // TODO: replace with View() of email //await _emailSender.SendEmailAsync(input.Email, "PinkUmbrella: Confirm your email", //$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); } else { var result2 = await _userManager.ConfirmEmailAsync(user, emailCode); if (!result2.Succeeded) { //AddErrors(result2); return(View("ConfirmEmail")); } } await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } } // await eventLog.Log(-1, EventNames.Account.Register.Error, null); // If we got this far, something failed, redisplay form ViewData["ReturnUrl"] = returnUrl; ViewData["Controller"] = "Account"; ViewData["Action"] = nameof(Register); return(View(new RegisterViewModel() { ReturnUrl = returnUrl, Input = input, Code = code, })); }