From 7d14ea62f145dce299da04f8faa276e50baefbea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Janis=20Daniel=20Da=CC=88hne?=
 <janis.daehne2@student.uni-halle.de>
Date: Wed, 20 Nov 2019 22:10:13 +0100
Subject: [PATCH] - fixed issue where hidden files could be displayed to users
 when saving a solution or creating a after solution

---
 src/ClientServer/Config/Constants.cs          |  2 +-
 .../DoExerciseAfterSolutionController.cs      | 19 +++++++++++-----
 .../Core/Exercises/DoExerciseController.cs    | 22 ++++++++++++++++++-
 .../Core/Testing/TestingController.cs         |  2 +-
 .../Helpers/UserSolutionHelper.cs             |  2 +-
 .../Models/Exercises/TemplateFile.cs          |  6 +++--
 6 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/src/ClientServer/Config/Constants.cs b/src/ClientServer/Config/Constants.cs
index 260ad43..074938e 100644
--- a/src/ClientServer/Config/Constants.cs
+++ b/src/ClientServer/Config/Constants.cs
@@ -13,7 +13,7 @@ namespace ClientServer.Helpers
     /// </summary>
     public static class Constants
     {
-        public static string VersionString = "2.6.2";
+        public static string VersionString = "2.6.3";
 
         /// <summary>
         /// this is only set once at program.cs!!
diff --git a/src/ClientServer/Controllers/Core/Exercises/DoExerciseAfterSolution/DoExerciseAfterSolutionController.cs b/src/ClientServer/Controllers/Core/Exercises/DoExerciseAfterSolution/DoExerciseAfterSolutionController.cs
index 5df260b..81331a1 100644
--- a/src/ClientServer/Controllers/Core/Exercises/DoExerciseAfterSolution/DoExerciseAfterSolutionController.cs
+++ b/src/ClientServer/Controllers/Core/Exercises/DoExerciseAfterSolution/DoExerciseAfterSolutionController.cs
@@ -422,12 +422,12 @@ namespace ClientServer.Controllers.Core.Exercises.DoExerciseAfterSolution
                     else
                     {
                         //create a new file (template exists)
-
-                        if (oldTemplate.IsEditableByUser) //use user content
-                        {
+ 
+                        if (oldTemplate.IsEditableByUser && oldTemplate.IsContentVisibleForUser) {
+                            
                             file = new AfterSolutionFile()
                             {
-                                Content = oldTemplate.IsContentVisibleForUser ? solutionFile.Content : "",
+                                Content = solutionFile.Content,
                                 FileNameWithExtension = solutionFile.FileNameWithExtension,
                                 TemplateFile = oldTemplate,
                                 IsDisplayed = solutionFile.UiIsDisplayed
@@ -443,7 +443,7 @@ namespace ClientServer.Controllers.Core.Exercises.DoExerciseAfterSolution
                             //better use the file from the template here...
                             file = new AfterSolutionFile()
                             {
-                                Content = oldTemplate.IsContentVisibleForUser ? oldTemplate.Content : "",
+                                Content = oldTemplate.Content,
                                 FileNameWithExtension = oldTemplate.FileNameWithExtension,
                                 TemplateFile = oldTemplate,
                                 IsDisplayed = solutionFile.UiIsDisplayed
@@ -563,6 +563,15 @@ namespace ClientServer.Controllers.Core.Exercises.DoExerciseAfterSolution
 
             //return after solution e.g. we corrected some files...
 
+            var errorMsg = UserSolutionHelper.ReplaceReadonlyFiles(oldAfterSolution, oldCodeTemplateForPLang, true, false);
+
+            if (errorMsg != null)
+            {
+                await
+                    Response.WriteAsync(
+                        Jc.Serialize(new BasicResponse(ResponseCode.ServerError, errorMsg)));
+                return;
+            }
 
             var solutionVersionForFrontend = new AfterSolutionDoExerciseFullBase()
             {
diff --git a/src/ClientServer/Controllers/Core/Exercises/DoExerciseController.cs b/src/ClientServer/Controllers/Core/Exercises/DoExerciseController.cs
index c4ec07b..26e4658 100644
--- a/src/ClientServer/Controllers/Core/Exercises/DoExerciseController.cs
+++ b/src/ClientServer/Controllers/Core/Exercises/DoExerciseController.cs
@@ -867,7 +867,7 @@ namespace ClientServer.Controllers.Core.Exercises
             {
                 var files = new SolutionFile()
                 {
-                    Content = templateFile.IsContentVisibleForUser ? templateFile.Content : "",
+                    Content = templateFile.Content, //always create initial content even if file is hidden or readonly, api must ensure that this is not send to the user
                     DisplayIndex = templateFile.DisplayIndex,
                     FileNameWithExtension = templateFile.FileNameWithExtension,
                     IsDisplayed = true,
@@ -1932,6 +1932,15 @@ namespace ClientServer.Controllers.Core.Exercises
                 return;
             }
 
+            string errorMsg = UserSolutionHelper.ReplaceReadonlyFiles(oldSolution, oldCodeTemplateForPLang, true, false);
+
+            if (string.IsNullOrEmpty(errorMsg) == false)
+            {
+                await
+                    Response.WriteAsync(
+                        Jc.Serialize(new BasicResponse(ResponseCode.ServerError, "could not replace readonly or hiden files")));
+                return;
+            }
 
             var solutionVersionForFrontend = new SolutionDoExerciseFullBase()
             {
@@ -2093,6 +2102,17 @@ namespace ClientServer.Controllers.Core.Exercises
                 oldExerciseReleaseWithUserAsParticipation);
 
             if (resetSolution == null) return;
+            
+            
+            string errorMsg = UserSolutionHelper.ReplaceReadonlyFiles(resetSolution, oldCodeTemplateForPLang, true, false);
+
+            if (string.IsNullOrEmpty(errorMsg) == false)
+            {
+                await
+                    Response.WriteAsync(
+                        Jc.Serialize(new BasicResponse(ResponseCode.ServerError, "could not replace readonly or hiden files")));
+                return;
+            }
 
 
             var solutionVersionForFrontend = new SolutionDoExerciseFullBase()
diff --git a/src/ClientServer/Controllers/Core/Testing/TestingController.cs b/src/ClientServer/Controllers/Core/Testing/TestingController.cs
index 36b12ee..0299185 100644
--- a/src/ClientServer/Controllers/Core/Testing/TestingController.cs
+++ b/src/ClientServer/Controllers/Core/Testing/TestingController.cs
@@ -1112,7 +1112,7 @@ namespace ClientServer.Controllers.Core.Testing
             var solution = new Solution()
             {
                 CreatedAt = DateTime.Now,
-                Note = "compile single file",
+                Note = "Just run program",
                 SolutionFiles = files,
                 MainFile = mainFile,
                 MainFileId = mainFile.Id,
diff --git a/src/ClientServer/Helpers/UserSolutionHelper.cs b/src/ClientServer/Helpers/UserSolutionHelper.cs
index 77aac11..dc30de2 100644
--- a/src/ClientServer/Helpers/UserSolutionHelper.cs
+++ b/src/ClientServer/Helpers/UserSolutionHelper.cs
@@ -14,7 +14,7 @@ namespace ClientServer.Helpers
     public static class UserSolutionHelper
     {
         /// <summary>
-        /// replaces all readonly file content (an name) with the contents from the coe template file
+        /// replaces all readonly file content (and name) with the contents from the code template file
         /// also replaces hidden files with the real content
         /// </summary>
         /// <param name="solutionWithFiles">the solution with template files loaded</param>
diff --git a/src/ClientServer/Models/Exercises/TemplateFile.cs b/src/ClientServer/Models/Exercises/TemplateFile.cs
index 1a4d774..8e6aa4e 100644
--- a/src/ClientServer/Models/Exercises/TemplateFile.cs
+++ b/src/ClientServer/Models/Exercises/TemplateFile.cs
@@ -30,11 +30,13 @@ namespace ClientServer.Models.Exercises
 
         /// <summary>
         /// true: content is visible for the user,
-        /// false: the content is set to the empty string before sending to frontend,
+        /// false: the content is set to the empty string before sending to frontend (this file content is not changed in the db),
+        ///     normally the initial solution will contain the template file code when the user solution was created
+        ///         this is that we have a "working" old user solution even if the exercise changes...
         ///     this only applies for the do exercise view (exercise creators and tutors need to see the content)
         ///     this means that the user cannot edit this file (content or name)
         ///
-        /// IF IsEditableByUser is true then this is false the content is hidden and the file is readonly!
+        /// IF IsEditableByUser is true and this is false the content is hidden and the file is readonly!
         /// </summary>
         public bool IsContentVisibleForUser { get; set; }
 
-- 
GitLab