public async Task <ActionResult> Save(InviteTemplate template) { if (ModelState.IsValid) { //check for subject template if smtp if (Settings.UseSMTP) { if (string.IsNullOrEmpty(template.SubjectTemplate)) { ModelState.AddModelError("SubjectTemplate", "With SMTP enabled, the subject template is required."); return(View("Edit", template)); } } try { template.TemplateAuthor = User.Identity.GetEmail(); if (template.Id == null) { template = await TemplateUtilities.AddTemplate(template); } else { template = await TemplateUtilities.UpdateTemplate(template); } return(RedirectToAction("Index")); } catch { return(View("Edit", template)); } } return(View("Edit", template)); }
public static async Task <IEnumerable <InviteTemplate> > InitializeDefaultTemplate(string templateAuthor) { var template = new InviteTemplate { LastUpdated = DateTime.UtcNow, TemplateAuthor = templateAuthor, TemplateName = "Default", TemplateVersion = 1 }; template.SubjectTemplate = AdalUtil.Settings.InvitationEmailSubject; if (Settings.UseSMTP) { template.TemplateContent = Settings.GetMailTemplate(AdalUtil.Settings.DefaultBodyTemplateName); } else { template.TemplateContent = ""; } template = (await DocDBRepo.DB <InviteTemplate> .CreateItemAsync(template)); var res = new List <InviteTemplate>(); res.Add(template); return(res); }
public static async Task <InviteTemplate> UpdateTemplate(InviteTemplate template) { template.LastUpdated = DateTime.UtcNow; template.TemplateVersion++; //TOSDocument is decorated with [AllowHtml], so clearing out dangerous tags template.TemplateContent = HtmlSanitizer.SanitizeHtml(template.TemplateContent); template = (await DocDBRepo.DB <InviteTemplate> .UpdateItemAsync(template)); return(template); }
public async Task <ActionResult> Delete(InviteTemplate template) { try { await TemplateUtilities.DeleteTemplate(template); return(RedirectToAction("Index")); } catch { ViewBag.Operation = "Delete"; return(RedirectToAction("Index")); } }
public async Task <ActionResult> Edit(string id) { InviteTemplate template; if (id == null) { ViewBag.Operation = "New"; template = new InviteTemplate(); } else { ViewBag.Operation = "Edit"; template = await TemplateUtilities.GetTemplate(id); } return(View(template)); }
public static async Task <IEnumerable <InviteTemplate> > InitializeDefaultTemplate(string templateAuthor) { var template = new InviteTemplate { LastUpdated = DateTime.UtcNow, TemplateAuthor = templateAuthor, TemplateName = "Default", TemplateVersion = 1 }; template.SubjectTemplate = MailSender.GetTemplateContents(Settings.DefaultSubjectTemplateName); template.TemplateContent = MailSender.GetTemplateContents(Settings.DefaultBodyTemplateName); template = (await DocDBRepo.DB <InviteTemplate> .CreateItemAsync(template)); var res = new List <InviteTemplate> { template }; return(res); }
/// <summary> /// Load latest site configuration record from the database. /// </summary> /// <returns>false if no record found, true indicates the latest record is available in Settings.CurrSiteConfig</returns> public static async Task <bool> LoadCurrSiteConfig() { try { Settings.CurrSiteConfig = await GetCurrConfig(); if (Settings.CurrSiteConfig != null) { if (Settings.CurrSiteConfig.InviteTemplateId != null) { Settings.CurrSiteConfig.InviteTemplateContent = await InviteTemplate.GetTemplate(Settings.CurrSiteConfig.InviteTemplateId); } } Settings.SiteConfigReady = (Settings.CurrSiteConfig != null); return(Settings.SiteConfigReady); } catch (Exception ex) { Logging.WriteToAppLog("Problem loading site config", System.Diagnostics.EventLogEntryType.Error, ex); Settings.SiteConfigReady = false; return(Settings.SiteConfigReady); } }
/// <summary> /// Load latest site configuration record from the database. /// </summary> /// <returns>false if no record found, true indicates the latest record is available in Settings.CurrSiteConfig</returns> public static bool LoadCurrSiteConfig() { try { CurrSiteConfig = DocDBRepo.DB <SiteConfig> .GetItemsAsync(c => c.DocType == DocTypes.SiteConfig).Result.LastOrDefault(); if (CurrSiteConfig != null) { if (CurrSiteConfig.InviteTemplateId != null) { CurrSiteConfig.InviteTemplateContent = InviteTemplate.GetTemplate(CurrSiteConfig.InviteTemplateId).GetAwaiter().GetResult(); } } SiteConfigReady = (CurrSiteConfig != null); return(SiteConfigReady); } catch (Exception ex) { Logging.WriteToAppLog("Problem loading site config", System.Diagnostics.EventLogEntryType.Error, ex); SiteConfigReady = false; return(SiteConfigReady); } }
public PreAuthDomain() { DomainRedemptionSettings = new RedemptionSettings(); InviteTemplateContent = new InviteTemplate(); Groups = new List <GroupObject>(); }
public SiteConfig() { SiteRedemptionSettings = new RedemptionSettings(); SiteRedemptionSettings.InviteRedirectUrl = string.Format("https://myapps.microsoft.com/{0}", Settings.Tenant); InviteTemplateContent = new InviteTemplate(); }
private static string FormatEmailBody(GraphInvitation data, RedemptionSettings redemption, InviteTemplate content) { var body = content.TemplateContent; body = body.Replace("{{InvitingOrgName}}", Settings.InvitingOrganization); body = body.Replace("{{InvitationLink}}", data.InviteRedeemUrl); body = body.Replace("{{OrgContactEmail}}", redemption.InviterResponseEmailAddr); return(body); }
private static string FormatEmailBody(GraphInvitation invitation, string inviterResponseEmailAddr, InviteTemplate mailTemplate) { var body = mailTemplate.TemplateContent; body = body.Replace("{{InvitingOrgName}}", Settings.CurrSiteConfig.InvitingOrg); body = body.Replace("{{InvitationLink}}", invitation.InviteRedeemUrl); body = body.Replace("{{InvitationStatus}}", invitation.Status); if (invitation.InvitedUserMessageInfo != null) { body = body.Replace("{{CustomMessage}}", invitation.InvitedUserMessageInfo.CustomizedMessageBody); } body = body.Replace("{{OrgContactEmail}}", inviterResponseEmailAddr); return(body); }
private async Task <GraphInvitation> SendGraphInvitationAsync(GraphInvitation invitation, List <GroupObject> groups, string inviterResponseEmailAddr = null, InviteTemplate mailTemplate = null, string accessToken = null) { AdalResponse serverResponse = null; GraphInvitation responseData = new GraphInvitation(); try { var inviteEndPoint = string.Format("{0}/{1}/invitations", Settings.GraphResource, Settings.GraphApiVersion); // Invite user. Your app needs to have User.ReadWrite.All or Directory.ReadWrite.All to invite serverResponse = AdalUtil.CallGraph(inviteEndPoint, invitation, false, null, _user, accessToken); responseData = JsonConvert.DeserializeObject <GraphInvitation>(serverResponse.ResponseContent); if (responseData.id == null) { responseData.ErrorInfo = string.Format("Error: Invite not sent - API error: {0}", serverResponse.Message); return(responseData); } if (groups.Count > 0) { var groupsAdded = AddUserToGroup(responseData.InvitedUser.Id, groups); if (!groupsAdded.Success) { var resErrors = string.Join(", ", groupsAdded.Responses.Where(r => !r.Successful).Select(r => r.Message)); responseData.ErrorInfo += string.Format("\n\rOne or more groups failed while assigning to user \"{0}\" ({1})", responseData.InvitedUserEmailAddress, resErrors); } } if (Settings.UseSMTP) { mailTemplate = mailTemplate ?? Settings.CurrSiteConfig.InviteTemplateContent; var emailSubject = mailTemplate.SubjectTemplate.Replace("{{InvitingOrgName}}", Settings.CurrSiteConfig.InvitingOrg); string body = FormatEmailBody(responseData, inviterResponseEmailAddr, mailTemplate); SendViaSMTP(emailSubject, body, invitation.InvitedUserEmailAddress); } var request = await GuestRequestRules.GetUserAsync(invitation.InvitedUserEmailAddress); request.InvitationResult = responseData; await GuestRequestRules.UpdateAsync(request); return(responseData); } catch (Exception ex) { var reason = (serverResponse == null ? "N/A" : serverResponse.ResponseContent); responseData.ErrorInfo = string.Format("Error: {0}<br>Server response: {1}", ex.Message, reason); return(responseData); } }
public static async Task <dynamic> DeleteTemplate(InviteTemplate template) { return(await DocDBRepo.DB <InviteTemplate> .DeleteItemAsync(template)); }
public static async Task <InviteTemplate> AddTemplate(InviteTemplate template) { template.LastUpdated = DateTime.UtcNow; return(await DocDBRepo.DB <InviteTemplate> .CreateItemAsync(template)); }
/// <summary> /// GraphInvitation is a model from https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/resources/invitation /// Do not alter /// </summary> /// <param name="invitation">The GraphInvitation object transmitted to the B2B Guest API</param> /// <param name="groups">Azure AD groups to assign the guest user to</param> /// <param name="inviterResponseEmailAddr"></param> /// <param name="mailTemplate"></param> /// <param name="accessToken"></param> /// <returns></returns> private async Task <GuestRequest> SendGraphInvitationAsync(GraphInvitation invitation, List <GroupObject> groups, GuestRequest request, string inviterResponseEmailAddr = null, InviteTemplate mailTemplate = null, string accessToken = null) { AdalResponse serverResponse = null; GraphInvitation responseData = new GraphInvitation(); try { var inviteEndPoint = string.Format("{0}/{1}/invitations", Settings.GraphResource, Settings.GraphApiVersion); // Invite user. Your app needs to have User.ReadWrite.All or Directory.ReadWrite.All to invite serverResponse = AdalUtil.CallGraph(inviteEndPoint, invitation, false, null, _user, accessToken); responseData = JsonConvert.DeserializeObject <GraphInvitation>(serverResponse.ResponseContent); request.InvitationResult = responseData; request.Status = responseData.Status; if (responseData.id == null) { //API call failed - put the request back in the queue responseData.ErrorInfo = string.Format("Error: Invite not sent - API error: {0}", serverResponse.Message); request.InvitationResult = responseData; request.Disposition = Disposition.Pending; } else { //invitation API call successful if (responseData.Status.Substring(0, 5) == "Error") { //API call was successful but something failed while trying to process the request - put the request back in the queue request.Disposition = Disposition.Pending; } else { //check to see if groups should be assigned if (groups.Count > 0) { try { var groupsAdded = AddUserToGroup(responseData.InvitedUser.Id, groups); if (!groupsAdded.Success) { //group assignment failed var resErrors = string.Join(", ", groupsAdded.Responses.Where(r => !r.Successful).Select(r => r.Message)); var msg = string.Format("\n\rOne or more groups failed while assigning to user \"{0}\" ({1})", responseData.InvitedUserEmailAddress, resErrors); request.PostProcessingStatus += msg; } } catch (Exception groupEx) { //group assignment errored var msg = string.Format("\n\rAn error occured while assigning a group to user \"{0}\" ({1})", responseData.InvitedUserEmailAddress, groupEx.Message); Logging.WriteToAppLog(msg, EventLogEntryType.Warning, groupEx); request.PostProcessingStatus += msg; } } //check to see if custom mail should be sent if (Settings.UseSMTP) { try { mailTemplate = mailTemplate ?? Settings.CurrSiteConfig.InviteTemplateContent; var emailSubject = mailTemplate.SubjectTemplate.Replace("{{InvitingOrgName}}", Settings.CurrSiteConfig.InvitingOrg); string body = FormatEmailBody(responseData, inviterResponseEmailAddr, mailTemplate); SendViaSMTP(emailSubject, body, invitation.InvitedUserEmailAddress); request.MailSent = true; } catch (Exception mailEx) { var msg = string.Format("\n\rSending invitation failed for user \"{0}\" ({1})", responseData.InvitedUserEmailAddress, mailEx.Message); Logging.WriteToAppLog(msg, EventLogEntryType.Warning, mailEx); request.PostProcessingStatus += msg; } } } } //all attempts complete, UPDATE GuestRequest await GuestRequestRules.UpdateAsync(request); return(request); } catch (Exception ex) { var reason = (serverResponse == null ? "N/A" : serverResponse.ResponseContent); responseData.ErrorInfo = string.Format("Error: {0}<br>Server response: {1}", ex.Message, reason); request.InvitationResult = responseData; //UPDATE GuestRequest await GuestRequestRules.UpdateAsync(request); return(request); } }