Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Drawing;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9. using System.DirectoryServices.AccountManagement;
  10. using System.DirectoryServices.ActiveDirectory;
  11. using System.Collections;
  12. using System.Reflection;
  13. using System.ComponentModel;
  14.  
  15. namespace nadut
  16. {
  17.     public enum NadutAccessType
  18.     {
  19.         User,
  20.         Admin
  21.     }
  22.  
  23.     public enum NadutFormSize
  24.     {
  25.         MainFormLoad,
  26.         MainFormRegular,
  27.         CSV,
  28.         LoginFormNormal,
  29.         LoginFormExchange,
  30.         SettingsFormNormal
  31.     }    
  32. }
  33.  
  34. namespace nadut.Configuration
  35. {
  36.     public class Config
  37.     {
  38.         private string _path;
  39.         private NadutRequiredSecurityGroups _requiredSecurityGroups { get; set; }
  40.         private List<NadutSite> _sites = new List<NadutSite>();
  41.  
  42.         public NadutRequiredSecurityGroups RequiredSecurityGroups
  43.         {
  44.             get { return _requiredSecurityGroups; }
  45.             set { _requiredSecurityGroups = value; }
  46.         }
  47.  
  48.         public List<NadutSite> Sites
  49.         {
  50.             get { return _sites; }
  51.         }
  52.  
  53.         public string Path
  54.         {
  55.             get { return _path; }
  56.             set { _path = value; }
  57.         }
  58.  
  59.         public void AddSite(NadutSite s)
  60.         {
  61.             Sites.Add(s);
  62.         }
  63.  
  64.         public Config()
  65.         {
  66.             _path = null;
  67.             List<NadutSite> _sites = new List<NadutSite>();
  68.             _requiredSecurityGroups = new NadutRequiredSecurityGroups();
  69.         }
  70.  
  71.         public static Object Import(string path)
  72.         {
  73.             StreamReader strReader = null;
  74.             XmlSerializer serializer = null;
  75.             XmlTextReader xmlReader = null;
  76.             Object obj = null;
  77.             try
  78.             {
  79.                 strReader = new StreamReader(path);
  80.                 serializer = new XmlSerializer(typeof(Config));
  81.                 xmlReader = new XmlTextReader(strReader);
  82.                 obj = serializer.Deserialize(xmlReader);
  83.             }
  84.             catch (Exception exp)
  85.             {
  86.                 Console.WriteLine("Unable to import config" + exp.Message);
  87.             }
  88.             finally
  89.             {
  90.                 if (xmlReader != null)
  91.                 {
  92.                     xmlReader.Close();
  93.                 }
  94.                 if (strReader != null)
  95.                 {
  96.                     strReader.Close();
  97.                 }
  98.             }
  99.             return obj;
  100.         }
  101.  
  102.         public static void Save(string path, Config c)
  103.         {
  104.             XmlSerializer writer = new XmlSerializer(typeof(Config));
  105.             XmlWriter file = XmlWriter.Create(path);
  106.             writer.Serialize(file, c);
  107.             if (file != null)
  108.             {
  109.                 file.Close();
  110.             }
  111.         }
  112.     }
  113.  
  114.     public class UserConfig
  115.     {
  116.         // private properties
  117.         private string _domain;
  118.         private string _username;
  119.         private string _password;
  120.         private string _accessType;
  121.         private bool _isConnectedToExchange;
  122.         private string[] _exchangeServers;
  123.  
  124.         // public get/set vars
  125.         public string Domain
  126.         {
  127.             get { return _domain; }
  128.             set { _domain = value; }
  129.         }
  130.  
  131.         public string Username
  132.         {
  133.             get { return _username; }
  134.             set { _username = value; }
  135.         }
  136.  
  137.         public string Password
  138.         {
  139.             get { return _password; }
  140.             set { _password = value; }
  141.         }
  142.  
  143.         public string AccessType
  144.         {
  145.             get { return _accessType; }
  146.             set { _accessType = value; }
  147.         }
  148.  
  149.         public bool IsConnectedToExchange
  150.         {
  151.             get { return _isConnectedToExchange; }
  152.             set { _isConnectedToExchange = value; }
  153.         }
  154.  
  155.         public string[] ExchangeServers
  156.         {
  157.             get { return _exchangeServers; }
  158.             set { _exchangeServers = value; }
  159.         }
  160.  
  161.         public UserConfig()
  162.         {
  163.             _domain = null;
  164.             _username = null;
  165.             _password = null;
  166.             _accessType = null;
  167.             _exchangeServers = null;
  168.         }
  169.     }
  170.  
  171.     public class NadutRequiredSecurityGroups
  172.     {
  173.         public string AppAdmins { get; set; }
  174.         public string AppUsers { get; set; }
  175.  
  176.         public NadutRequiredSecurityGroups()
  177.         {
  178.             AppAdmins = "Domain Admins";
  179.             AppUsers = "Domain Users";
  180.         }
  181.  
  182.         public NadutRequiredSecurityGroups(string admins, string users)
  183.         {
  184.             AppAdmins = admins;
  185.             AppUsers = users;
  186.         }
  187.     }
  188.  
  189.     public static class UserAccessType
  190.     {
  191.         public static string Get(Array usersGroups, string appAdminsGroup, string appUsersGroup)
  192.         {
  193.             string currentRights = null;
  194.             bool skipUserCheck = false;
  195.             foreach (string group in usersGroups)
  196.             {
  197.                 if (group == appAdminsGroup)
  198.                 {
  199.                     currentRights = "Administrator";
  200.                     skipUserCheck = true;
  201.                     break;
  202.                 }
  203.                 if (skipUserCheck == false)
  204.                 {
  205.                     if (group == appUsersGroup)
  206.                     {
  207.                         currentRights = "User";
  208.                         break;
  209.                     }
  210.                 }
  211.             }
  212.             return currentRights;
  213.         }
  214.     }
  215.  
  216.     public class NadutSite
  217.     {
  218.         public string Company { get; set; }
  219.         public string Office { get; set; }
  220.         public string Address { get; set; }
  221.         public string State { get; set; }
  222.         public string City { get; set; }
  223.         public string PostalCode { get; set; }
  224.  
  225.         public NadutSite()
  226.         {
  227.             Company = null;
  228.             Office = null;
  229.             Address = null;
  230.             State = null;
  231.             City = null;
  232.             PostalCode = null;
  233.         }
  234.  
  235.         public NadutSite(string company, string office, string address, string state, string city, string postalcode)
  236.         {
  237.             Company = company;
  238.             Office = office;
  239.             Address = address;
  240.             State = state;
  241.             City = city;
  242.             PostalCode = postalcode;
  243.         }
  244.     }
  245. }
  246.  
  247. namespace nadut.Forms
  248. {
  249.     public static class Mover
  250.     {
  251.         public const int WM_NCLBUTTONDOWN = 0xA1;
  252.         public const int HT_CAPTION = 0x2;
  253.         [DllImport("user32.dll")]
  254.         public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  255.         [DllImport("user32.dll")]
  256.         public static extern bool ReleaseCapture();
  257.  
  258.         public static void OnMouseDown_MoveForm(MouseEventArgs e, Form f)
  259.         {
  260.             if (e.Button == MouseButtons.Left)
  261.             {
  262.                 f.Opacity = 0.5;
  263.                 ReleaseCapture();
  264.                 SendMessage(f.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
  265.                 f.Opacity = 1;
  266.             }
  267.         }
  268.     }
  269.  
  270.     public static class CenterForm
  271.     {
  272.         public static void Form(Form f)
  273.         {
  274.             Rectangle screen = Screen.PrimaryScreen.WorkingArea;
  275.             int width = (screen.Width / 2 - f.Width / 2);
  276.             int height = (screen.Height / 2 - f.Height / 2);
  277.             Point location = new Point(width, height);
  278.             f.Location = location;
  279.         }
  280.     }
  281.  
  282.     public static class Resize
  283.     {
  284.         public static void SetSize(Form f, NadutFormSize s)
  285.         {
  286.             switch (s)
  287.             {
  288.                 case NadutFormSize.MainFormLoad:
  289.                     f.Size = new Size(10, 10);
  290.                     break;
  291.  
  292.                 case NadutFormSize.MainFormRegular:
  293.                     f.Size = new Size(960, 630);
  294.                     break;
  295.  
  296.                 case NadutFormSize.CSV:
  297.                     f.Size = new Size(1484, 635);
  298.                     break;
  299.  
  300.                 case NadutFormSize.LoginFormExchange:
  301.                     f.Size = new Size(567, 274);
  302.                     break;
  303.  
  304.                 case NadutFormSize.LoginFormNormal:
  305.                     f.Size = new Size(325, 274);
  306.                     break;
  307.  
  308.                 case NadutFormSize.SettingsFormNormal:
  309.                     f.Size = new Size(905, 496);
  310.                     break;
  311.             }
  312.             CenterForm.Form(f);
  313.         }
  314.     }
  315.  
  316.     public static class ComboBoxFactory
  317.     {
  318.         public static void FillFromArray(ComboBox c, String[] a)
  319.         {
  320.             foreach (string i in a)
  321.             {
  322.                 c.Items.Add(i);
  323.             }
  324.         }
  325.  
  326.         public static void SetAutoCompleteUsingOwnContents(ComboBox c)
  327.         {
  328.             if (c.Items.Count > 0)
  329.             {
  330.                 c.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
  331.                 c.AutoCompleteSource = AutoCompleteSource.CustomSource;
  332.  
  333.                 foreach (string i in c.Items)
  334.                 {
  335.                     c.AutoCompleteCustomSource.Add(i);
  336.                 }
  337.             }
  338.         }
  339.     }
  340.  
  341.     public static class Controls
  342.     {
  343.         public static string GetControlText(Control c)
  344.         {
  345.             string r = c.Text.ToString();
  346.             return r;
  347.         }
  348.  
  349.         public static void _Disable(params Control[] controls)
  350.         {
  351.             foreach(Control c in controls)
  352.             {
  353.                 c.Enabled = false;
  354.             }
  355.         }
  356.  
  357.         public static void _Enable(params Control[] controls)
  358.         {
  359.             foreach (Control c in controls)
  360.             {
  361.                 c.Enabled = true;
  362.             }
  363.         }
  364.  
  365.         public static void RemoveClickEvent(Control[] controlOrControls)
  366.         {
  367.             FieldInfo f1 = typeof(Control).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
  368.             foreach (Control c in controlOrControls)
  369.             {                
  370.                 object obj = f1.GetValue(c);
  371.                 PropertyInfo pi = c.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
  372.                 EventHandlerList list = (EventHandlerList)pi.GetValue(c, null);
  373.                 list.RemoveHandler(obj, list[obj]);
  374.             }
  375.         }
  376.  
  377.         // returns TRUE if text IS found
  378.         // returns FALSE if NO TEXT is found
  379.         public static bool CheckText(Control[] controls)
  380.         {
  381.             bool r = true;
  382.             foreach(Control c in controls)
  383.             {
  384.                 string t = c.Text.ToString();
  385.                 if (String.IsNullOrEmpty(t))
  386.                 {
  387.                     r = false;
  388.                 }
  389.             }
  390.             return r;
  391.         }
  392.     }
  393. }
  394.  
  395. namespace nadut.ActiveDirectory
  396. {
  397.     public class Authentication
  398.     {
  399.         public static bool ValidateCredentials(string domain, string username, string password)
  400.         {            
  401.             bool result = false;
  402.             try
  403.             {
  404.                 PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain);
  405.                 result = pc.ValidateCredentials(username, password);
  406.                 if (result != true)
  407.                 {
  408.                     result = false;
  409.                 }
  410.             }
  411.             catch
  412.             {
  413.                 result = false;
  414.             }
  415.             return result;
  416.         }
  417.     }
  418.  
  419.     public static class Get
  420.     {
  421.         public static Domain DomainInfo(string domain, string username, string password)
  422.         {
  423.             DirectoryContext dirContext = new DirectoryContext(DirectoryContextType.Domain, domain, username, password);
  424.             return Domain.GetDomain(dirContext);
  425.         }
  426.  
  427.         public static ArrayList GroupMembership(string domain, string username, string password)
  428.         {
  429.             PrincipalContext prinCon = new PrincipalContext(ContextType.Domain, domain, username, password);
  430.             UserPrincipal userPrin = UserPrincipal.FindByIdentity(prinCon, username);
  431.             PrincipalSearchResult<Principal> gpp = userPrin.GetGroups();
  432.             ArrayList r = new ArrayList();
  433.             foreach(Principal g in gpp)
  434.             {
  435.                 string gg = g.Name.ToString();
  436.                 r.Add(gg);
  437.             }
  438.             return r;
  439.         }
  440.     }
  441. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement