void AssignUpdatePermissions(AssignmentFolder folder, SPUser learner, LearnerAssignmentState? status) { folder.RemoveAllPermissions(); if (status != null) { switch (status.Value) { case LearnerAssignmentState.NotStarted: break; case LearnerAssignmentState.Active: ApplyInstructorsReadAccessPermissions(folder); folder.ApplyPermission(learner, SPRoleType.Contributor); break; case LearnerAssignmentState.Completed: ApplyInstructorsContributeAccessPermissions(folder); break; case LearnerAssignmentState.Final: ApplyInstructorsReadAccessPermissions(folder); folder.ApplyPermission(learner, SPRoleType.Reader); break; } } }
void ApplyInstructorsReadAccessPermissions(AssignmentFolder folder) { foreach (SlkUser instructor in assignmentProperties.Instructors) { folder.ApplyPermission(instructor.SPUser, SPRoleType.Reader); } }
void ApplyInstructorsReadAccessPermissionsToDropBox(SPWeb web, DropBox dropBox) { foreach (SlkUser instructor in assignmentProperties.Instructors) { AssignmentFolder.ApplySharePointPermission(web, dropBox.DropBoxList, instructor.SPUser, SPRoleType.Reader); } }
/// <summary>Creates an assignment folder.</summary> /// <returns>The created folder or null if it already exists.</returns> public AssignmentFolder CreateSelfAssignmentFolder() { AssignmentFolder assignmentFolder = null; SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite spSite = new SPSite(assignmentProperties.SPSiteGuid)) { using (SPWeb spWeb = spSite.OpenWeb(assignmentProperties.SPWebGuid)) { DropBox dropBox = new DropBox(store, spWeb); //Get the folder if it exists assignmentFolder = dropBox.GetAssignmentFolder(assignmentProperties); //Create the assignment folder if it does not exist if (assignmentFolder == null) { assignmentFolder = dropBox.CreateAssignmentFolder(assignmentProperties); assignmentFolder.ApplyPermission(CurrentUser, SPRoleType.Reader); assignmentFolder.CreateLearnerAssignmentFolder(CurrentUser); } else { assignmentFolder = null; } } } }); return assignmentFolder; }
void ApplyInstructorsReadAccessPermissions(AssignmentFolder folder, SPWeb web, DropBox dropBox) { // In one instance had an issue that instructors couldn't see uploaded assignments in OWA if they // didn't have read access on the drop box. ApplyInstructorsReadAccessPermissionsToDropBox(web, dropBox); ApplyInstructorsReadAccessPermissions(folder); }
/// <summary>Creates an assignment folder.</summary> public void CreateAssignmentFolder() { Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest(); SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite spSite = new SPSite(assignmentProperties.SPSiteGuid)) { using (SPWeb spWeb = spSite.OpenWeb(assignmentProperties.SPWebGuid)) { DropBox dropBox = new DropBox(store, spWeb); //Get the folder if it exists if (dropBox.GetAssignmentFolder(assignmentProperties) != null) { throw new SafeToDisplayException(culture.Resources.AssFolderAlreadyExists); } AssignmentFolder assignmentFolder = dropBox.CreateAssignmentFolder(assignmentProperties); ApplyInstructorsReadAccessPermissions(assignmentFolder, spWeb, dropBox); //Create a Subfolder for each learner foreach (SlkUser learner in assignmentProperties.Learners) { SPUser spLearner = learner.SPUser; assignmentFolder.ApplyPermission(spLearner, SPRoleType.Reader); AssignmentFolder.ApplySharePointPermission(spWeb, dropBox.DropBoxList, learner.SPUser, SPRoleType.Reader); assignmentFolder.CreateLearnerAssignmentFolder(spLearner); } } } }); }
/// <summary>Gets the assignment folder and creates it if it doesn't exist.</summary> /// <param name="properties">The assignment properties.</param> /// <returns>The assignment folder.</returns> public AssignmentFolder GetOrCreateAssignmentFolder(AssignmentProperties properties) { AssignmentFolder folder = GetAssignmentFolder(properties); if (folder == null) { return(CreateAssignmentFolder(properties)); } else { return(folder); } }
/// <summary>Changes the assignment folder name.</summary> /// <param name="oldAssignmentFolderName">The old assignment name.</param> /// <param name="newAssignmentFolderName">The new assignment name.</param> public void ChangeFolderName(string oldAssignmentFolderName, string newAssignmentFolderName) { //Get old assignment folder AssignmentFolder oldAssignmentFolder = GetAssignmentFolder(oldAssignmentFolderName, null); if (oldAssignmentFolder != null) { oldAssignmentFolder.ChangeName(oldAssignmentFolderName, newAssignmentFolderName); } else { throw new SafeToDisplayException(culture.Resources.AssFolderNotFound); } }
void DeleteRemovedLearnerFolders(AssignmentFolder assignmentFolder, AssignmentProperties oldAssignmentProperties) { foreach (SlkUser oldLearner in oldAssignmentProperties.Learners) { if (!assignmentProperties.Learners.Contains(oldLearner.UserId)) { // Get learner subfolder, and delete it if exists AssignmentFolder learnerSubFolder = assignmentFolder.FindLearnerFolder(oldLearner.SPUser); if (learnerSubFolder != null) { learnerSubFolder.Delete(); } } } }
/// <summary>Sets the correct permissions when the item is submitted.</summary> /// <param name="web">The web the assignment is for.</param> void ApplySubmittedPermissions(SPWeb web) { DropBox dropBox = new DropBox(store, web); AssignmentFolder assignmentFolder = dropBox.GetAssignmentFolder(assignmentProperties); if (assignmentFolder == null) { assignmentFolder = dropBox.CreateAssignmentFolder(assignmentProperties); } else { AssignmentFolder learnerSubFolder = assignmentFolder.FindLearnerFolder(CurrentUser); ApplySubmittedPermissions(learnerSubFolder); } }
/// <summary>Applies permissions to the learner folder and creates it if required.</summary> /// <param name="user">The learner to create the folder for.</param> /// <param name="learnerPermissions">The permissions to set.</param> /// <param name="instructorPermissions">The instructor permissions to set.</param> /// <param name="removeObserverPermissions">Whether to remove observer permissions or not.</param> void ApplyAssignmentPermission(SPUser user, SPRoleType learnerPermissions, SPRoleType instructorPermissions, bool removeObserverPermissions) { SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite spSite = new SPSite(assignmentProperties.SPSiteGuid)) { using (SPWeb spWeb = spSite.OpenWeb(assignmentProperties.SPWebGuid)) { DropBox dropBox = new DropBox(store, spWeb); AssignmentFolder assignmentFolder = dropBox.GetAssignmentFolder(assignmentProperties); if (assignmentFolder != null) { // Get the learner sub folder AssignmentFolder learnerSubFolder = assignmentFolder.FindLearnerFolder(user); if (learnerSubFolder == null) { learnerSubFolder = assignmentFolder.CreateLearnerAssignmentFolder(user); } // Apply learner permissions learnerSubFolder.RemovePermissions(user); if (learnerPermissions != SPRoleType.None) { learnerSubFolder.ApplyPermission(user, learnerPermissions); } // Apply instructor permissions foreach (SlkUser instructor in assignmentProperties.Instructors) { learnerSubFolder.RemovePermissions(instructor.SPUser); learnerSubFolder.ApplyPermission(instructor.SPUser, instructorPermissions); } if (removeObserverPermissions) { learnerSubFolder.RemoveObserverPermission(); } } } } }); }
/// <summary>Uploads files to the learner's drop box.</summary> /// <param name="files">The files to upload.</param> /// <param name="existingFilesToKeep">Existing files to keep.</param> public void UploadFiles(AssignmentUpload[] files, int[] existingFilesToKeep) { SPUser currentUser = CurrentUser; SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite spSite = new SPSite(assignmentProperties.SPSiteGuid)) { spSite.CatchAccessDeniedException = false; using (SPWeb spWeb = spSite.OpenWeb(assignmentProperties.SPWebGuid)) { DropBox dropBox = new DropBox(store, spWeb); AssignmentFolder assignmentFolder = dropBox.GetOrCreateAssignmentFolder(assignmentProperties); assignmentFolder.ApplyPermission(currentUser, SPRoleType.Reader); AssignmentFolder learnerSubFolder = assignmentFolder.FindLearnerFolder(currentUser); if (learnerSubFolder == null) { learnerSubFolder = assignmentFolder.CreateLearnerAssignmentFolder(currentUser); } else { learnerSubFolder.ResetIsLatestFiles(existingFilesToKeep, currentUser.ID); } CheckExtensions(spSite, files); using (new AllowUnsafeUpdates(spWeb)) { SlkUser currentSlkUser = new SlkUser(currentUser); foreach (AssignmentUpload upload in files) { learnerSubFolder.SaveFile(upload.Name, upload.Stream, currentSlkUser); } } ApplySubmittedPermissions(spWeb); } } }); }
void ApplySubmittedPermissions(AssignmentFolder learnerSubFolder) { // IF the assignment is auto return, the learner will still be able to view the drop box assignment files // otherwise, learner permissions will be removed from the learner's subfolder in the Drop Box document library if (assignmentProperties.AutoReturn == false) { learnerSubFolder.RemovePermissions(CurrentUser); } // Grant instructors contribute permission on learner subfolder foreach (SlkUser instructor in assignmentProperties.Instructors) { if (instructor.SPUser == null) { throw new SafeToDisplayException(string.Format(culture.Culture, culture.Resources.DropBoxManagerUploadFilesNoInstructor, instructor.Name)); } learnerSubFolder.RemovePermissions(instructor.SPUser); learnerSubFolder.ApplyPermission(instructor.SPUser, SPRoleType.Contributor); } }
private AssignmentFile SaveFile(SPFile file) { AssignmentFile assignmentFile = null; using (SPSite destinationSite = new SPSite(assignmentProperties.SPSiteGuid)) { destinationSite.CatchAccessDeniedException = false; using (SPWeb destinationWeb = destinationSite.OpenWeb(assignmentProperties.SPWebGuid)) { using (new AllowUnsafeUpdates(destinationWeb)) { // Temporarily turn off property promotion. Possible cause for exceptions saving docs. I don't actually know // if settings this will disable it as without an update it's not persisted and we don't want to persist the // setting, just turn it off for this addition. It may even work asynchronously. destinationWeb.ParserEnabled = false; SPUser learner = CurrentUser; DropBox dropBox = new DropBox(store, destinationWeb); AssignmentFolder assignmentFolder = dropBox.GetAssignmentFolder(assignmentProperties); AssignmentFolder learnerSubFolder = null; if (assignmentFolder == null) { assignmentFolder = dropBox.CreateAssignmentFolder(assignmentProperties); } // ApplyAssignmentPermission creates the learner folder if required ApplyAssignmentPermission(learner, SPRoleType.Contributor, SPRoleType.Reader, true); learnerSubFolder = assignmentFolder.FindLearnerFolder(learner); using (Stream stream = file.OpenBinaryStream()) { assignmentFile = learnerSubFolder.SaveFile(file.Name, stream, new SlkUser(learner)); } } } } return assignmentFile; }
/// <summary>Deletes the assignment folder.</summary> public void DeleteAssignmentFolder() { SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite spSite = new SPSite(assignmentProperties.SPSiteGuid)) { using (SPWeb spWeb = spSite.OpenWeb(assignmentProperties.SPWebGuid)) { DropBox dropBox = new DropBox(store, spWeb); //Get the folder if it exists AssignmentFolder assignmentFolder = dropBox.GetAssignmentFolder(assignmentProperties); if (assignmentFolder != null) { using (new AllowUnsafeUpdates(spWeb)) { assignmentFolder.Delete(); } } } } }); }
/// <summary>Function to update the DropBox document library after the current assignment being edited</summary> /// <param name="oldAssignmentProperties">The old assignment properties</param> public void UpdateAssignment(AssignmentProperties oldAssignmentProperties) { SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite spSite = new SPSite(assignmentProperties.SPSiteGuid)) { using (SPWeb spWeb = spSite.OpenWeb(assignmentProperties.SPWebGuid)) { using (new AllowUnsafeUpdates(spWeb)) { DropBox dropBox = new DropBox(store, spWeb); string oldAssignmentFolderName = DropBox.GenerateFolderName(oldAssignmentProperties); string newAssignmentFolderName = DropBox.GenerateFolderName(assignmentProperties); // If assignment title has been changed, create a new assignment folder and move old assignment folder contents to it if (string.Compare(oldAssignmentFolderName, newAssignmentFolderName, true, CultureInfo.InvariantCulture) != 0) { dropBox.ChangeFolderName(oldAssignmentFolderName, newAssignmentFolderName); } // Get new assignment folder, or the old one if the title has not been changed // in both cases, the value of the current assignment folder name will be stored in newAssignmentFolderName AssignmentFolder assignmentFolder = dropBox.GetAssignmentFolder(assignmentProperties); if (assignmentFolder != null) { assignmentFolder.RemoveAllPermissions(); // Grant assignment instructors Read permission on the assignment folder ApplyInstructorsReadAccessPermissions(assignmentFolder, spWeb, dropBox); // Delete subfolders of the learners who have been removed from the assignment DeleteRemovedLearnerFolders(assignmentFolder, oldAssignmentProperties); foreach (SlkUser learner in assignmentProperties.Learners) { // Grant assignment learners Read permission on the assignment folder and drop box assignmentFolder.ApplyPermission(learner.SPUser, SPRoleType.Reader); AssignmentFolder.ApplySharePointPermission(spWeb, dropBox.DropBoxList, learner.SPUser, SPRoleType.Reader); AssignmentFolder learnerSubFolder = assignmentFolder.FindLearnerFolder(learner.SPUser); LearnerAssignmentProperties result = assignmentProperties.ResultForLearner(learner); if (learnerSubFolder == null) { // Create a new subfolder for this learner learnerSubFolder = assignmentFolder.CreateLearnerAssignmentFolder(learner.SPUser); } if (result != null) { AssignUpdatePermissions(learnerSubFolder, learner.SPUser, result.Status); } } } } } } }); }