Advertisement
Guest User

Untitled

a guest
Apr 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.DirectoryServices.AccountManagement;
  4. using System.Linq;
  5.  
  6. using SupportPoint.BOL;
  7. using SupportPoint.DAL;
  8. using SupportPoint.DAL.GlobalValues;
  9. using SupportPoint.DAL.Helpers;
  10. using SupportPoint.DAL.ScriptEngine;
  11.  
  12. namespace SupportPoint.CodeRuleLibrary.Integrations.SMS
  13. {
  14. public class ResetADPassword : IScriptRuleBaseActionCallBack<LoginVO, Incident>
  15. {
  16. private static string CreatePasswordString()
  17. {
  18. var r = new Random();
  19. return string.Concat(Words[r.Next(16)], r.Next(9999).ToString("0000"));
  20. }
  21.  
  22. private static readonly string[] Words =
  23. {
  24. "Alfa","Bravo","Delta","Golf","Hotel","India","Kilo","Lima","Mike","November","Papa",
  25. "Romeo","Sierra","Tango","Uniform","Zulu" };
  26.  
  27. #region IScriptBase<LoginVO,Incident> Members
  28.  
  29. public RuleReturn Run(LoginVO l, Incident i, IDictionary<string, object> p)
  30. {
  31. try
  32. {
  33. var db = DataContextManager<SupportPointDataContext>.GetInstance();
  34. var ldapDS = new LDAPDS();
  35.  
  36. string newPassword = !p.ContainsKey("pwd_value") || string.IsNullOrEmpty(Convert.ToString(p["pwd_value"])) ? CreatePasswordString() : Convert.ToString(p["pwd_value"]);
  37.  
  38. var profile = db.Profiles.SingleOrDefault(pr => pr.EMail == i.ProfileSubset.EMail);
  39.  
  40. if (profile == null || !profile.LDAPSettingID.HasValue)
  41. {
  42. new JournalDS().Create(new JournalVO
  43. {
  44. Text = string.Format("Feil: Profile not found or ExternalProfileID does not have value"),
  45. Public = false,
  46. Type = new JournalTypeDS().Single("Notes"),
  47. ParentType = SupportPointType.Incident
  48. }, l, i.IncidentID);
  49.  
  50. return new RuleReturn
  51. {
  52. Script = "Ext.Msg.alert('Info','Profile not found or ExternalProfileID does not have value');",
  53. Refresh = false,
  54. Success = true
  55. };
  56. }
  57.  
  58. var ldapVo = ldapDS.Single(profile.LDAPSettingID.Value);
  59.  
  60. if (ldapVo == null)
  61. {
  62. new JournalDS().Create(new JournalVO
  63. {
  64. Text = string.Format("Feil: Passordet kan ikke resettes fordi brukeren ikke har en knytning til en gyldig Active Directory konfigurasjon."),
  65. Public = false,
  66. Type = new JournalTypeDS().Single("Notes"),
  67. ParentType = SupportPointType.Incident
  68. }, l, i.IncidentID);
  69. return new RuleReturn
  70. {
  71. Script = "Ext.Msg.alert('Info',' Passordet kan ikke resettes fordi brukeren ikke har en knytning til en gyldig Active Directory konfigurasjon.');",
  72. Refresh = false,
  73. Success = true
  74. };
  75. }
  76.  
  77. var domain = ConvertPath(ldapVo.Path);
  78.  
  79. var password = string.IsNullOrEmpty(ldapVo.Username) ? null : Encryption.Decrypt(ldapVo.Password);
  80.  
  81. PrincipalContext context;
  82. try
  83. {
  84. context = new PrincipalContext(ContextType.Domain, domain, ldapVo.Username, password);
  85. }
  86. catch (Exception e)
  87. {
  88. new JournalDS().Create(new JournalVO
  89. {
  90. Text = string.Format("Feil: " + e.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path),
  91. Public = false,
  92. Type = new JournalTypeDS().Single("Notes"),
  93. ParentType = SupportPointType.Incident
  94. }, l, i.IncidentID);
  95.  
  96. return new RuleReturn
  97. {
  98. Refresh = false,
  99. Success = true,
  100. Script = "Ext.Msg.alert('Info', +'" + e.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path + "');"
  101. };
  102. }
  103.  
  104. if (!context.ValidateCredentials(ldapVo.Username, password, ContextOptions.Negotiate))
  105. {
  106. new JournalDS().Create(new JournalVO
  107. {
  108. Text = string.Format("Brukernavn og passord til AD er ikke godkjent."),
  109. Public = false,
  110. Type = new JournalTypeDS().Single("Notes"),
  111. ParentType = SupportPointType.Incident
  112. }, l, i.IncidentID);
  113.  
  114. return new RuleReturn
  115. {
  116. Script = "Ext.Msg.alert('Info','Brukernavn og passord til AD er ikke godkjent.');",
  117. Refresh = false,
  118. Success = true
  119. };
  120. }
  121.  
  122. UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, i.ProfileSubset.Username);
  123.  
  124. if (user != null)
  125. {
  126. bool requireChange = p.ContainsKey("pwd_requirechange") && !string.IsNullOrEmpty(Convert.ToString(p["pwd_requirechange"])) && Convert.ToString(p["pwd_requirechange"]) == "true";
  127. bool fillSolution = p.ContainsKey("fill_solution") && !string.IsNullOrEmpty(Convert.ToString(p["fill_solution"])) && Convert.ToString(p["fill_solution"]) == "true";
  128. bool unlockOnly = p.ContainsKey("unlock_only") && !string.IsNullOrEmpty(Convert.ToString(p["unlock_only"])) && Convert.ToString(p["unlock_only"]) == "true";
  129. try
  130. {
  131. if (user.IsAccountLockedOut())
  132. user.UnlockAccount();
  133.  
  134. if(!unlockOnly)
  135. user.SetPassword(newPassword);
  136.  
  137. if (!unlockOnly && requireChange)
  138. user.ExpirePasswordNow();
  139.  
  140. user.Save();
  141. }
  142. catch (PrincipalOperationException poex)
  143. {
  144. new JournalDS().Create(new JournalVO
  145. {
  146. Text = string.Format("PrincipalOperationException: " + poex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path),
  147. Public = false,
  148. Type = new JournalTypeDS().Single("Notes"),
  149. ParentType = SupportPointType.Incident
  150. }, l, i.IncidentID);
  151.  
  152. return new RuleReturn
  153. {
  154. Refresh = false,
  155. Success = true,
  156. Script = "Ext.Msg.alert('Info', +'" + poex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path + "');"
  157. };
  158. }
  159. catch (PasswordException pex)
  160. {
  161. new JournalDS().Create(new JournalVO
  162. {
  163. Text = string.Format("PasswordException: " + pex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path),
  164. Public = false,
  165. Type = new JournalTypeDS().Single("Notes"),
  166. ParentType = SupportPointType.Incident
  167. }, l, i.IncidentID);
  168.  
  169. return new RuleReturn
  170. {
  171. Refresh = false,
  172. Success = true,
  173. Script = "Ext.Msg.alert('Info', +'" + pex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path + "');"
  174. };
  175. }
  176. catch (InvalidOperationException ioex)
  177. {
  178. new JournalDS().Create(new JournalVO
  179. {
  180. Text = string.Format("InvalidOperationException: " + ioex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path),
  181. Public = false,
  182. Type = new JournalTypeDS().Single("Notes"),
  183. ParentType = SupportPointType.Incident
  184. }, l, i.IncidentID);
  185.  
  186. return new RuleReturn
  187. {
  188. Refresh = false,
  189. Success = true,
  190. Script = "Ext.Msg.alert('Info', +'" + ioex.Message + "<br/>User: " + ldapVo.Username + "<br/>Domain: " + domain + "<br/>Path: " + ldapVo.Path + "');"
  191. };
  192. }
  193.  
  194. var hidePassword = Globals.Instance["SMS.Integration.PasswordMessage.HidePassword"].TryParseBool();
  195.  
  196. var parentId = hidePassword ? -1 : i.IncidentID;
  197.  
  198. if (!string.IsNullOrEmpty(i.ProfileSubset.Phone_Mobile) || p.ContainsKey("phone_number"))
  199. {
  200. var integrationLogin = new LoginHandler().IntegrationLogin();
  201. new SMSDS().Create(
  202. new SMSVO
  203. {
  204. SentDateTime = DateTime.Now,
  205. CreatedBy = integrationLogin.LoginName,
  206. Direction = "Outbound",
  207. From = integrationLogin.LoginName,
  208. Sent = false,
  209. To = string.IsNullOrEmpty(i.ProfileSubset.Phone_Mobile) ? Convert.ToString(p["phone_number"]).Replace(" ","").Replace("+","") : i.ProfileSubset.Phone_Mobile.Replace(" ","").Replace("+",""),
  210. Text = Globals.Instance["SMS.Integration.PasswordMessage"].Inject(new { Password = newPassword }),
  211. CreatedDateTime = DateTime.Now,
  212. ParentID = i.IncidentID,
  213. ParentType = SupportPointType.Incident
  214. },
  215. parentId);
  216. }
  217.  
  218.  
  219. if (string.IsNullOrEmpty(i.Solution) || fillSolution)
  220. {
  221. i.Solution =
  222. Globals.Instance["SMS.Integration.Incident.Solution"].Inject(new {Password = newPassword})
  223. .Inject(i);
  224. }
  225.  
  226. var journalText = hidePassword
  227. ? "Password is reset. The SMS will not be displayed on this incident"
  228. : string.Format("Password reset to '{0}'", newPassword)
  229. + (requireChange ? " with require change on next logon" : "");
  230.  
  231. i.Status = StatusQueries.SingleIDResult(db,7);
  232. i.RespondedDateTime = DateTime.Now;
  233. i.RespondedBy = "Integration";
  234.  
  235. i.ResolvedDateTime = DateTime.Now;
  236. i.ResolvedBy = "Integration";
  237.  
  238. var aDs = new AuditDS();
  239. aDs.AuditChanges(db, i.IncidentID, i, l);
  240.  
  241. new JournalDS().Create(new JournalVO
  242. {
  243. Text = journalText,
  244. Public = false,
  245. Type = new JournalTypeDS().Single("Notes"),
  246. ParentType = SupportPointType.Incident
  247. }, l, i.IncidentID);
  248.  
  249. db.SubmitChanges();
  250.  
  251. return new RuleReturn
  252. {
  253. Refresh = false,
  254. Success = true,
  255. 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');"
  256. };
  257. }
  258.  
  259. new JournalDS().Create(new JournalVO
  260. {
  261. Text = string.Format("Fant ikke bruker"),
  262. Public = false,
  263. Type = new JournalTypeDS().Single("Notes"),
  264. ParentType = SupportPointType.Incident
  265. }, l, i.IncidentID);
  266.  
  267. return new RuleReturn
  268. {
  269. Refresh = false,
  270. Success = true,
  271. Script = "Ext.Msg.alert('Info', 'Fant ikke bruker');"
  272. };
  273. }
  274. catch (Exception e)
  275. {
  276. new JournalDS().Create(new JournalVO
  277. {
  278. Text = string.Format("Ukjent feil: " + e.StackTrace + "<br/>" + e.Message + "<br/>Inner exception:" + e.InnerException.Message),
  279. Public = false,
  280. Type = new JournalTypeDS().Single("Notes"),
  281. ParentType = SupportPointType.Incident
  282. }, l, i.IncidentID);
  283.  
  284. return new RuleReturn
  285. {
  286. Script = "Ext.Msg.alert('Info', '" + e.StackTrace + "<br/>" + e.Message + "<br/>');",
  287. Refresh = false,
  288. Success = true
  289. };
  290. }
  291. }
  292.  
  293. private static string ConvertPath(string path)
  294. {
  295. path = path.ToUpper().Replace("LDAP://", "");
  296.  
  297. if (path.Contains('/'))
  298. path = path.Split(new[] { '/' })[0];
  299. return path;
  300. }
  301.  
  302. #endregion
  303. }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement