public static int DeleteSqlDatabase(int itemId)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);

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

            // load original meta item
            SqlDatabase origItem = (SqlDatabase)PackageController.GetPackageItem(itemId);

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

            // place log record
            TaskManager.StartTask("SQL_DATABASE", "DELETE", origItem.Name);
            TaskManager.ItemId = itemId;
            TaskManager.WriteParameter("Provider", origItem.GroupName);

            try
            {
                // get service
                DatabaseServer sql = GetDatabaseServer(origItem.ServiceId);

                // delete service item
                sql.DeleteDatabase(origItem.Name);

                // delete meta item
                PackageController.DeletePackageItem(origItem.Id);

                return(0);
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);
                // Return a generic error instead of re-throwing an exception
                return(BusinessErrorCodes.FAILED_EXECUTE_SERVICE_OPERATION);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
Beispiel #2
0
        public static int AddSite(SharePointSite 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
            QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.SHAREPOINT_SITES);

            if (quota.QuotaExhausted)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_QUOTA_LIMIT);
            }

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

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

            // check package items
            if (PackageController.GetPackageItemByName(item.PackageId, item.Name, typeof(SharePointSite)) != null)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_PACKAGE_ITEM_EXISTS);
            }

            // place log record
            TaskManager.StartTask("SHAREPOINT", "ADD_SITE", item.Name);
            TaskManager.WriteParameter("Database group", item.DatabaseGroupName);
            TaskManager.WriteParameter("Database name", item.DatabaseName);
            TaskManager.WriteParameter("Database user", item.DatabaseUser);

            int databaseItemId     = 0;
            int databaseUserItemId = 0;

            try
            {
                // load web site
                WebSite siteItem = (WebSite)PackageController.GetPackageItemByName(item.PackageId,
                                                                                   item.Name, typeof(WebSite));

                if (siteItem == null)
                {
                    return(BusinessErrorCodes.ERROR_WEB_SITE_SERVICE_UNAVAILABLE);
                }

                // get service web site
                WebServer web = new WebServer();
                ServiceProviderProxy.Init(web, siteItem.ServiceId);
                WebSite site = web.GetSite(siteItem.SiteId);

                /////////////////////////////////////////
                //
                //  PREPARE SHAREPOINT SITE INSTALLATION
                //

                ServiceInfo wssService = ServerController.GetServiceInfo(serviceId);
                bool        wss30      = (wssService.ProviderId == 23);     // WSS 3.0

                // remember original web site bindings
                ServerBinding[] bindings = site.Bindings;

                // set application pool and root folder
                item.ApplicationPool = site.ApplicationPool;
                item.RootFolder      = site.ContentPath;

                // change web site .NET framework if required
                bool siteUpdated = false;
                if (!wss30 && (site.AspNetInstalled != "1" ||
                               site.DedicatedApplicationPool))
                {
                    site.AspNetInstalled          = "1";
                    site.DedicatedApplicationPool = false;
                    web.UpdateSite(site);

                    siteUpdated = true;
                }

                if (wss30 && site.AspNetInstalled != "2")
                {
                    site.AspNetInstalled = "2";
                    web.UpdateSite(site);

                    siteUpdated = true;
                }

                if (siteUpdated)
                {
                    site = web.GetSite(siteItem.SiteId);
                    item.ApplicationPool = site.ApplicationPool;
                }

                if (site.FrontPageInstalled)
                {
                    // remove FrontPage
                    web.UninstallFrontPage(siteItem.SiteId, siteItem.FrontPageAccount);
                }

                // create SQL database
                SqlDatabase database = new SqlDatabase();
                database.PackageId = item.PackageId;
                database.Name      = item.DatabaseName;
                databaseItemId     = DatabaseServerController.AddSqlDatabase(database, item.DatabaseGroupName);
                if (databaseItemId < 0)
                {
                    return(databaseItemId);
                }

                // create SQL user
                SqlUser dbUser = new SqlUser();
                dbUser.PackageId   = item.PackageId;
                dbUser.Name        = item.DatabaseUser;
                dbUser.Password    = item.DatabasePassword;
                databaseUserItemId = DatabaseServerController.AddSqlUser(dbUser, item.DatabaseGroupName);
                if (databaseUserItemId < 0)
                {
                    return(databaseUserItemId);
                }

                // delete SQL database from service
                // and change database user role
                int sqlServiceId = PackageController.GetPackageServiceId(item.PackageId, item.DatabaseGroupName);
                if (sqlServiceId < 0)
                {
                    return(BusinessErrorCodes.ERROR_MSSQL_RESOURCE_UNAVAILABLE);
                }

                // load server settings
                StringDictionary sqlSettings = ServerController.GetServiceSettings(sqlServiceId);
                item.DatabaseServer = sqlSettings["ExternalAddress"];

                DatabaseServer dbServer = new DatabaseServer();
                ServiceProviderProxy.Init(dbServer, sqlServiceId);

                // delete database from service
                dbServer.DeleteDatabase(database.Name);

                // give SQL user "db_creator" role
                dbServer.ExecuteSqlNonQuery("master", String.Format(
                                                "sp_addsrvrolemember '{0}', 'dbcreator'\nGO", dbUser.Name));

                // install SharePoint site
                SharePointServer sps = GetSharePoint(serviceId);
                sps.ExtendVirtualServer(item);

                // remove SQL user from "db_creator" role
                dbServer.ExecuteSqlNonQuery("master", String.Format(
                                                "sp_dropsrvrolemember '{0}', 'dbcreator'\nGO", dbUser.Name));

                // restore original web site bindings
                web.UpdateSiteBindings(site.SiteId, bindings);

                // save statistics item
                item.ServiceId = serviceId;
                int itemId = PackageController.AddPackageItem(item);

                TaskManager.ItemId = itemId;

                return(itemId);
            }
            catch (Exception ex)
            {
                // delete database if required
                if (databaseItemId > 0)
                {
                    DatabaseServerController.DeleteSqlDatabase(databaseItemId);
                }

                // delete user if required
                if (databaseUserItemId > 0)
                {
                    DatabaseServerController.DeleteSqlUser(databaseUserItemId);
                }

                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static int AddSite(SharePointSite 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
            QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.SHAREPOINT_SITES);
            if (quota.QuotaExhausted)
                return BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_QUOTA_LIMIT;

            // check if stats resource is available
            int serviceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.SharePoint);
            if (serviceId == 0)
                return BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_UNAVAILABLE;

            // check package items
            if (PackageController.GetPackageItemByName(item.PackageId, item.Name, typeof(SharePointSite)) != null)
                return BusinessErrorCodes.ERROR_SHAREPOINT_PACKAGE_ITEM_EXISTS;

            // place log record
            TaskManager.StartTask("SHAREPOINT", "ADD_SITE", item.Name);
            TaskManager.WriteParameter("Database group", item.DatabaseGroupName);
            TaskManager.WriteParameter("Database name", item.DatabaseName);
            TaskManager.WriteParameter("Database user", item.DatabaseUser);

            int databaseItemId = 0;
            int databaseUserItemId = 0;

            try
            {
                // load web site
                WebSite siteItem = (WebSite)PackageController.GetPackageItemByName(item.PackageId,
                    item.Name, typeof(WebSite));

                if (siteItem == null)
                    return BusinessErrorCodes.ERROR_WEB_SITE_SERVICE_UNAVAILABLE;

                // get service web site
                WebServer web = new WebServer();
                ServiceProviderProxy.Init(web, siteItem.ServiceId);
                WebSite site = web.GetSite(siteItem.SiteId);

                /////////////////////////////////////////
                //
                //  PREPARE SHAREPOINT SITE INSTALLATION
                //

				ServiceInfo wssService = ServerController.GetServiceInfo(serviceId);
				bool wss30 = (wssService.ProviderId == 23); // WSS 3.0

				// remember original web site bindings
				ServerBinding[] bindings = site.Bindings;

				// set application pool and root folder
				item.ApplicationPool = site.ApplicationPool;
				item.RootFolder = site.ContentPath;

                // change web site .NET framework if required
				bool siteUpdated = false;
                if (!wss30 && (site.AspNetInstalled != "1"
                    || site.DedicatedApplicationPool))
                {
                    site.AspNetInstalled = "1";
                    site.DedicatedApplicationPool = false;
                    web.UpdateSite(site);

					siteUpdated = true;
                }

				if (wss30 && site.AspNetInstalled != "2")
				{
					site.AspNetInstalled = "2";
					web.UpdateSite(site);

					siteUpdated = true;
				}

				if (siteUpdated)
				{
					site = web.GetSite(siteItem.SiteId);
					item.ApplicationPool = site.ApplicationPool;
				}

                if (site.FrontPageInstalled)
                {
                    // remove FrontPage
                    web.UninstallFrontPage(siteItem.SiteId, siteItem.FrontPageAccount);
                }
                
                // create SQL database
                SqlDatabase database = new SqlDatabase();
                database.PackageId = item.PackageId;
                database.Name = item.DatabaseName;
                databaseItemId = DatabaseServerController.AddSqlDatabase(database, item.DatabaseGroupName);
                if (databaseItemId < 0)
                    return databaseItemId;

                // create SQL user
                SqlUser dbUser = new SqlUser();
                dbUser.PackageId = item.PackageId;
                dbUser.Name = item.DatabaseUser;
                dbUser.Password = item.DatabasePassword;
                databaseUserItemId = DatabaseServerController.AddSqlUser(dbUser, item.DatabaseGroupName);
                if (databaseUserItemId < 0)
                    return databaseUserItemId;

                // delete SQL database from service
                // and change database user role
                int sqlServiceId = PackageController.GetPackageServiceId(item.PackageId, item.DatabaseGroupName);
                if (sqlServiceId < 0)
                    return BusinessErrorCodes.ERROR_MSSQL_RESOURCE_UNAVAILABLE;

                // load server settings
                StringDictionary sqlSettings = ServerController.GetServiceSettings(sqlServiceId);
                item.DatabaseServer = sqlSettings["ExternalAddress"];

                DatabaseServer dbServer = new DatabaseServer();
                ServiceProviderProxy.Init(dbServer, sqlServiceId);

                // delete database from service
                dbServer.DeleteDatabase(database.Name);

                // give SQL user "db_creator" role
                dbServer.ExecuteSqlNonQuery("master", String.Format(
                    "sp_addsrvrolemember '{0}', 'dbcreator'\nGO", dbUser.Name));

                // install SharePoint site
                SharePointServer sps = GetSharePoint(serviceId);
                sps.ExtendVirtualServer(item);

                // remove SQL user from "db_creator" role
                dbServer.ExecuteSqlNonQuery("master", String.Format(
                    "sp_dropsrvrolemember '{0}', 'dbcreator'\nGO", dbUser.Name));

				// restore original web site bindings
				web.UpdateSiteBindings(site.SiteId, bindings, false);

                // save statistics item
                item.ServiceId = serviceId;
                int itemId = PackageController.AddPackageItem(item);

                TaskManager.ItemId = itemId;

                return itemId;
            }
            catch (Exception ex)
            {
                // delete database if required
                if (databaseItemId > 0)
                    DatabaseServerController.DeleteSqlDatabase(databaseItemId);

                // delete user if required
                if (databaseUserItemId > 0)
                    DatabaseServerController.DeleteSqlUser(databaseUserItemId);

                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }