Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.DirectoryServices.AccountManagement;
- using System.Linq;
- using SupportPoint.BOL;
- using SupportPoint.DAL;
- using SupportPoint.DAL.GlobalValues;
- using SupportPoint.DAL.Helpers;
- using SupportPoint.DAL.ScriptEngine;
- namespace SupportPoint.CodeRuleLibrary.Integrations.SMS
- {
- public class ResetADPassword : IScriptRuleBaseActionCallBack<LoginVO, Incident>
- {
- private static string CreatePasswordString()
- {
- var r = new Random();
- return string.Concat(Words[r.Next(16)], r.Next(9999).ToString("0000"));
- }
- private static readonly string[] Words =
- {
- "Alfa","Bravo","Delta","Golf","Hotel","India","Kilo","Lima","Mike","November","Papa",
- "Romeo","Sierra","Tango","Uniform","Zulu" };
- #region IScriptBase<LoginVO,Incident> Members
- public RuleReturn Run(LoginVO l, Incident i, IDictionary<string, object> p)
- {
- try
- {
- var db = DataContextManager<SupportPointDataContext>.GetInstance();
- var ldapDS = new LDAPDS();
- string newPassword = !p.ContainsKey("pwd_value") || string.IsNullOrEmpty(Convert.ToString(p["pwd_value"])) ? CreatePasswordString() : Convert.ToString(p["pwd_value"]);
- var profile = db.Profiles.SingleOrDefault(pr => pr.EMail == i.ProfileSubset.EMail);
- if (profile == null || !profile.LDAPSettingID.HasValue)
- {
- new JournalDS().Create(new JournalVO
- {
- Text = string.Format("Feil: Profile not found or ExternalProfileID does not have value"),
- Public = false,
- Type = new JournalTypeDS().Single("Notes"),
- ParentType = SupportPointType.Incident
- }, l, i.IncidentID);
- return new RuleReturn
- {
- Script = "Ext.Msg.alert('Info','Profile not found or ExternalProfileID does not have value');",
- Refresh = false,
- Success = true
- };
- }
- var ldapVo = ldapDS.Single(profile.LDAPSettingID.Value);
- if (ldapVo == null)
- {
- new JournalDS().Create(new JournalVO
- {
- Text = string.Format("Feil: Passordet kan ikke resettes fordi brukeren ikke har en knytning til en gyldig Active Directory konfigurasjon."),
- Public = false,
- Type = new JournalTypeDS().Single("Notes"),
- ParentType = SupportPointType.Incident
- }, l, i.IncidentID);
- return new RuleReturn
- {
- Script = "Ext.Msg.alert('Info',' Passordet kan ikke resettes fordi brukeren ikke har en knytning til en gyldig Active Directory konfigurasjon.');",
- Refresh = false,
- Success = true
- };
- }
- var domain = ConvertPath(ldapVo.Path);
- var password = string.IsNullOrEmpty(ldapVo.Username) ? null : Encryption.Decrypt(ldapVo.Password);
- PrincipalContext context;
- try
- {
- context = new PrincipalContext(ContextType.Domain, domain, ldapVo.Username, password);
- }
- catch (Exception e)
- {
- new JournalDS().Create(new JournalVO
- {
- Text = string.Format("Feil: " + e.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path),
- Public = false,
- Type = new JournalTypeDS().Single("Notes"),
- ParentType = SupportPointType.Incident
- }, l, i.IncidentID);
- return new RuleReturn
- {
- Refresh = false,
- Success = true,
- Script = "Ext.Msg.alert('Info', +'" + e.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path + "');"
- };
- }
- if (!context.ValidateCredentials(ldapVo.Username, password, ContextOptions.Negotiate))
- {
- new JournalDS().Create(new JournalVO
- {
- Text = string.Format("Brukernavn og passord til AD er ikke godkjent."),
- Public = false,
- Type = new JournalTypeDS().Single("Notes"),
- ParentType = SupportPointType.Incident
- }, l, i.IncidentID);
- return new RuleReturn
- {
- Script = "Ext.Msg.alert('Info','Brukernavn og passord til AD er ikke godkjent.');",
- Refresh = false,
- Success = true
- };
- }
- UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, i.ProfileSubset.Username);
- if (user != null)
- {
- bool requireChange = p.ContainsKey("pwd_requirechange") && !string.IsNullOrEmpty(Convert.ToString(p["pwd_requirechange"])) && Convert.ToString(p["pwd_requirechange"]) == "true";
- bool fillSolution = p.ContainsKey("fill_solution") && !string.IsNullOrEmpty(Convert.ToString(p["fill_solution"])) && Convert.ToString(p["fill_solution"]) == "true";
- bool unlockOnly = p.ContainsKey("unlock_only") && !string.IsNullOrEmpty(Convert.ToString(p["unlock_only"])) && Convert.ToString(p["unlock_only"]) == "true";
- try
- {
- if (user.IsAccountLockedOut())
- user.UnlockAccount();
- if(!unlockOnly)
- user.SetPassword(newPassword);
- if (!unlockOnly && requireChange)
- user.ExpirePasswordNow();
- user.Save();
- }
- catch (PrincipalOperationException poex)
- {
- new JournalDS().Create(new JournalVO
- {
- Text = string.Format("PrincipalOperationException: " + poex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path),
- Public = false,
- Type = new JournalTypeDS().Single("Notes"),
- ParentType = SupportPointType.Incident
- }, l, i.IncidentID);
- return new RuleReturn
- {
- Refresh = false,
- Success = true,
- Script = "Ext.Msg.alert('Info', +'" + poex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path + "');"
- };
- }
- catch (PasswordException pex)
- {
- new JournalDS().Create(new JournalVO
- {
- Text = string.Format("PasswordException: " + pex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path),
- Public = false,
- Type = new JournalTypeDS().Single("Notes"),
- ParentType = SupportPointType.Incident
- }, l, i.IncidentID);
- return new RuleReturn
- {
- Refresh = false,
- Success = true,
- Script = "Ext.Msg.alert('Info', +'" + pex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path + "');"
- };
- }
- catch (InvalidOperationException ioex)
- {
- new JournalDS().Create(new JournalVO
- {
- Text = string.Format("InvalidOperationException: " + ioex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path),
- Public = false,
- Type = new JournalTypeDS().Single("Notes"),
- ParentType = SupportPointType.Incident
- }, l, i.IncidentID);
- return new RuleReturn
- {
- Refresh = false,
- Success = true,
- Script = "Ext.Msg.alert('Info', +'" + ioex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path + "');"
- };
- }
- var hidePassword = Globals.Instance["SMS.Integration.PasswordMessage.HidePassword"].TryParseBool();
- var parentId = hidePassword ? -1 : i.IncidentID;
- if (!string.IsNullOrEmpty(i.ProfileSubset.Phone_Mobile) || p.ContainsKey("phone_number"))
- {
- var integrationLogin = new LoginHandler().IntegrationLogin();
- new SMSDS().Create(
- new SMSVO
- {
- SentDateTime = DateTime.Now,
- CreatedBy = integrationLogin.LoginName,
- Direction = "Outbound",
- From = integrationLogin.LoginName,
- Sent = false,
- To = string.IsNullOrEmpty(i.ProfileSubset.Phone_Mobile) ? Convert.ToString(p["phone_number"]).Replace(" ","").Replace("+","") : i.ProfileSubset.Phone_Mobile.Replace(" ","").Replace("+",""),
- Text = Globals.Instance["SMS.Integration.PasswordMessage"].Inject(new { Password = newPassword }),
- CreatedDateTime = DateTime.Now,
- ParentID = i.IncidentID,
- ParentType = SupportPointType.Incident
- },
- parentId);
- }
- if (string.IsNullOrEmpty(i.Solution) || fillSolution)
- {
- i.Solution =
- Globals.Instance["SMS.Integration.Incident.Solution"].Inject(new {Password = newPassword})
- .Inject(i);
- }
- var journalText = hidePassword
- ? "Password is reset. The SMS will not be displayed on this incident"
- : string.Format("Password reset to '{0}'", newPassword)
- + (requireChange ? " with require change on next logon" : "");
- i.Status = StatusQueries.SingleIDResult(db,7);
- i.RespondedDateTime = DateTime.Now;
- i.RespondedBy = "Integration";
- i.ResolvedDateTime = DateTime.Now;
- i.ResolvedBy = "Integration";
- var aDs = new AuditDS();
- aDs.AuditChanges(db, i.IncidentID, i, l);
- new JournalDS().Create(new JournalVO
- {
- Text = journalText,
- Public = false,
- Type = new JournalTypeDS().Single("Notes"),
- ParentType = SupportPointType.Incident
- }, l, i.IncidentID);
- db.SubmitChanges();
- return new RuleReturn
- {
- Refresh = false,
- Success = true,
- Script = "Ext.Msg.alert('Info', 'Passord er byttet');Ext.getCmp('incident_journals').getStore().reload();Ext.getCmp('incident_emails').getStore().reload();Ext.getCmp('incident_solution').setValue('Passord byttet'); Ext.getCmp('incident_doResolve').setValue('yes');Ext.getCmp('incident_status').setValue('60: Løst');"
- };
- }
- new JournalDS().Create(new JournalVO
- {
- Text = string.Format("Fant ikke bruker"),
- Public = false,
- Type = new JournalTypeDS().Single("Notes"),
- ParentType = SupportPointType.Incident
- }, l, i.IncidentID);
- return new RuleReturn
- {
- Refresh = false,
- Success = true,
- Script = "Ext.Msg.alert('Info', 'Fant ikke bruker');"
- };
- }
- catch (Exception e)
- {
- new JournalDS().Create(new JournalVO
- {
- Text = string.Format("Ukjent feil: " + e.StackTrace + "<br/>" + e.Message + "<br/>Inner exception:" + e.InnerException.Message),
- Public = false,
- Type = new JournalTypeDS().Single("Notes"),
- ParentType = SupportPointType.Incident
- }, l, i.IncidentID);
- return new RuleReturn
- {
- Script = "Ext.Msg.alert('Info', '" + e.StackTrace + "<br/>" + e.Message + "<br/>');",
- Refresh = false,
- Success = true
- };
- }
- }
- private static string ConvertPath(string path)
- {
- path = path.ToUpper().Replace("LDAP://", "");
- if (path.Contains('/'))
- path = path.Split(new[] { '/' })[0];
- return path;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement