/// <summary> /// Create new budget access right /// </summary> /// <param name="id">Id of budget where budget access right belongs</param> /// <returns></returns> public ActionResult Create(Guid id) { // creating new CreateBudgetAccessRightModel instance var model = new CreateBudgetAccessRightModel { BudgetId = id, Permissions = this.GetPermissions() }; return this.View(model); }
/// <summary> /// Sends email to user with link which can be used to share desired budget. /// </summary> /// <param name="model">budget access right model with set id, email and permission</param> private async Task SendRequest(CreateBudgetAccessRightModel model) { var confirmationData = getConfirmationString(model); var callbackUrl = Url.Action(BudgetAccessRightConstant.ConfirmRequest, BudgetAccessRightConstant.BudgetAccessRight, new { budgetId = model.BudgetId, userEmail = model.AssignedUserEmail, permission = model.Permission, hash=confirmationData.GetHashCode() }, protocol: Request.Url.Scheme); var message = new MailMessage(); message.To.Add(new MailAddress(model.AssignedUserEmail)); message.Subject = BudgetAccessRightResource.EmailSubject; message.Body = string.Format(BudgetAccessRightResource.EmailInvitation, User.Identity.Name, callbackUrl.ToString()); message.IsBodyHtml = true; using (var smtp = new SmtpClient()) { var credential = new NetworkCredential { UserName = ConfigurationManager.AppSettings["GoogleUserName"], Password = ConfigurationManager.AppSettings["GooglePassword"] }; smtp.Credentials = credential; await smtp.SendMailAsync(message); } }
/// <summary> /// Prepares string with ApplicationId, budget id, assigned user id and selected permission. /// </summary> /// <param name="model">budget access right model with set id, email and permission</param> /// <returns>string with ApplicationId, budget id, assigned user id and selected permission</returns> private string getConfirmationString(CreateBudgetAccessRightModel model) { return String.Join( Environment.NewLine, new[] { ConfigurationManager.AppSettings["ApplicationId"], model.BudgetId.ToString(), model.AssignedUserEmail, model.Permission.ToString(), }); }
public async Task<ActionResult> Create(CreateBudgetAccessRightModel model) { this.IsCaptchaValid(SharedResource.CaptchaValidationFailed); // checking if model is valid if (!ModelState.IsValid) { this.AddError(SharedResource.ModelStateIsNotValid); model.Permissions = this.GetPermissions(); return this.View(model); } var userId = await this.GetUserProfileByEmail(model.AssignedUserEmail); try { await SendRequest(model); this.AddSuccess(string.Format(BudgetAccessRightResource.RequestSent, model.AssignedUserEmail)); return RedirectToAction(SharedConstant.Index, new { id = model.BudgetId }); } catch (ServiceValidationException exception) { ModelState.AddModelErrors(exception); model.Permissions = this.GetPermissions(); return this.View(model); } }