websitepanel/WebsitePanel/Sources/Tools/WebsitePanel.Import.Enterprise/ApplicationForm.cs
2011-07-13 16:07:32 -07:00

333 lines
No EOL
9.3 KiB
C#

// Copyright (c) 2011, Outercurve Foundation.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// - Neither the name of the Outercurve Foundation nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.DirectoryServices;
using WebsitePanel.EnterpriseServer;
using WebsitePanel.Providers.HostedSolution;
namespace WebsitePanel.Import.Enterprise
{
public partial class ApplicationForm : BaseForm
{
private string username;
private string password;
private OrganizationImporter importer;
internal bool ImportStarted = false;
public ApplicationForm()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
public void InitializeForm(string username, string password)
{
this.username = username;
this.password = password;
UserInfo info = UserController.GetUser(username);
SecurityContext.SetThreadPrincipal(info);
importer = new OrganizationImporter();
Assembly assembly = Assembly.GetExecutingAssembly();
this.Text += " v" + assembly.GetName().Version.ToString(3);
UpdateForm();
}
private void OnBrowseSpace(object sender, EventArgs e)
{
SelectSpace();
}
private void SelectSpace()
{
SpaceForm form = new SpaceForm();
form.InitializeForm(username, password);
if (form.ShowDialog(this) == DialogResult.OK)
{
Global.Space = form.SelectedSpace;
txtSpace.Text = Global.Space.PackageName;
LoadSpaceData(Global.Space);
}
}
private void LoadSpaceData(PackageInfo packageInfo)
{
int serviceId = PackageController.GetPackageServiceId(packageInfo.PackageId, ResourceGroups.HostedOrganizations);
ServiceInfo serviceInfo = ServerController.GetServiceInfo(serviceId);
StringDictionary serviceSettings = ServerController.GetServiceSettingsAdmin(serviceId);
Global.RootOU = serviceSettings["RootOU"];
Global.PrimaryDomainController = serviceSettings["PrimaryDomainController"];
Global.TempDomain = serviceSettings["TempDomain"];
ServerInfo serverInfo = ServerController.GetServerById(serviceInfo.ServerId);
Global.ADRootDomain = serverInfo.ADRootDomain;
}
private void OnBrowseOU(object sender, EventArgs e)
{
SelectOU();
}
private void SelectOU()
{
if (string.IsNullOrEmpty(txtSpace.Text))
{
ShowWarning("Please select hosting space first.");
return;
}
OUForm form = new OUForm();
form.InitializeForm();
if (form.ShowDialog(this) == DialogResult.OK)
{
Global.OrgDirectoryEntry = form.DirectoryEntry;
string orgId = (string)Global.OrgDirectoryEntry.Properties["name"].Value;
txtOU.Text = form.DirectoryEntry.Path;
Global.OrganizationId = orgId;
LoadOrganizationData(Global.OrgDirectoryEntry);
}
}
private void LoadOrganizationData(DirectoryEntry parent)
{
string orgId = (string)parent.Properties["name"].Value;
txtOrgId.Text = orgId;
Organization org = OrganizationController.GetOrganizationById(orgId);
if (org != null)
{
rbCreateAndImport.Checked = false;
rbImport.Checked = true;
txtOrgName.Text = org.Name;
}
else
{
rbCreateAndImport.Checked = true;
rbImport.Checked = false;
txtOrgName.Text = orgId;
}
LoadOrganizationAccounts(parent);
}
private void LoadOrganizationAccounts(DirectoryEntry ou)
{
lvUsers.Items.Clear();
ListViewItem item = null;
string email;
string type;
string name;
PropertyValueCollection typeProp;
string ouName = (string)ou.Properties["name"].Value;
foreach (DirectoryEntry child in ou.Children)
{
type = null;
email = null;
name = (string)child.Properties["name"].Value;
//account type
typeProp = child.Properties["msExchRecipientDisplayType"];
switch (child.SchemaClassName)
{
case "user":
email = (string)child.Properties["userPrincipalName"].Value;
if (typeProp == null || typeProp.Value == null)
{
type = "User";
}
else
{
int mailboxType = (int)typeProp.Value;
switch (mailboxType)
{
case 1073741824:
type = "User Mailbox";
break;
case 7:
type = "Room Mailbox";
break;
case 8:
type = "Equipment Mailbox";
break;
}
}
if (!string.IsNullOrEmpty(type))
{
item = new ListViewItem(name);
item.ImageIndex = 0;
item.Checked = true;
item.Tag = child;
item.SubItems.Add(email);
item.SubItems.Add(type);
lvUsers.Items.Add(item);
}
break;
case "contact":
if (typeProp != null && typeProp.Value != null && 6 == (int)typeProp.Value)
{
type = "Mail Contact";
if (child.Properties["targetAddress"] != null)
{
email = (string)child.Properties["targetAddress"].Value;
if (email != null && email.ToLower().StartsWith("smtp:"))
email = email.Substring(5);
}
}
if (!string.IsNullOrEmpty(type))
{
item = new ListViewItem(name);
item.ImageIndex = 1;
item.Checked = true;
item.Tag = child;
item.SubItems.Add(email);
item.SubItems.Add(type);
lvUsers.Items.Add(item);
}
break;
case "group":
if (child.Properties["mail"] != null)
email = (string)child.Properties["mail"].Value;
if ((typeProp != null) && (typeProp.Value != null) && (1073741833 == (int)typeProp.Value))
{
//Universal Security Group
type = "Mail-Enabled Universal Security Group";
//email
PropertyValueCollection proxyAddresses = child.Properties["proxyAddresses"];
if (proxyAddresses != null)
{
foreach (string address in proxyAddresses)
{
if (address != null && address.StartsWith("SMTP:"))
{
email = address.Substring(5);
break;
}
}
}
}
if (!string.IsNullOrEmpty(type) && name != ouName)
{
item = new ListViewItem(name);
item.ImageIndex = 2;
item.Checked = true;
item.Tag = child;
item.SubItems.Add(email);
item.SubItems.Add(type);
lvUsers.Items.Add(item);
}
break;
}
}
}
private void OnDataChanged(object sender, EventArgs e)
{
UpdateForm();
}
private void UpdateForm()
{
if (string.IsNullOrEmpty(txtSpace.Text) ||
string.IsNullOrEmpty(txtOU.Text))
{
btnStart.Enabled = false;
}
else
{
btnStart.Enabled = true;
}
}
private void OnImportClick(object sender, EventArgs e)
{
List<DirectoryEntry> accounts = new List<DirectoryEntry>();
foreach (ListViewItem item in lvUsers.Items)
{
if (item.Checked)
{
accounts.Add((DirectoryEntry)item.Tag);
}
}
Global.SelectedAccounts = accounts;
Global.OrganizationName = txtOrgName.Text;
Global.ImportAccountsOnly = rbImport.Checked;
Global.HasErrors = false;
importer.Initialize(this.username, this);
importer.Start();
}
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = ImportStarted;
}
private void OnCheckedChanged(object sender, EventArgs e)
{
txtOrgName.ReadOnly = !rbCreateAndImport.Checked;
}
private void OnSelectAllClick(object sender, EventArgs e)
{
foreach (ListViewItem item in lvUsers.Items)
{
item.Checked = true;
}
}
private void OnDeselectAllClick(object sender, EventArgs e)
{
foreach (ListViewItem item in lvUsers.Items)
{
item.Checked = false;
}
}
}
}