Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Drawing;
- using System.Xml;
- using System.Xml.Serialization;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.DirectoryServices.AccountManagement;
- using System.DirectoryServices.ActiveDirectory;
- using System.Collections;
- using System.Reflection;
- using System.ComponentModel;
- namespace nadut
- {
- public enum NadutAccessType
- {
- User,
- Admin
- }
- public enum NadutFormSize
- {
- MainFormLoad,
- MainFormRegular,
- CSV,
- LoginFormNormal,
- LoginFormExchange,
- SettingsFormNormal
- }
- }
- namespace nadut.Configuration
- {
- public class Config
- {
- private string _path;
- private NadutRequiredSecurityGroups _requiredSecurityGroups { get; set; }
- private List<NadutSite> _sites = new List<NadutSite>();
- public NadutRequiredSecurityGroups RequiredSecurityGroups
- {
- get { return _requiredSecurityGroups; }
- set { _requiredSecurityGroups = value; }
- }
- public List<NadutSite> Sites
- {
- get { return _sites; }
- }
- public string Path
- {
- get { return _path; }
- set { _path = value; }
- }
- public void AddSite(NadutSite s)
- {
- Sites.Add(s);
- }
- public Config()
- {
- _path = null;
- List<NadutSite> _sites = new List<NadutSite>();
- _requiredSecurityGroups = new NadutRequiredSecurityGroups();
- }
- public static Object Import(string path)
- {
- StreamReader strReader = null;
- XmlSerializer serializer = null;
- XmlTextReader xmlReader = null;
- Object obj = null;
- try
- {
- strReader = new StreamReader(path);
- serializer = new XmlSerializer(typeof(Config));
- xmlReader = new XmlTextReader(strReader);
- obj = serializer.Deserialize(xmlReader);
- }
- catch (Exception exp)
- {
- Console.WriteLine("Unable to import config" + exp.Message);
- }
- finally
- {
- if (xmlReader != null)
- {
- xmlReader.Close();
- }
- if (strReader != null)
- {
- strReader.Close();
- }
- }
- return obj;
- }
- public static void Save(string path, Config c)
- {
- XmlSerializer writer = new XmlSerializer(typeof(Config));
- XmlWriter file = XmlWriter.Create(path);
- writer.Serialize(file, c);
- if (file != null)
- {
- file.Close();
- }
- }
- }
- public class UserConfig
- {
- // private properties
- private string _domain;
- private string _username;
- private string _password;
- private string _accessType;
- private bool _isConnectedToExchange;
- private string[] _exchangeServers;
- // public get/set vars
- public string Domain
- {
- get { return _domain; }
- set { _domain = value; }
- }
- public string Username
- {
- get { return _username; }
- set { _username = value; }
- }
- public string Password
- {
- get { return _password; }
- set { _password = value; }
- }
- public string AccessType
- {
- get { return _accessType; }
- set { _accessType = value; }
- }
- public bool IsConnectedToExchange
- {
- get { return _isConnectedToExchange; }
- set { _isConnectedToExchange = value; }
- }
- public string[] ExchangeServers
- {
- get { return _exchangeServers; }
- set { _exchangeServers = value; }
- }
- public UserConfig()
- {
- _domain = null;
- _username = null;
- _password = null;
- _accessType = null;
- _exchangeServers = null;
- }
- }
- public class NadutRequiredSecurityGroups
- {
- public string AppAdmins { get; set; }
- public string AppUsers { get; set; }
- public NadutRequiredSecurityGroups()
- {
- AppAdmins = "Domain Admins";
- AppUsers = "Domain Users";
- }
- public NadutRequiredSecurityGroups(string admins, string users)
- {
- AppAdmins = admins;
- AppUsers = users;
- }
- }
- public static class UserAccessType
- {
- public static string Get(Array usersGroups, string appAdminsGroup, string appUsersGroup)
- {
- string currentRights = null;
- bool skipUserCheck = false;
- foreach (string group in usersGroups)
- {
- if (group == appAdminsGroup)
- {
- currentRights = "Administrator";
- skipUserCheck = true;
- break;
- }
- if (skipUserCheck == false)
- {
- if (group == appUsersGroup)
- {
- currentRights = "User";
- break;
- }
- }
- }
- return currentRights;
- }
- }
- public class NadutSite
- {
- public string Company { get; set; }
- public string Office { get; set; }
- public string Address { get; set; }
- public string State { get; set; }
- public string City { get; set; }
- public string PostalCode { get; set; }
- public NadutSite()
- {
- Company = null;
- Office = null;
- Address = null;
- State = null;
- City = null;
- PostalCode = null;
- }
- public NadutSite(string company, string office, string address, string state, string city, string postalcode)
- {
- Company = company;
- Office = office;
- Address = address;
- State = state;
- City = city;
- PostalCode = postalcode;
- }
- }
- }
- namespace nadut.Forms
- {
- public static class Mover
- {
- public const int WM_NCLBUTTONDOWN = 0xA1;
- public const int HT_CAPTION = 0x2;
- [DllImport("user32.dll")]
- public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
- [DllImport("user32.dll")]
- public static extern bool ReleaseCapture();
- public static void OnMouseDown_MoveForm(MouseEventArgs e, Form f)
- {
- if (e.Button == MouseButtons.Left)
- {
- f.Opacity = 0.5;
- ReleaseCapture();
- SendMessage(f.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
- f.Opacity = 1;
- }
- }
- }
- public static class CenterForm
- {
- public static void Form(Form f)
- {
- Rectangle screen = Screen.PrimaryScreen.WorkingArea;
- int width = (screen.Width / 2 - f.Width / 2);
- int height = (screen.Height / 2 - f.Height / 2);
- Point location = new Point(width, height);
- f.Location = location;
- }
- }
- public static class Resize
- {
- public static void SetSize(Form f, NadutFormSize s)
- {
- switch (s)
- {
- case NadutFormSize.MainFormLoad:
- f.Size = new Size(10, 10);
- break;
- case NadutFormSize.MainFormRegular:
- f.Size = new Size(960, 630);
- break;
- case NadutFormSize.CSV:
- f.Size = new Size(1484, 635);
- break;
- case NadutFormSize.LoginFormExchange:
- f.Size = new Size(567, 274);
- break;
- case NadutFormSize.LoginFormNormal:
- f.Size = new Size(325, 274);
- break;
- case NadutFormSize.SettingsFormNormal:
- f.Size = new Size(905, 496);
- break;
- }
- CenterForm.Form(f);
- }
- }
- public static class ComboBoxFactory
- {
- public static void FillFromArray(ComboBox c, String[] a)
- {
- foreach (string i in a)
- {
- c.Items.Add(i);
- }
- }
- public static void SetAutoCompleteUsingOwnContents(ComboBox c)
- {
- if (c.Items.Count > 0)
- {
- c.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
- c.AutoCompleteSource = AutoCompleteSource.CustomSource;
- foreach (string i in c.Items)
- {
- c.AutoCompleteCustomSource.Add(i);
- }
- }
- }
- }
- public static class Controls
- {
- public static string GetControlText(Control c)
- {
- string r = c.Text.ToString();
- return r;
- }
- public static void _Disable(params Control[] controls)
- {
- foreach(Control c in controls)
- {
- c.Enabled = false;
- }
- }
- public static void _Enable(params Control[] controls)
- {
- foreach (Control c in controls)
- {
- c.Enabled = true;
- }
- }
- public static void RemoveClickEvent(Control[] controlOrControls)
- {
- FieldInfo f1 = typeof(Control).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
- foreach (Control c in controlOrControls)
- {
- object obj = f1.GetValue(c);
- PropertyInfo pi = c.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
- EventHandlerList list = (EventHandlerList)pi.GetValue(c, null);
- list.RemoveHandler(obj, list[obj]);
- }
- }
- // returns TRUE if text IS found
- // returns FALSE if NO TEXT is found
- public static bool CheckText(Control[] controls)
- {
- bool r = true;
- foreach(Control c in controls)
- {
- string t = c.Text.ToString();
- if (String.IsNullOrEmpty(t))
- {
- r = false;
- }
- }
- return r;
- }
- }
- }
- namespace nadut.ActiveDirectory
- {
- public class Authentication
- {
- public static bool ValidateCredentials(string domain, string username, string password)
- {
- bool result = false;
- try
- {
- PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain);
- result = pc.ValidateCredentials(username, password);
- if (result != true)
- {
- result = false;
- }
- }
- catch
- {
- result = false;
- }
- return result;
- }
- }
- public static class Get
- {
- public static Domain DomainInfo(string domain, string username, string password)
- {
- DirectoryContext dirContext = new DirectoryContext(DirectoryContextType.Domain, domain, username, password);
- return Domain.GetDomain(dirContext);
- }
- public static ArrayList GroupMembership(string domain, string username, string password)
- {
- PrincipalContext prinCon = new PrincipalContext(ContextType.Domain, domain, username, password);
- UserPrincipal userPrin = UserPrincipal.FindByIdentity(prinCon, username);
- PrincipalSearchResult<Principal> gpp = userPrin.GetGroups();
- ArrayList r = new ArrayList();
- foreach(Principal g in gpp)
- {
- string gg = g.Name.ToString();
- r.Add(gg);
- }
- return r;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement