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>Generate the drop box edit details.</summary> /// <param name="file">The file to edit.</param> /// <param name="web">The web the file is in.</param> /// <param name="mode">The open mode.</param> /// <param name="sourceUrl">The source page.</param> /// <returns></returns> public DropBoxEditDetails GenerateDropBoxEditDetails(AssignmentFile file, SPWeb web, DropBoxEditMode mode, string sourceUrl) { DropBoxEditDetails details = new DropBoxEditDetails(); // Set default details details.Url = file.Url; details.OnClick = EditJavascript(file, web); try { bool isIpad = SlkUtilities.IsIpad(); if (settings.UseOfficeWebApps) { if (file.IsOfficeFile) { if (settings.OpenOfficeInIpadApp && isIpad) { details.Url = file.GenerateOfficeProtocolUrl(web, sourceUrl); details.OnClick = null; } else if (mode == DropBoxEditMode.Edit) { // Document must be 2010 format to be edited in office web apps if (file.IsOwaCompatible) { details.Url = file.GenerateOfficeAppsEditUrl(web, sourceUrl); details.OnClick = null; } } // Use Office Web Apps viewer for office files except for Excel which does not support it for pre 2010 worksheets else if (file.Extension.ToUpperInvariant() != "XLS") { details.Url = file.GenerateOfficeAppsViewUrl(web, sourceUrl); details.OnClick = null; } } } else if (settings.OpenOfficeInIpadApp && isIpad) { details.Url = file.GenerateOfficeProtocolUrl(web, sourceUrl); details.OnClick = null; } } catch (ArgumentOutOfRangeException) { // Not valid for office web apps details.Url = file.Url; details.OnClick = EditJavascript(file, web); } return details; }
/// <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); }
/// <summary> /// If a specified query string is present in the current HttpContext, /// its value is retrieved as a string and true is returned. If the query string is /// absent, then (a) if the caller specifies that the value is required, a /// SafeToDisplayException is thrown, or (b) if the caller specifies that /// the value is optional, null is retrieved and false is returned. /// </summary> /// <param name="queryStringName">Name of the QueryString.</param> /// <param name="isOptional">If false, SafeToDisplayException is thrown if /// the query string is absent.</param> static string Get(string queryStringName, bool isOptional) { string queryStringValue = HttpContext.Current.Request.QueryString[queryStringName]; queryStringValue = SlkUtilities.Trim(queryStringValue); if (queryStringValue == string.Empty) { queryStringValue = null; } bool isValidQueryString = (String.IsNullOrEmpty(queryStringValue) == false); if (!isValidQueryString && !isOptional) { throw new SafeToDisplayException(Resources.SlkExQueryStringNotFound, queryStringName); } return(queryStringValue); }
public RenderedCell[] RenderQueryRowCells(DataRow dataRow, int[,] columnMap, SPWebResolver spWebResolver, SPTimeZone timeZone) { if (dataRow == null) { throw new ArgumentNullException("dataRow"); } if (columnMap == null) { throw new ArgumentNullException("columnMap"); } RenderedCell[] renderedCells = new RenderedCell[m_columnDefinitions.Count]; int iColumnDefinition = 0; foreach (ColumnDefinition columnDefinition in m_columnDefinitions) { // set <cellValue> to the rendered value to be displayed in this cell (i.e. this // column of this row); set <cellSortKey> to the cell's sort key value (i.e. the value // to use for sorting); set <cellId> to the LearningStoreItemIdentifier associated // with this cell (null if none); set <cellToolTip> to the tooltip associated with // this cell (null if none) object cellValue, cellSortKey; LearningStoreItemIdentifier cellId; string cellToolTip; string text, textNotRounded; Guid? cellSiteGuid = null, cellWebGuid = null; string cellWebUrl = null; bool renderAsLink = false; switch (columnDefinition.RenderAs) { case ColumnRenderAs.Default: cellValue = dataRow[columnMap[iColumnDefinition, 0]]; if (cellValue is DBNull) { cellValue = (columnDefinition.NullDisplayString ?? String.Empty); cellSortKey = String.Empty; cellId = null; cellToolTip = null; } else { if ((cellId = cellValue as LearningStoreItemIdentifier) != null) { cellValue = cellSortKey = cellId.GetKey(); } else { cellSortKey = cellValue; } if (columnDefinition.ToolTipFormat != null) { cellToolTip = culture.Format(columnDefinition.ToolTipFormat, cellValue); } else { cellToolTip = null; } if (columnDefinition.CellFormat != null) { text = FormatValue(cellValue, columnDefinition.CellFormat); cellValue = text; cellSortKey = text.ToLower(culture.Culture); } } break; case ColumnRenderAs.UtcAsLocalDateTime: cellValue = dataRow[columnMap[iColumnDefinition, 0]]; if (cellValue is DBNull) { cellValue = (columnDefinition.NullDisplayString ?? String.Empty); cellSortKey = String.Empty; cellId = null; cellToolTip = null; } else { DateTime dateTime; try { dateTime = timeZone.UTCToLocalTime((DateTime)cellValue); } catch (InvalidCastException) { throw new SlkSettingsException(columnDefinition.LineNumber, culture.Resources.SlkUtilitiesViewColumnNameNonDateTime); } cellValue = cellSortKey = dateTime; if (columnDefinition.CellFormat != null) { cellValue = FormatValue(dateTime, columnDefinition.CellFormat); } cellId = null; if (columnDefinition.ToolTipFormat != null) { cellToolTip = culture.Format(columnDefinition.ToolTipFormat, dateTime); } else { cellToolTip = null; } } break; case ColumnRenderAs.SPWebTitle: case ColumnRenderAs.SPWebName: renderAsLink = (columnDefinition.RenderAs == ColumnRenderAs.SPWebName); cellWebGuid = GetQueryCell <Guid>(dataRow, columnMap, columnDefinition, iColumnDefinition, 0); cellSiteGuid = GetQueryCell <Guid>(dataRow, columnMap, columnDefinition, iColumnDefinition, 1); if (spWebResolver != null) { spWebResolver(cellWebGuid.Value, cellSiteGuid.Value, out text, out cellWebUrl); } else { text = null; } if (text == null) { text = cellWebGuid.Value.ToString(); } cellValue = text; cellSortKey = text.ToLower(culture.Culture); cellId = null; if (columnDefinition.ToolTipFormat != null) { cellToolTip = culture.Format(columnDefinition.ToolTipFormat, text); } else { cellToolTip = null; } break; case ColumnRenderAs.Link: text = GetQueryCell <string>(dataRow, columnMap, columnDefinition, iColumnDefinition, 0); cellId = GetQueryCell <LearningStoreItemIdentifier>(dataRow, columnMap, columnDefinition, iColumnDefinition, 1); cellValue = text; cellSortKey = text.ToLower(culture.Culture); if (columnDefinition.ToolTipFormat != null) { cellToolTip = culture.Format(columnDefinition.ToolTipFormat, text); } else { cellToolTip = null; } break; case ColumnRenderAs.LearnerAssignmentStatus: bool unused; LearnerAssignmentState learnerAssignmentState = (LearnerAssignmentState)GetQueryCell <int>(dataRow, columnMap, columnDefinition, iColumnDefinition, 0, out unused); text = SlkUtilities.GetLearnerAssignmentState(learnerAssignmentState); cellValue = text; cellSortKey = learnerAssignmentState; cellId = null; if (columnDefinition.ToolTipFormat != null) { cellToolTip = culture.Format(columnDefinition.ToolTipFormat, text); } else { cellToolTip = null; } break; case ColumnRenderAs.IfEmpty: cellToolTip = null; bool noColumn1; object column1 = GetQueryCell <object>(dataRow, columnMap, columnDefinition, iColumnDefinition, 0, out noColumn1); bool noColumn2; object column2 = GetQueryCell <object>(dataRow, columnMap, columnDefinition, iColumnDefinition, 1, out noColumn2); if (noColumn1 == false) { cellValue = column1; if (noColumn2 == false) { if (columnDefinition.ToolTipFormat != null) { cellToolTip = culture.Format("{0} ({1})", cellValue, column2); } } } else if (noColumn2) { cellValue = culture.Resources.SlkUtilitiesPointsNoValue; } else { cellValue = column2; } cellId = null; cellSortKey = cellValue.ToString().ToLower(culture.Culture); if (string.IsNullOrEmpty(cellToolTip)) { cellToolTip = cellValue.ToString(); } break; case ColumnRenderAs.ScoreAndPossible: // get <finalPoints> and <pointsPossible> from <dataRow> bool noFinalPoints; float finalPoints = GetQueryCell <float>(dataRow, columnMap, columnDefinition, iColumnDefinition, 0, out noFinalPoints); bool noPointsPossible; float pointsPossible = GetQueryCell <float>(dataRow, columnMap, columnDefinition, iColumnDefinition, 1, out noPointsPossible); // round to two decimal places float finalPointsRounded = (float)Math.Round(finalPoints, 2); float pointsPossibleRounded = (float)Math.Round(pointsPossible, 2); // format the result if (!noFinalPoints) { // FinalPoints is not NULL text = culture.Format(culture.Resources.SlkUtilitiesPointsValue, FormatValue(finalPointsRounded, columnDefinition.CellFormat)); textNotRounded = culture.Format(culture.Resources.SlkUtilitiesPointsValue, finalPoints); } else { // FinalPoints is NULL text = culture.Resources.SlkUtilitiesPointsNoValue; textNotRounded = culture.Resources.SlkUtilitiesPointsNoValue; } if (!noPointsPossible) { // PointsPossible is not NULL text = culture.Format(culture.Resources.SlkUtilitiesPointsPossible, text, FormatValue(pointsPossibleRounded, columnDefinition.CellFormat)); textNotRounded = culture.Format(culture.Resources.SlkUtilitiesPointsPossible, textNotRounded, pointsPossible); } cellValue = text; cellId = null; if ((columnDefinition.ToolTipFormat != null) && (!noFinalPoints || !noPointsPossible)) { cellToolTip = culture.Format(columnDefinition.ToolTipFormat, textNotRounded); } else { cellToolTip = null; } // set <cellSortKey> if (!noFinalPoints) { if (!noPointsPossible) { cellSortKey = ((double)finalPoints) / pointsPossible; } else { cellSortKey = (double)finalPoints; } } else { cellSortKey = (double)0; } break; case ColumnRenderAs.Submitted: int countCompletedOrFinal = GetQueryCell <int>(dataRow, columnMap, columnDefinition, iColumnDefinition, 0); int countTotal = GetQueryCell <int>(dataRow, columnMap, columnDefinition, iColumnDefinition, 1); text = culture.Format(culture.Resources.SlkUtilitiesSubmitted, countCompletedOrFinal, countTotal); cellValue = text; cellId = null; cellToolTip = null; cellSortKey = countCompletedOrFinal; break; default: throw new SlkSettingsException(columnDefinition.LineNumber, culture.Resources.SlkUtilitiesUnknownRenderAsValue, columnDefinition.RenderAs); } // add to <renderedCells> RenderedCell renderedCell; if (cellSiteGuid != null) { renderedCell = new WebNameRenderedCell(cellValue, cellSortKey, cellId, cellToolTip, columnDefinition.Wrap, cellSiteGuid.Value, cellWebGuid.Value, cellWebUrl); ((WebNameRenderedCell)renderedCell).RenderAsLink = renderAsLink; } else { renderedCell = new RenderedCell(cellValue, cellSortKey, cellId, cellToolTip, columnDefinition.Wrap); } renderedCells[iColumnDefinition++] = renderedCell; } return(renderedCells); }