static void CreateDatabase(SlkSPSiteMapping mapping, string databaseName, string appPoolAccountName, string databaseSchema)
        {
            // restrict the characters in <databaseName>
            if (!Regex.Match(databaseName, @"^\w+$").Success)
            {
                throw new SafeToDisplayException(SlkCulture.GetResources().InvalidDatabaseName, databaseName);
            }

            // if <appPoolAccountName> is null, set it to the name of the application pool account
            // (e.g. "NT AUTHORITY\NETWORK SERVICE"); set <appPoolSid> to its SID
            byte[] appPoolSid = null;
            if (appPoolAccountName == null)
            {
                using (SPSite site = new SPSite(mapping.SPSiteGuid))
                {
                    appPoolAccountName = site.WebApplication.ApplicationPool.Username;
                }
            }

            NTAccount          appPoolAccount = new NTAccount(appPoolAccountName);
            SecurityIdentifier securityId     =
                (SecurityIdentifier)appPoolAccount.Translate(typeof(SecurityIdentifier));

            appPoolSid = new byte[securityId.BinaryLength];
            securityId.GetBinaryForm(appPoolSid, 0);

            SlkUtilities.ImpersonateAppPool(delegate()
            {
                CreateDatabase(mapping.DatabaseServerConnectionString, databaseName,
                               mapping.DatabaseConnectionString, appPoolAccountName,
                               appPoolSid, databaseSchema);
            });
        }
        /// <summary>Loads SLK configuration information from WSS and LearningStore, in a form that's
        /// suitable for copying to Configure.aspx form fields. </summary>
        ///
        /// <param name="spSiteGuid">The GUID of the SPSite to retrieve configuration information
        ///     from.</param>
        ///
        /// <returns>An AdministrationConfiguration.</returns>
        ///
        /// <remarks>
        /// This method is static so it can used outside the context of IIS.  Only SharePoint
        /// administrators can perform this function.
        /// </remarks>
        ///
        /// <exception cref="SafeToDisplayException">
        /// An error occurred that can be displayed to a browser user.
        /// </exception>
        ///
        public static AdministrationConfiguration LoadConfiguration(Guid spSiteGuid)
        {
            AdministrationConfiguration configuration = new AdministrationConfiguration(spSiteGuid);

            // only SharePoint administrators can perform this action
            CheckPermissions();

            // set <mapping> to the mapping between <spSiteGuid> and the LearningStore connection
            // information for that SPSite
            SlkSPSiteMapping mapping = SlkSPSiteMapping.GetMapping(spSiteGuid);

            // set "out" parameters based on <mappingExists> and <mapping>
            if (mapping != null)
            {
                // the mapping exists -- set "out" parameters based on <mapping>
                configuration.DatabaseServer       = mapping.DatabaseServer;
                configuration.DatabaseName         = mapping.DatabaseName;
                configuration.InstructorPermission = mapping.InstructorPermission;
                configuration.LearnerPermission    = mapping.LearnerPermission;

                // The below given condition will be true only during the migration of SLK from
                // 'SLK without Observer role' to 'SLK with Observer role' implementation
                if (mapping.ObserverPermission == null)
                {
                    mapping.ObserverPermission = LoadCulture(spSiteGuid).Resources.DefaultSlkObserverPermissionName;
                }
                configuration.ObserverPermission = mapping.ObserverPermission;
            }
            else
            {
                SlkCulture        siteCulture    = LoadCulture(spSiteGuid);
                AppResourcesLocal adminResources = SlkCulture.GetResources();

                configuration.IsNewConfiguration = true;
                mapping = SlkSPSiteMapping.CreateMapping(spSiteGuid);
                // the mapping doesn't exist -- set "out" parameters to default values
                SPWebService adminWebService = SlkAdministration.GetAdminWebService();
                configuration.DatabaseServer       = adminWebService.DefaultDatabaseInstance.Server.Address;
                configuration.DatabaseName         = adminResources.DefaultSlkDatabaseName;
                mapping.DatabaseServer             = configuration.DatabaseServer;
                mapping.DatabaseName               = configuration.DatabaseName;
                configuration.InstructorPermission = siteCulture.Resources.DefaultSlkInstructorPermissionName;
                configuration.LearnerPermission    = siteCulture.Resources.DefaultSlkLearnerPermissionName;
                configuration.ObserverPermission   = siteCulture.Resources.DefaultSlkObserverPermissionName;
            }

            // set "out" parameters that need to be computed
            bool createDatabaseResult = false;

            SlkUtilities.ImpersonateAppPool(delegate()
            {
                createDatabaseResult = !DatabaseExists(mapping.DatabaseServerConnectionString, mapping.DatabaseName);
            });
            configuration.CreateDatabase = createDatabaseResult;

            return(configuration);
        }
        /// <summary>
        /// Returns <c>true</c> if all SharePoint permissions specified by a given set of permission
        /// names exist in the root web site of a given SPSite, <c>false</c> if not.
        /// </summary>
        bool PermissionsExist()
        {
            bool returnValue = true;

            SlkUtilities.ImpersonateAppPool(delegate()
            {
                bool catchAccessDenied = SPSecurity.CatchAccessDeniedException;
                try
                {
                    SPSecurity.CatchAccessDeniedException = false;

                    // populate <existingPermissions> with the existing permissions on the root SPWeb of the
                    // site with GUID <spSiteGuid>
                    Dictionary <string, bool> existingPermissions = new Dictionary <string, bool>(20);
                    using (SPSite spSite = new SPSite(siteId))
                    {
                        using (SPWeb rootWeb = spSite.RootWeb)
                        {
                            foreach (SPRoleDefinition roleDef in rootWeb.RoleDefinitions)
                            {
                                existingPermissions[roleDef.Name] = true;
                            }
                        }
                    }

                    if (existingPermissions.ContainsKey(LearnerPermission) == false)
                    {
                        returnValue = false;
                    }
                    else if (existingPermissions.ContainsKey(InstructorPermission) == false)
                    {
                        returnValue = false;
                    }
                    else if (existingPermissions.ContainsKey(ObserverPermission) == false)
                    {
                        returnValue = false;
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    SlkCulture culture = new SlkCulture();
                    throw new SafeToDisplayException(culture.Resources.NoAccessToSite);
                }
                finally
                {
                    SPSecurity.CatchAccessDeniedException = catchAccessDenied;
                }
            });

            return(returnValue);
        }