Esempio n. 1
0
        private static bool CheckQuotas(int packageId, out int errorCode)
        {
            // check account
            errorCode = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
            if (errorCode < 0)
            {
                return(false);
            }

            // check package
            errorCode = SecurityContext.CheckPackage(packageId, DemandPackage.IsActive);
            if (errorCode < 0)
            {
                return(false);
            }

            // check organizations quota
            QuotaValueInfo quota = PackageController.GetPackageQuota(packageId, Quotas.ORGANIZATIONS);

            if (quota.QuotaExhausted)
            {
                errorCode = BusinessErrorCodes.ERROR_ORGS_RESOURCE_QUOTA_LIMIT;
                return(false);
            }


            // check sub-domains quota (for temporary domain)
            quota = PackageController.GetPackageQuota(packageId, Quotas.OS_SUBDOMAINS);
            if (quota.QuotaExhausted)
            {
                errorCode = BusinessErrorCodes.ERROR_SUBDOMAIN_QUOTA_LIMIT;
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// Restores SharePoint site collection.
        /// </summary>
        /// <param name="itemId">Site collection id within metabase.</param>
        /// <param name="uploadedFile"></param>
        /// <param name="packageFile"></param>
        /// <returns></returns>
        public static int RestoreSiteCollection(int itemId, string uploadedFile, string packageFile)
        {
            // Check account.
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // Load original meta item.
            SharePointEnterpriseSiteCollection origItem = (SharePointEnterpriseSiteCollection)PackageController.GetPackageItem(itemId);

            if (origItem == null)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_PACKAGE_ITEM_NOT_FOUND);
            }

            // Check package.
            int packageCheck = SecurityContext.CheckPackage(origItem.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // Log operation.
            TaskManager.StartTask("HOSTED_SHAREPOINT_ENTERPRISE", "BACKUP_SITE_COLLECTION", origItem.Name, itemId);

            try
            {
                // Create site collection on server.
                HostedSharePointServerEnt hostedSharePointServer = GetHostedSharePointServer(origItem.ServiceId);

                string backupFile = null;
                if (!String.IsNullOrEmpty(packageFile))
                {
                    // Copy package files to the remote SharePoint Server.
                    string path   = null;
                    byte[] buffer = null;

                    int offset = 0;
                    do
                    {
                        // Read package file.
                        buffer = FilesController.GetFileBinaryChunk(origItem.PackageId, packageFile, offset, FILE_BUFFER_LENGTH);

                        // Write remote backup file
                        string tempPath = hostedSharePointServer.Enterprise_AppendTempFileBinaryChunk(Path.GetFileName(packageFile), path, buffer);
                        if (path == null)
                        {
                            path       = tempPath;
                            backupFile = path;
                        }

                        offset += FILE_BUFFER_LENGTH;
                    }while (buffer.Length == FILE_BUFFER_LENGTH);
                }
                else if (!String.IsNullOrEmpty(uploadedFile))
                {
                    // Upladed files.
                    backupFile = uploadedFile;
                }

                // Restore.
                if (!String.IsNullOrEmpty(backupFile))
                {
                    hostedSharePointServer.Enterprise_RestoreSiteCollection(origItem, backupFile);
                }

                return(0);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        /// <summary>
        /// Adds SharePoint site collection.
        /// </summary>
        /// <param name="item">Site collection description.</param>
        /// <returns>Created site collection id within metabase.</returns>
        public static int AddSiteCollection(SharePointEnterpriseSiteCollection item)
        {
            // Check account.
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // Check package.
            int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // Check quota.
            OrganizationStatistics orgStats = OrganizationController.GetOrganizationStatistics(item.OrganizationId);

            //QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.HOSTED_SHAREPOINT_SITES);

            if (orgStats.AllocatedSharePointEnterpriseSiteCollections > -1 &&
                orgStats.CreatedSharePointEnterpriseSiteCollections >= orgStats.AllocatedSharePointEnterpriseSiteCollections)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_QUOTA_LIMIT);
            }

            // Check if stats resource is available
            int serviceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.SharepointEnterpriseServer);

            if (serviceId == 0)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_UNAVAILABLE);
            }

            StringDictionary hostedSharePointSettings = ServerController.GetServiceSettings(serviceId);
            QuotaValueInfo   quota             = PackageController.GetPackageQuota(item.PackageId, Quotas.HOSTED_SHAREPOINT_ENTERPRISE_USESHAREDSSL);
            Uri          rootWebApplicationUri = new Uri(hostedSharePointSettings["RootWebApplicationUri"]);
            Organization org      = OrganizationController.GetOrganization(item.OrganizationId);
            string       siteName = item.Name;

            if (quota.QuotaAllocatedValue == 1)
            {
                string sslRoot = hostedSharePointSettings["SharedSSLRoot"];


                string defaultDomain = org.DefaultDomain;
                string hostNameBase  = string.Empty;

                string[] tmp = defaultDomain.Split('.');
                if (tmp.Length == 2)
                {
                    hostNameBase = tmp[0];
                }
                else
                {
                    if (tmp.Length > 2)
                    {
                        hostNameBase = tmp[0] + tmp[1];
                    }
                }

                int counter = 0;
                item.Name = String.Format("{0}://{1}", rootWebApplicationUri.Scheme, hostNameBase + "-" + counter.ToString() + "." + sslRoot);
                siteName  = String.Format("{0}", hostNameBase + "-" + counter.ToString() + "." + sslRoot);

                while (DataProvider.CheckServiceItemExists(serviceId, item.Name, "WebsitePanel.Providers.SharePoint.SharePointEnterpriseSiteCollection,   WebsitePanel.Providers.Base"))
                {
                    counter++;
                    item.Name = String.Format("{0}://{1}", rootWebApplicationUri.Scheme, hostNameBase + "-" + counter.ToString() + "." + sslRoot);
                    siteName  = String.Format("{0}", hostNameBase + "-" + counter.ToString() + "." + sslRoot);
                }
            }
            else
            {
                item.Name = String.Format("{0}://{1}", rootWebApplicationUri.Scheme, item.Name);
            }

            if (rootWebApplicationUri.Port > 0 && rootWebApplicationUri.Port != 80 && rootWebApplicationUri.Port != 443)
            {
                item.PhysicalAddress = String.Format("{0}:{1}", item.Name, rootWebApplicationUri.Port);
            }
            else
            {
                item.PhysicalAddress = item.Name;
            }

            if (Utils.ParseBool(hostedSharePointSettings["LocalHostFile"], false))
            {
                item.RootWebApplicationInteralIpAddress = hostedSharePointSettings["RootWebApplicationInteralIpAddress"];
                item.RootWebApplicationFQDN             = item.Name.Replace(rootWebApplicationUri.Scheme + "://", "");
            }

            item.MaxSiteStorage = RecalculateMaxSize(org.MaxSharePointEnterpriseStorage, (int)item.MaxSiteStorage);
            item.WarningStorage = item.MaxSiteStorage == -1 ? -1 : Math.Min((int)item.WarningStorage, item.MaxSiteStorage);


            // Check package item with given name already exists.
            if (PackageController.GetPackageItemByName(item.PackageId, item.Name, typeof(SharePointEnterpriseSiteCollection)) != null)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_PACKAGE_ITEM_EXISTS);
            }

            // Log operation.
            TaskManager.StartTask("HOSTED_SHAREPOINT_ENTERPRISE", "ADD_SITE_COLLECTION", item.Name);

            try
            {
                // Create site collection on server.
                HostedSharePointServerEnt hostedSharePointServer = GetHostedSharePointServer(serviceId);

                hostedSharePointServer.Enterprise_CreateSiteCollection(item);

                // Make record in metabase.
                item.ServiceId = serviceId;
                int itemId = PackageController.AddPackageItem(item);

                hostedSharePointServer.Enterprise_SetPeoplePickerOu(item.Name, org.DistinguishedName);

                int dnsServiceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.Dns);
                if (dnsServiceId > 0)
                {
                    string[] tmpStr     = siteName.Split('.');
                    string   hostName   = tmpStr[0];
                    string   domainName = siteName.Substring(hostName.Length + 1, siteName.Length - (hostName.Length + 1));

                    List <GlobalDnsRecord> dnsRecords      = ServerController.GetDnsRecordsByService(serviceId);
                    List <DnsRecord>       resourceRecords = DnsServerController.BuildDnsResourceRecords(dnsRecords, hostName, domainName, "");
                    DNSServer dns = new DNSServer();

                    ServiceProviderProxy.Init(dns, dnsServiceId);
                    // add new resource records
                    dns.AddZoneRecords(domainName, resourceRecords.ToArray());
                }

                TaskManager.ItemId = itemId;

                return(itemId);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
Esempio n. 4
0
        private static int AddOrganizationDomain(int itemId, string domainName)
        {
            Log.WriteStart(string.Format("Importing domain {0}...", domainName));

            // load organization
            Organization org = (Organization)PackageController.GetPackageItem(itemId);

            if (org == null)
            {
                return(-1);
            }

            // check package
            int packageCheck = SecurityContext.CheckPackage(org.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            DomainInfo domain = null;

            // check if the domain already exists
            int checkResult = ServerController.CheckDomain(domainName);

            if (checkResult == BusinessErrorCodes.ERROR_DOMAIN_ALREADY_EXISTS)
            {
                // domain exists
                // check if it belongs to the same space
                domain = ServerController.GetDomain(domainName);
                if (domain == null)
                {
                    return(checkResult);
                }

                if (domain.PackageId != org.PackageId)
                {
                    return(checkResult);
                }

                if (DataProvider.ExchangeOrganizationDomainExists(domain.DomainId))
                {
                    return(BusinessErrorCodes.ERROR_ORGANIZATION_DOMAIN_IS_IN_USE);
                }
            }
            else if (checkResult == BusinessErrorCodes.ERROR_RESTRICTED_DOMAIN)
            {
                return(checkResult);
            }

            // create domain if required
            if (domain == null)
            {
                domain                 = new DomainInfo();
                domain.PackageId       = org.PackageId;
                domain.DomainName      = domainName;
                domain.IsPreviewDomain = false;
                domain.IsSubDomain     = false;

                // add domain
                domain.DomainId = ServerController.AddDomain(domain);
            }



            // register domain
            DataProvider.AddExchangeOrganizationDomain(itemId, domain.DomainId, false);

            // register service item
            OrganizationDomain exchDomain = new OrganizationDomain();

            exchDomain.Name      = domainName;
            exchDomain.PackageId = org.PackageId;
            exchDomain.ServiceId = org.ServiceId;
            PackageController.AddPackageItem(exchDomain);
            Log.WriteEnd("Domain imported");
            return(0);
        }
        /// <summary>
        /// Adds SharePoint site collection.
        /// </summary>
        /// <param name="item">Site collection description.</param>
        /// <returns>Created site collection id within metabase.</returns>
        public static int AddSiteCollection(SharePointSiteCollection item)
        {
            string domainName = item.Name;
            // Check account.
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // Check package.
            int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // Check quota.
            OrganizationStatistics orgStats = OrganizationController.GetOrganizationStatistics(item.OrganizationId);

            //QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.HOSTED_SHAREPOINT_SITES);

            if (orgStats.AllocatedSharePointSiteCollections > -1 &&
                orgStats.CreatedSharePointSiteCollections >= orgStats.AllocatedSharePointSiteCollections)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_QUOTA_LIMIT);
            }

            // Check if stats resource is available
            int serviceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.HostedSharePoint);

            if (serviceId == 0)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_UNAVAILABLE);
            }
            StringDictionary hostedSharePointSettings = ServerController.GetServiceSettings(serviceId);
            Uri rootWebApplicationUri = new Uri(hostedSharePointSettings["RootWebApplicationUri"]);

            item.Name = String.Format("{0}://{1}", rootWebApplicationUri.Scheme, item.Name);
            if (rootWebApplicationUri.Port > 0 && rootWebApplicationUri.Port != 80 && rootWebApplicationUri.Port != 443)
            {
                item.PhysicalAddress = String.Format("{0}:{1}", item.Name, rootWebApplicationUri.Port);
            }
            else
            {
                item.PhysicalAddress = item.Name;
            }

            Organization org = OrganizationController.GetOrganization(item.OrganizationId);

            item.MaxSiteStorage = RecalculateMaxSize(org.MaxSharePointStorage, (int)item.MaxSiteStorage);
            item.WarningStorage = item.MaxSiteStorage == -1 ? -1 : Math.Min((int)item.WarningStorage, item.MaxSiteStorage);


            // Check package item with given name already exists.
            if (PackageController.GetPackageItemByName(item.PackageId, item.Name, typeof(SharePointSiteCollection)) != null)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_PACKAGE_ITEM_EXISTS);
            }

            // Log operation.
            TaskManager.StartTask("HOSTEDSHAREPOINT", "ADD_SITE_COLLECTION", item.Name);

            try
            {
                // Create site collection on server.
                HostedSharePointServer hostedSharePointServer = GetHostedSharePointServer(serviceId);

                hostedSharePointServer.CreateSiteCollection(item);
                // Make record in metabase.
                item.ServiceId = serviceId;
                int itemId = PackageController.AddPackageItem(item);

                int dnsServiceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.Dns);
                if (dnsServiceId > 0)
                {
                    DomainInfo domain = ServerController.GetDomain(domainName);
                    if (domain != null)
                    {
                        string website = domain.WebSiteName;

                        if (!String.IsNullOrEmpty(domain.WebSiteName))
                        {
                            DnsRecord[] records = ServerController.GetDnsZoneRecords(domain.DomainId);
                            foreach (DnsRecord record in records)
                            {
                                if (record.RecordType.Equals(DnsRecordType.A) && String.IsNullOrEmpty(record.RecordName))
                                {
                                    ServerController.DeleteDnsZoneRecord(domain.DomainId, String.Empty, DnsRecordType.A, record.RecordData);
                                    break;
                                }
                            }
                        }
                        ServerController.AddDnsZoneRecord(domain.DomainId, String.Empty, DnsRecordType.A, hostedSharePointSettings["RootWebApplicationIpAddress"], 0);
                    }
                }

                TaskManager.ItemId = itemId;
                return(itemId);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }