// Copyright (c) 2012, 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.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using Microsoft.Win32;
namespace WebsitePanel.Providers.HostedSolution
{
public class LyncBase : HostingServiceProviderBase, ILyncServer
{
#region Fields
private static InitialSessionState session;
#endregion
#region Properties
internal static string LyncRegistryPath { get; set; }
internal static string LyncVersion { get; set; }
internal string PoolFQDN
{
get { return ProviderSettings[LyncConstants.PoolFQDN]; }
}
internal string SimpleUrlRoot
{
get { return ProviderSettings[LyncConstants.SimpleUrlRoot]; }
}
internal string PrimaryDomainController
{
get { return ProviderSettings["PrimaryDomainController"]; }
}
internal string RootOU
{
get { return ProviderSettings["RootOU"]; }
}
internal string RootDomain
{
get { return ServerSettings.ADRootDomain; }
}
#endregion
#region Methods
public string CreateOrganization(string organizationId, string sipDomain, bool enableConferencing, bool enableConferencingVideo, int maxConferenceSize, bool enabledFederation, bool enabledEnterpriseVoice)
{
return CreateOrganizationInternal(organizationId, sipDomain, enableConferencingVideo, maxConferenceSize, enabledFederation, enabledEnterpriseVoice);
}
public virtual string GetOrganizationTenantId(string organizationId)
{
return "NoHostingPack";
}
public virtual bool DeleteOrganization(string organizationId, string sipDomain)
{
return DeleteOrganizationInternal(organizationId, sipDomain);
}
public virtual bool CreateUser(string organizationId, string userUpn, LyncUserPlan plan)
{
return CreateUserInternal(organizationId, userUpn, plan);
}
public virtual LyncUser GetLyncUserGeneralSettings(string organizationId, string userUpn)
{
return GetLyncUserGeneralSettingsInternal(organizationId, userUpn);
}
public virtual bool SetLyncUserGeneralSettings(string organizationId, string userUpn, LyncUser lyncUser)
{
return SetLyncUserGeneralSettingsInternal(organizationId, userUpn, lyncUser);
}
public virtual bool SetLyncUserPlan(string organizationId, string userUpn, LyncUserPlan plan)
{
return SetLyncUserPlanInternal(organizationId, userUpn, plan, null);
}
public virtual bool DeleteUser(string userUpn)
{
return DeleteUserInternal(userUpn);
}
public virtual LyncFederationDomain[] GetFederationDomains(string organizationId)
{
return GetFederationDomainsInternal(organizationId);
}
public virtual bool AddFederationDomain(string organizationId, string domainName, string proxyFqdn)
{
return AddFederationDomainInternal(organizationId, domainName, proxyFqdn);
}
public virtual bool RemoveFederationDomain(string organizationId, string domainName)
{
return RemoveFederationDomainInternal(organizationId, domainName);
}
public virtual void ReloadConfiguration()
{
ReloadConfigurationInternal();
}
public virtual string[] GetPolicyList(LyncPolicyType type, string name)
{
return GetPolicyListInternal(type, name);
}
public override bool IsInstalled()
{
bool bResult = false;
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(LyncRegistryPath);
if (registryKey != null)
{
var value = (string) registryKey.GetValue("ProductVersion", null);
if (value.StartsWith(LyncVersion))
{
bResult = true;
}
registryKey.Close();
}
return bResult;
}
internal virtual string CreateOrganizationInternal(string organizationId, string sipDomain, bool enableConferencingVideo, int maxConferenceSize, bool enabledFederation, bool enabledEnterpriseVoice)
{
throw new NotImplementedException();
}
internal virtual bool DeleteOrganizationInternal(string organizationId, string sipDomain)
{
throw new NotImplementedException();
}
internal virtual bool CreateUserInternal(string organizationId, string userUpn, LyncUserPlan plan)
{
throw new NotImplementedException();
}
internal virtual LyncUser GetLyncUserGeneralSettingsInternal(string organizationId, string userUpn)
{
throw new NotImplementedException();
}
internal virtual bool SetLyncUserGeneralSettingsInternal(string organizationId, string userUpn, LyncUser lyncUser)
{
throw new NotImplementedException();
}
internal virtual bool SetLyncUserPlanInternal(string organizationId, string userUpn, LyncUserPlan plan, Runspace runspace)
{
throw new NotImplementedException();
}
internal virtual bool DeleteUserInternal(string userUpn)
{
throw new NotImplementedException();
}
internal virtual LyncFederationDomain[] GetFederationDomainsInternal(string organizationId)
{
throw new NotImplementedException();
}
internal virtual bool AddFederationDomainInternal(string organizationId, string domainName, string proxyFqdn)
{
throw new NotImplementedException();
}
internal virtual bool RemoveFederationDomainInternal(string organizationId, string domainName)
{
throw new NotImplementedException();
}
internal virtual void ReloadConfigurationInternal()
{
throw new NotImplementedException();
}
internal virtual string[] GetPolicyListInternal(LyncPolicyType type, string name)
{
throw new NotImplementedException();
}
#region PowerShell integration
/// Opens runspace.
/// The runspace.
internal Runspace OpenRunspace()
{
HostedSolutionLog.LogStart("OpenRunspace");
if (session == null)
{
session = InitialSessionState.CreateDefault();
session.ImportPSModule(new[] {"ActiveDirectory", "Lync"});
}
Runspace runspace = RunspaceFactory.CreateRunspace(session);
runspace.Open();
runspace.SessionStateProxy.SetVariable("ConfirmPreference", "none");
HostedSolutionLog.LogEnd("OpenRunspace");
return runspace;
}
/// Closes runspace.
/// The runspace.
internal void CloseRunspace(Runspace runspace)
{
try
{
if (runspace != null && runspace.RunspaceStateInfo.State == RunspaceState.Opened)
{
runspace.Close();
}
}
catch (Exception ex)
{
HostedSolutionLog.LogError("Runspace error", ex);
}
}
/// Executes shell command.
/// The runspace.
/// Scripts list.
/// The result.
internal Collection ExecuteShellCommand(Runspace runspace, List scripts)
{
object[] errors;
return ExecuteShellCommand(runspace, scripts, out errors);
}
/// Executes shell command.
/// The runspace.
/// The command.
/// True - if domain controller should be used.
/// The result.
internal Collection ExecuteShellCommand(Runspace runspace, Command command, bool useDomainController)
{
object[] errors;
return ExecuteShellCommand(runspace, command, useDomainController, out errors);
}
/// Executes shell command.
/// The runspace.
/// The command.
/// Errors list.
/// The result.
internal Collection ExecuteShellCommand(Runspace runspace, Command command, out object[] errors)
{
return ExecuteShellCommand(runspace, command, true, out errors);
}
/// Executes shell command.
/// The runspace.
/// The command.
/// True - if domain controller should be used.
/// Errors list.
/// The result.
internal Collection ExecuteShellCommand(Runspace runspace, Command command, bool useDomainController, out object[] errors)
{
HostedSolutionLog.LogStart("ExecuteShellCommand");
var errorList = new List