using System; using System.Linq; using System.Net; using System.Web; using System.Web.UI.WebControls; using CMS.DataCom; using CMS.ExtendedControls.ActionsConfig; using CMS.FormControls; using CMS.WorkflowEngine; using CMS.GlobalHelper; using CMS.SettingsProvider; public partial class CMSModules_ContactManagement_FormControls_DataComLogin : FormEngineUserControl { #region "Variables" private WorkflowStepInfo mWorkflowStep; private NetworkCredential mCredential; #endregion #region "Properties" /// /// Value of the control - Data.com UserName. /// public override object Value { get { return Credential.UserName; } set { } } /// /// Returns redirect URL to login page. /// private string LoginPageUrl { get { var url = "~/CMSModules/ContactManagement/Pages/Tools/DataCom/Login.aspx"; url = URLHelper.AddParameterToUrl(url, "returnurl", HttpUtility.UrlEncode(URLHelper.CurrentURL)); url = URLHelper.AddParameterToUrl(url, "workflowstepid", QueryHelper.GetString("workflowstepid", "")); return url; } } /// /// Returns workflow step. /// private WorkflowStepInfo WorkflowStep { get { return mWorkflowStep ?? (mWorkflowStep = WorkflowStepInfoProvider.GetWorkflowStepInfo(QueryHelper.GetInteger("workflowstepid", 0))); } } /// /// Returns credential saved in step. Null if no credentials were saved. /// private NetworkCredential Credential { get { if (mCredential == null) { mCredential = new StepCredentialProvider(WorkflowStep).GetCredential(); } return mCredential; } } #endregion #region "Methods" protected override void OnLoad(EventArgs e) { HeaderActions.ActionPerformed += HeaderActions_ActionPerformed; ShowInfo(); base.OnLoad(e); } /// /// Shows information and initializes remove action. /// private void ShowInfo() { bool validCredentials = false; try { validCredentials = CheckCredential(Credential); } catch (Exception ex) { DisableSaveAction(); LogAndShowError(String.Format(GetString("datacom.serviceexception")), "DATACOM", ex); return; } if (validCredentials) { ShowInformation(string.Format(GetString("datacom.automation.stepisusingcredentials"), Credential.UserName)); } else { if (Credential == null) { URLHelper.Redirect(LoginPageUrl); return; } ShowWarning(GetString("datacom.automation.stepisusinginvalidcredentials"), null, null); DisableSaveAction(); } InitializeRemoveAction(); } /// /// Adds logout button to header actions. /// private void InitializeRemoveAction() { AddHeaderAction(new HeaderAction { Text = string.Format(GetString("datacom.automation.removecredentials"), Credential.UserName), GenerateSeparatorBeforeAction = true, ImageUrl = GetImageUrl("CMSModules/CMS_PortalEngine/OnSiteEdit/signout.png"), CommandName = "remove", OnClientClick = "return confirm('" + GetString("datacom.automation.confirmremoval") + "')" }); } protected void HeaderActions_ActionPerformed(object sender, CommandEventArgs e) { switch (e.CommandName.ToLowerInvariant()) { case "remove": var provider = new StepCredentialProvider(WorkflowStep); provider.SetCredential(new NetworkCredential()); URLHelper.Redirect(LoginPageUrl); break; case "save": // Raises the save event of form. This must be used instead of Form.SaveData(), because this control is used in // Marketing Automaton step, where multiple forms are present and Form.SaveData() simply doesn't work. ComponentEvents.RequestEvents.RaiseComponentEvent(this, e, ComponentName, ComponentEvents.SAVE_DATA); ComponentEvents.RequestEvents.RaiseComponentEvent(this, e, ComponentName, ComponentEvents.SAVE); URLHelper.Redirect(URLHelper.AddParameterToUrl(URLHelper.CurrentURL, "saved", "1")); break; } } /// /// Checks if given user's DataCom credentials are valid. Makes API call to Data.com service. /// /// User to check credentials for /// Returns true if valid private bool CheckCredential(NetworkCredential credential) { if (credential == null || String.IsNullOrEmpty(credential.UserName)) { return false; } DataComClient client = DataComHelper.CreateClient(); return client.UserIsValid(credential); } /// /// Disables save action. /// private void DisableSaveAction() { if (HeaderActions.ActionsList.Count > 0) { var saveAction = HeaderActions.ActionsList.FirstOrDefault(t => t.CommandName.ToLowerCSafe() == "save"); if (saveAction != null) { saveAction.Enabled = false; } } } #endregion }