From 23bc83e404efca84c0f14e63636d00b69593ace1 Mon Sep 17 00:00:00 2001 From: user Date: Fri, 4 Aug 2023 02:48:00 +0200 Subject: [PATCH 01/17] Update Lib_CSharp --- Lib_CSharp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib_CSharp b/Lib_CSharp index bb97ff32..f512d8ba 160000 --- a/Lib_CSharp +++ b/Lib_CSharp @@ -1 +1 @@ -Subproject commit bb97ff32e5ae5adbb3525b5b03c69cce697fe860 +Subproject commit f512d8ba2d933b9491dc55f39299413675a6cf53 From 797af1dc3c9e98739b222427e3f1d864eda0cce4 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 12:26:47 +0200 Subject: [PATCH 02/17] Update PoliFemoBackend.csproj --- Backend/PoliFemoBackend.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/Backend/PoliFemoBackend.csproj b/Backend/PoliFemoBackend.csproj index fd3b2e42..476a299a 100644 --- a/Backend/PoliFemoBackend.csproj +++ b/Backend/PoliFemoBackend.csproj @@ -57,6 +57,7 @@ + From 5de8fcd28574d2f2936e7de7b86b9fcff5bdee96 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 13:56:11 +0200 Subject: [PATCH 03/17] Update PoliFemoBackend.csproj, DiscoverPeopleController.cs, DiscoverInfoBio.cs, and 3 more files --- Backend/PoliFemoBackend.csproj | 1 - .../DiscoverPeopleController.cs | 58 ++++++++++++ .../DiscoverPeople/Info/DiscoverInfoBio.cs | 56 ++++++++++++ .../DiscoverPeople/Info/DiscoverInfoLink.cs | 56 ++++++++++++ .../DiscoverPeople/MatchController.cs | 89 +++++++++++++++++++ .../Controllers/DiscoverPeople/UserUtil.cs | 18 ++++ 6 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs create mode 100644 Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs create mode 100644 Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs create mode 100644 Backend/Source/Controllers/DiscoverPeople/MatchController.cs create mode 100644 Backend/Source/Controllers/DiscoverPeople/UserUtil.cs diff --git a/Backend/PoliFemoBackend.csproj b/Backend/PoliFemoBackend.csproj index 476a299a..fd3b2e42 100644 --- a/Backend/PoliFemoBackend.csproj +++ b/Backend/PoliFemoBackend.csproj @@ -57,7 +57,6 @@ - diff --git a/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs b/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs new file mode 100644 index 00000000..f417882f --- /dev/null +++ b/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs @@ -0,0 +1,58 @@ +#region + +using System.Data; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; +using PoliFemoBackend.Source.Data; +using PoliFemoBackend.Source.Utils.Auth; +using DB = PoliNetwork.Db.Utils.Database; + +#endregion + +namespace PoliFemoBackend.Source.Controllers.DiscoverPeople; + +[ApiController] +[ApiExplorerSettings(GroupName = "DiscoverPeople")] +public class DiscoverPeopleController : ControllerBase +{ + /// + /// Discover people + /// + /// A JSON object of people + /// Request completed successfully + /// No available people + /// Can't connect to the server + [HttpGet] + [Authorize] + [Route("/discoverpeople/random")] + [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] + public ActionResult GetRandomPeople() + { + var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); + if (string.IsNullOrEmpty(tempSub)) + return new EmptyResult(); + + var a = NewPeople(tempSub); + return a == null ? NotFound() : Ok(a); + } + + + private static JObject? NewPeople(string tempSub) + { + var results = DB.ExecuteSelect( + "SELECT user_id, discover_bio " + + "FROM Users " + + "WHERE user_id NOT IN (SELECT to_person PeopleDiscoverMatch WHERE from_person = @id) ORDER BY RAND() LIMIT 10", + GlobalVariables.DbConfigVar, + new Dictionary + { + { "@id", tempSub } + }); + + var row = results?.Rows[0]; + return row == null ? null : UserUtil.GetUser(row); + } + + +} \ No newline at end of file diff --git a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs new file mode 100644 index 00000000..d0713f62 --- /dev/null +++ b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs @@ -0,0 +1,56 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; +using PoliFemoBackend.Source.Data; +using PoliFemoBackend.Source.Utils.Auth; +using DB = PoliNetwork.Db.Utils.Database; + +namespace PoliFemoBackend.Source.Controllers.DiscoverPeople.Info; + +public class DiscoverInfoBio : ControllerBase +{ + [HttpPost] + [Authorize] + [Route("/discoverpeople/info/setBio/{stringBio}")] + [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] + public ActionResult SetBio(string stringBio) + { + var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); + return string.IsNullOrEmpty(tempSub) ? new EmptyResult() : SetBio(tempSub, stringBio, this); + } + + [HttpGet] + [Authorize] + [Route("/discoverpeople/info/getBio/")] + [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] + public ActionResult GetBio() + { + var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); + return string.IsNullOrEmpty(tempSub) ? new EmptyResult() : GetBioUtil(tempSub, this); + } + + private static ActionResult GetBioUtil(string tempSub, ControllerBase discoverInfo) + { + const string q = "SELECT discover_bio FROM Users WHERE user_id = @id"; + var i = DB.ExecuteSelect(q, GlobalVariables.DbConfigVar, new Dictionary() + { + { "@id", tempSub } + }); + if (i == null) + return discoverInfo.NotFound(); + + var value = i.Rows[0].ItemArray[0]?.ToString(); + return discoverInfo.Ok(new JObject() { { "bio", value } }); + } + + private static ActionResult SetBio(string tempSub, string stringBio, ControllerBase discoverInfo) + { + const string q = "UPDATE Users SET discover_bio = @bio WHERE user_id = @id"; + var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary() + { + { "@id", tempSub }, + { "@bio", stringBio } + }); + return discoverInfo.Ok(i); + } +} \ No newline at end of file diff --git a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs new file mode 100644 index 00000000..08c404e2 --- /dev/null +++ b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs @@ -0,0 +1,56 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; +using PoliFemoBackend.Source.Data; +using PoliFemoBackend.Source.Utils.Auth; +using DB = PoliNetwork.Db.Utils.Database; + +namespace PoliFemoBackend.Source.Controllers.DiscoverPeople.Info; + +public class DiscoverInfoLink : ControllerBase +{ + [HttpPost] + [Authorize] + [Route("/discoverpeople/info/setLink/{stringLink}")] + [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] + public ActionResult SetLink(string stringLink) + { + var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); + return string.IsNullOrEmpty(tempSub) ? new EmptyResult() : SetLink(tempSub, stringLink, this); + } + + [HttpGet] + [Authorize] + [Route("/discoverpeople/info/getLink/")] + [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] + public ActionResult GetLink() + { + var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); + return string.IsNullOrEmpty(tempSub) ? new EmptyResult() : GetLinkUtil(tempSub, this); + } + + private static ActionResult GetLinkUtil(string tempSub, ControllerBase discoverInfo) + { + const string q = "SELECT discover_link FROM Users WHERE user_id = @id"; + var i = DB.ExecuteSelect(q, GlobalVariables.DbConfigVar, new Dictionary() + { + { "@id", tempSub } + }); + if (i == null) + return discoverInfo.NotFound(); + + var value = i.Rows[0].ItemArray[0]?.ToString(); + return discoverInfo.Ok(new JObject() { { "link", value } }); + } + + private static ActionResult SetLink(string tempSub, string stringLink, ControllerBase discoverInfo) + { + const string q = "UPDATE Users SET discover_link = @link WHERE user_id = @id"; + var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary() + { + { "@id", tempSub }, + { "@link", stringLink } + }); + return discoverInfo.Ok(i); + } +} \ No newline at end of file diff --git a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs new file mode 100644 index 00000000..cddf83e5 --- /dev/null +++ b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs @@ -0,0 +1,89 @@ +using System.Data; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; +using PoliFemoBackend.Source.Data; +using PoliFemoBackend.Source.Utils.Auth; +using DB = PoliNetwork.Db.Utils.Database; + + +namespace PoliFemoBackend.Source.Controllers.DiscoverPeople; + +[ApiController] +[ApiExplorerSettings(GroupName = "DiscoverPeople")] +public class MatchController : ControllerBase +{ + [HttpPost] + [Authorize] + [Route("/discoverpeople/match/setYes/{id}")] + [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] + public ActionResult SetAnswerMatchYes(string id) + { + var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); + return string.IsNullOrEmpty(tempSub) ? new EmptyResult() : SetAnswerMatch(tempSub, id, true, this); + } + + + [HttpGet] + [Authorize] + [Route("/discoverpeople/match/get")] + [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] + public ActionResult GetMatched() + { + var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); + if (string.IsNullOrEmpty(tempSub)) + return new EmptyResult(); + var answerMatchYes = GetMatched(tempSub); + return Ok(answerMatchYes); + } + + [HttpPost] + [Authorize] + [Route("/discoverpeople/match/setNo/{id}")] + [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] + public ActionResult SetAnswerMatchNo(string id) + { + var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); + return string.IsNullOrEmpty(tempSub) ? new EmptyResult() : SetAnswerMatch(tempSub, id, false, this); + } + + private static ActionResult SetAnswerMatch(string fromUser, string toUser, bool yesOrNo, + ControllerBase discoverPeopleController) + { + const string q = "INSERT IGNORE INTO PeopleDiscoverMatch (from_person, to_person, answer) VALUES (@p1,@p2,@a)"; + var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary() + { + { "@p1", fromUser }, + { "@p2", toUser }, + { "@a", yesOrNo } + }); + return discoverPeopleController.Ok(new JObject() { { "r", i } }); + } + + + private static JArray? GetMatched(string tempSub) + { + const string q = "SELECT user_id, discover_bio, discover_link " + + "FROM Users u " + + "WHERE u.user_id IN (SELECT p1.to_person FROM PeopleDiscoverMatch p1 WHERE p1.from_person = @id AND p1.answer = TRUE AND p1.to_person IN (" + + "SELECT p2.from_person FROM PeopleDiscoverMatch p2 WHERE p2.from_person = p1.to_person AND p2.to_person = @id AND p2.answer = TRUE" + + "))"; + var dictionary = new Dictionary + { + { "@id", tempSub } + }; + var results = DB.ExecuteSelect(q, GlobalVariables.DbConfigVar, dictionary); + + if (results == null) + return null; + + var jArray = new JArray(); + foreach (DataRow variable in results.Rows) + { + var j = UserUtil.GetUser(variable); + jArray.Add(j); + } + return jArray; + + } +} \ No newline at end of file diff --git a/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs b/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs new file mode 100644 index 00000000..4106251d --- /dev/null +++ b/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs @@ -0,0 +1,18 @@ +using System.Data; +using Newtonsoft.Json.Linq; + +namespace PoliFemoBackend.Source.Controllers.DiscoverPeople; + +public class UserUtil +{ + public static JObject GetUser(DataRow row) + { + var r = new JObject + { + ["user_id"] = row.ItemArray[0]?.ToString(), + ["discover_bio"] = row.ItemArray[1]?.ToString(), + ["discover_link"] = row.ItemArray.Length > 2 ? row.ItemArray[2]?.ToString() : null + }; + return r; + } +} \ No newline at end of file From 695ebb8c9dabe79368e4ecb3712757a21e8d2051 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 13:59:06 +0200 Subject: [PATCH 04/17] Update DiscoverPeopleController.cs, DiscoverInfoBio.cs, DiscoverInfoLink.cs, and 3 more files --- .../DiscoverPeople/DiscoverPeopleController.cs | 7 ++----- .../DiscoverPeople/Info/DiscoverInfoBio.cs | 10 +++++++--- .../DiscoverPeople/Info/DiscoverInfoLink.cs | 10 +++++++--- .../Controllers/DiscoverPeople/MatchController.cs | 12 ++++++++---- .../Source/Controllers/DiscoverPeople/UserUtil.cs | 4 ++++ Backend/Source/Utils/Rooms/SingleRoomUtil.cs | 6 +++--- 6 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs b/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs index f417882f..20f5232e 100644 --- a/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs +++ b/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs @@ -1,6 +1,5 @@ #region -using System.Data; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; @@ -32,11 +31,11 @@ public ActionResult GetRandomPeople() var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); if (string.IsNullOrEmpty(tempSub)) return new EmptyResult(); - + var a = NewPeople(tempSub); return a == null ? NotFound() : Ok(a); } - + private static JObject? NewPeople(string tempSub) { @@ -53,6 +52,4 @@ public ActionResult GetRandomPeople() var row = results?.Rows[0]; return row == null ? null : UserUtil.GetUser(row); } - - } \ No newline at end of file diff --git a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs index d0713f62..3072069c 100644 --- a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs +++ b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs @@ -1,3 +1,5 @@ +#region + using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; @@ -5,6 +7,8 @@ using PoliFemoBackend.Source.Utils.Auth; using DB = PoliNetwork.Db.Utils.Database; +#endregion + namespace PoliFemoBackend.Source.Controllers.DiscoverPeople.Info; public class DiscoverInfoBio : ControllerBase @@ -32,7 +36,7 @@ public ActionResult GetBio() private static ActionResult GetBioUtil(string tempSub, ControllerBase discoverInfo) { const string q = "SELECT discover_bio FROM Users WHERE user_id = @id"; - var i = DB.ExecuteSelect(q, GlobalVariables.DbConfigVar, new Dictionary() + var i = DB.ExecuteSelect(q, GlobalVariables.DbConfigVar, new Dictionary { { "@id", tempSub } }); @@ -40,13 +44,13 @@ private static ActionResult GetBioUtil(string tempSub, ControllerBase discoverIn return discoverInfo.NotFound(); var value = i.Rows[0].ItemArray[0]?.ToString(); - return discoverInfo.Ok(new JObject() { { "bio", value } }); + return discoverInfo.Ok(new JObject { { "bio", value } }); } private static ActionResult SetBio(string tempSub, string stringBio, ControllerBase discoverInfo) { const string q = "UPDATE Users SET discover_bio = @bio WHERE user_id = @id"; - var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary() + var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary { { "@id", tempSub }, { "@bio", stringBio } diff --git a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs index 08c404e2..5f07080f 100644 --- a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs +++ b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs @@ -1,3 +1,5 @@ +#region + using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; @@ -5,6 +7,8 @@ using PoliFemoBackend.Source.Utils.Auth; using DB = PoliNetwork.Db.Utils.Database; +#endregion + namespace PoliFemoBackend.Source.Controllers.DiscoverPeople.Info; public class DiscoverInfoLink : ControllerBase @@ -32,7 +36,7 @@ public ActionResult GetLink() private static ActionResult GetLinkUtil(string tempSub, ControllerBase discoverInfo) { const string q = "SELECT discover_link FROM Users WHERE user_id = @id"; - var i = DB.ExecuteSelect(q, GlobalVariables.DbConfigVar, new Dictionary() + var i = DB.ExecuteSelect(q, GlobalVariables.DbConfigVar, new Dictionary { { "@id", tempSub } }); @@ -40,13 +44,13 @@ private static ActionResult GetLinkUtil(string tempSub, ControllerBase discoverI return discoverInfo.NotFound(); var value = i.Rows[0].ItemArray[0]?.ToString(); - return discoverInfo.Ok(new JObject() { { "link", value } }); + return discoverInfo.Ok(new JObject { { "link", value } }); } private static ActionResult SetLink(string tempSub, string stringLink, ControllerBase discoverInfo) { const string q = "UPDATE Users SET discover_link = @link WHERE user_id = @id"; - var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary() + var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary { { "@id", tempSub }, { "@link", stringLink } diff --git a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs index cddf83e5..90d6041d 100644 --- a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs +++ b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs @@ -1,3 +1,5 @@ +#region + using System.Data; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -6,6 +8,8 @@ using PoliFemoBackend.Source.Utils.Auth; using DB = PoliNetwork.Db.Utils.Database; +#endregion + namespace PoliFemoBackend.Source.Controllers.DiscoverPeople; @@ -51,13 +55,13 @@ private static ActionResult SetAnswerMatch(string fromUser, string toUser, bool ControllerBase discoverPeopleController) { const string q = "INSERT IGNORE INTO PeopleDiscoverMatch (from_person, to_person, answer) VALUES (@p1,@p2,@a)"; - var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary() + var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary { { "@p1", fromUser }, { "@p2", toUser }, { "@a", yesOrNo } }); - return discoverPeopleController.Ok(new JObject() { { "r", i } }); + return discoverPeopleController.Ok(new JObject { { "r", i } }); } @@ -76,14 +80,14 @@ private static ActionResult SetAnswerMatch(string fromUser, string toUser, bool if (results == null) return null; - + var jArray = new JArray(); foreach (DataRow variable in results.Rows) { var j = UserUtil.GetUser(variable); jArray.Add(j); } - return jArray; + return jArray; } } \ No newline at end of file diff --git a/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs b/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs index 4106251d..3024fa39 100644 --- a/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs +++ b/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs @@ -1,6 +1,10 @@ +#region + using System.Data; using Newtonsoft.Json.Linq; +#endregion + namespace PoliFemoBackend.Source.Controllers.DiscoverPeople; public class UserUtil diff --git a/Backend/Source/Utils/Rooms/SingleRoomUtil.cs b/Backend/Source/Utils/Rooms/SingleRoomUtil.cs index a70e0df4..2d879d2f 100644 --- a/Backend/Source/Utils/Rooms/SingleRoomUtil.cs +++ b/Backend/Source/Utils/Rooms/SingleRoomUtil.cs @@ -21,9 +21,9 @@ public static class SingleRoomUtil /* example of property tag - Codice vano -
 LCF040800S042 - + Codice vano +
 LCF040800S042 + (parsing doesn't work very well, regex++) */ var fetchedHtml = html.GetData() ?? ""; From 955739da2ca3bee3f60c2b4b440843e0aace9d27 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 14:09:53 +0200 Subject: [PATCH 05/17] Update DiscoverPeopleController.cs --- .../Controllers/DiscoverPeople/DiscoverPeopleController.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs b/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs index 20f5232e..de669b80 100644 --- a/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs +++ b/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs @@ -42,7 +42,12 @@ public ActionResult GetRandomPeople() var results = DB.ExecuteSelect( "SELECT user_id, discover_bio " + "FROM Users " + - "WHERE user_id NOT IN (SELECT to_person PeopleDiscoverMatch WHERE from_person = @id) ORDER BY RAND() LIMIT 10", + "WHERE user_id NOT IN (SELECT to_person PeopleDiscoverMatch WHERE from_person = @id) " + + "AND discover_bio IS NOT NULL " + + "AND discover_bio != '' " + + "AND discover_link IS NOT NULL " + + "AND discover_link != '' " + + "ORDER BY RAND() LIMIT 10", GlobalVariables.DbConfigVar, new Dictionary { From 6be7817b5723950b877fa9a60d7ad3ab85250aa7 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 14:46:51 +0200 Subject: [PATCH 06/17] Update DiscoverInfoLink.cs --- .../Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs index 5f07080f..d37eec17 100644 --- a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs +++ b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs @@ -49,6 +49,9 @@ private static ActionResult GetLinkUtil(string tempSub, ControllerBase discoverI private static ActionResult SetLink(string tempSub, string stringLink, ControllerBase discoverInfo) { + if (IsValidHttpOrHttpsLink(stringLink) == false) + return discoverInfo.BadRequest(); + const string q = "UPDATE Users SET discover_link = @link WHERE user_id = @id"; var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary { @@ -57,4 +60,10 @@ private static ActionResult SetLink(string tempSub, string stringLink, Controlle }); return discoverInfo.Ok(i); } + + static bool IsValidHttpOrHttpsLink(string input) + { + return Uri.TryCreate(input, UriKind.Absolute, out Uri? result) && + (result.Scheme == Uri.UriSchemeHttp || result.Scheme == Uri.UriSchemeHttps); + } } \ No newline at end of file From c2e86869d986e0624c297b456e2364433dc95429 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 14:53:54 +0200 Subject: [PATCH 07/17] Update AccountExport.cs --- .../Controllers/Accounts/AccountExport.cs | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Backend/Source/Controllers/Accounts/AccountExport.cs b/Backend/Source/Controllers/Accounts/AccountExport.cs index 9755eaed..dd94915c 100644 --- a/Backend/Source/Controllers/Accounts/AccountExport.cs +++ b/Backend/Source/Controllers/Accounts/AccountExport.cs @@ -33,7 +33,7 @@ public FileContentResult ExportData() var sub = AuthUtil.GetSubjectFromHttpRequest(Request); var query = - "SELECT user_id, last_activity, account_type, expires_days FROM Users WHERE user_id = SHA2(@sub, 256)"; + "SELECT user_id, last_activity, account_type, expires_days, discover_bio, discover_link FROM Users WHERE user_id = SHA2(@sub, 256)"; var parameters = new Dictionary { { "@sub", sub } @@ -44,12 +44,18 @@ public FileContentResult ExportData() var accountType = q?.Rows[0]["account_type"]?.ToString() ?? ""; var expiresDays = int.Parse(q?.Rows[0]["expires_days"]?.ToString() ?? "0"); + JObject other = new JObject + { + ["discover_bio"] = q?.Rows[0]["discover_bio"]?.ToString(), + ["discover_link"] = q?.Rows[0]["discover_link"]?.ToString() + }; + query = "SELECT * FROM RoomOccupancyReports WHERE user_id = SHA2(@sub, 256)"; q = DB.ExecuteSelect(query, GlobalVariables.DbConfigVar, parameters); var occupancyReports = q?.Rows; var roc = new JArray(); if (occupancyReports == null) - return FileExport(id, lastActivity, accountType, expiresDays, sub, roc); + return FileExport(id, lastActivity, accountType, expiresDays, sub, roc, other); foreach (DataRow row in occupancyReports) roc.Add(JObject.FromObject(new @@ -58,20 +64,23 @@ public FileContentResult ExportData() when_reported = row["when_reported"], rate = row["rate"] })); - return FileExport(id, lastActivity, accountType, expiresDays, sub, roc); + return FileExport(id, lastActivity, accountType, expiresDays, sub, roc, other); } - private FileContentResult FileExport(string id, DateTime lastActivity, string accountType, int edays, string? sub, - JArray roc) + private FileContentResult FileExport(string id, DateTime lastActivity, string accountType, int eDays, string? sub, + JArray roc, JObject other) { - return File(Encoding.UTF8.GetBytes(JObject.FromObject(new + var fromObject = JObject.FromObject(new { id, last_activity = lastActivity.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture), account_type = accountType, - expires_days = edays, + expires_days = eDays, permissions = Grant.GetFormattedPerms(AccountAuthUtil.GetPermissions(sub)), room_occupancy_reports = roc - }).ToString()), "application/json", id + ".json"); + }); + var fileContents = Encoding.UTF8.GetBytes(fromObject.ToString()); + var fileDownloadName = id + ".json"; + return File(fileContents, "application/json", fileDownloadName); } } \ No newline at end of file From dee6166f9a0914d46ed793bdee8a53ddf6510443 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 15:01:03 +0200 Subject: [PATCH 08/17] Update AccountController.cs, DiscoverPeopleController.cs, DiscoverInfoBio.cs, and 2 more files --- .../Controllers/Accounts/AccountController.cs | 31 ++++++++++--------- .../DiscoverPeopleController.cs | 2 +- .../DiscoverPeople/Info/DiscoverInfoBio.cs | 4 +-- .../DiscoverPeople/Info/DiscoverInfoLink.cs | 4 +-- .../DiscoverPeople/MatchController.cs | 6 ++-- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/Backend/Source/Controllers/Accounts/AccountController.cs b/Backend/Source/Controllers/Accounts/AccountController.cs index 855af88f..4da2eca2 100644 --- a/Backend/Source/Controllers/Accounts/AccountController.cs +++ b/Backend/Source/Controllers/Accounts/AccountController.cs @@ -29,30 +29,33 @@ public class ArticleByIdController : ControllerBase /// Can't connect to the server [HttpGet] [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] - public ObjectResult ProfileDetails() + public ObjectResult? ProfileDetails() { - string userid; var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); - var sub = tempSub ?? ""; - var permissions = AccountAuthUtil.GetPermissions(sub); - using (var sha256Hash = SHA256.Create()) - { - //From String to byte array - var sourceBytes = Encoding.UTF8.GetBytes(sub); - var hashBytes = sha256Hash.ComputeHash(sourceBytes); - userid = BitConverter.ToString(hashBytes).Replace("-", string.Empty); - } + if (string.IsNullOrEmpty(tempSub)) + return null; + + var userid = GetUserId(tempSub); - var permarray = Grant.GetFormattedPerms(permissions); + var permissions = AccountAuthUtil.GetPermissions(tempSub); + var permArray = Grant.GetFormattedPerms(permissions); return new ObjectResult(new { id = userid.ToLower(), - permissions = permarray, - authorized_authors = AccountAuthoursAuthUtil.GetAuthorizedAuthors(sub) + permissions = permArray, + authorized_authors = AccountAuthoursAuthUtil.GetAuthorizedAuthors(tempSub) }); } + public string GetUserId(string sub) + { + var sourceBytes = Encoding.UTF8.GetBytes(sub); + var hashBytes = SHA256.HashData(sourceBytes); + var userid = BitConverter.ToString(hashBytes).Replace("-", string.Empty); + return userid; + } + /// /// Delete the user's account and data diff --git a/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs b/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs index de669b80..87e76489 100644 --- a/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs +++ b/Backend/Source/Controllers/DiscoverPeople/DiscoverPeopleController.cs @@ -42,7 +42,7 @@ public ActionResult GetRandomPeople() var results = DB.ExecuteSelect( "SELECT user_id, discover_bio " + "FROM Users " + - "WHERE user_id NOT IN (SELECT to_person PeopleDiscoverMatch WHERE from_person = @id) " + + "WHERE user_id NOT IN (SELECT to_person PeopleDiscoverMatch WHERE from_person = SHA2(@id,256)) " + "AND discover_bio IS NOT NULL " + "AND discover_bio != '' " + "AND discover_link IS NOT NULL " + diff --git a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs index 3072069c..ea9b5269 100644 --- a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs +++ b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs @@ -35,7 +35,7 @@ public ActionResult GetBio() private static ActionResult GetBioUtil(string tempSub, ControllerBase discoverInfo) { - const string q = "SELECT discover_bio FROM Users WHERE user_id = @id"; + const string q = "SELECT discover_bio FROM Users WHERE user_id = SHA2(@id,256)"; var i = DB.ExecuteSelect(q, GlobalVariables.DbConfigVar, new Dictionary { { "@id", tempSub } @@ -49,7 +49,7 @@ private static ActionResult GetBioUtil(string tempSub, ControllerBase discoverIn private static ActionResult SetBio(string tempSub, string stringBio, ControllerBase discoverInfo) { - const string q = "UPDATE Users SET discover_bio = @bio WHERE user_id = @id"; + const string q = "UPDATE Users SET discover_bio = @bio WHERE user_id = SHA2(@id,256)"; var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary { { "@id", tempSub }, diff --git a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs index d37eec17..268869d7 100644 --- a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs +++ b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs @@ -35,7 +35,7 @@ public ActionResult GetLink() private static ActionResult GetLinkUtil(string tempSub, ControllerBase discoverInfo) { - const string q = "SELECT discover_link FROM Users WHERE user_id = @id"; + const string q = "SELECT discover_link FROM Users WHERE user_id = SHA2(@id,256)"; var i = DB.ExecuteSelect(q, GlobalVariables.DbConfigVar, new Dictionary { { "@id", tempSub } @@ -52,7 +52,7 @@ private static ActionResult SetLink(string tempSub, string stringLink, Controlle if (IsValidHttpOrHttpsLink(stringLink) == false) return discoverInfo.BadRequest(); - const string q = "UPDATE Users SET discover_link = @link WHERE user_id = @id"; + const string q = "UPDATE Users SET discover_link = @link WHERE user_id = SHA2(@id,256)"; var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary { { "@id", tempSub }, diff --git a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs index 90d6041d..fb6f66c3 100644 --- a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs +++ b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs @@ -54,7 +54,7 @@ public ActionResult SetAnswerMatchNo(string id) private static ActionResult SetAnswerMatch(string fromUser, string toUser, bool yesOrNo, ControllerBase discoverPeopleController) { - const string q = "INSERT IGNORE INTO PeopleDiscoverMatch (from_person, to_person, answer) VALUES (@p1,@p2,@a)"; + const string q = "INSERT IGNORE INTO PeopleDiscoverMatch (from_person, to_person, answer) VALUES (SHA2(@p1,256),SHA2(@p2,256),@a)"; var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary { { "@p1", fromUser }, @@ -69,8 +69,8 @@ private static ActionResult SetAnswerMatch(string fromUser, string toUser, bool { const string q = "SELECT user_id, discover_bio, discover_link " + "FROM Users u " + - "WHERE u.user_id IN (SELECT p1.to_person FROM PeopleDiscoverMatch p1 WHERE p1.from_person = @id AND p1.answer = TRUE AND p1.to_person IN (" + - "SELECT p2.from_person FROM PeopleDiscoverMatch p2 WHERE p2.from_person = p1.to_person AND p2.to_person = @id AND p2.answer = TRUE" + + "WHERE u.user_id IN (SELECT p1.to_person FROM PeopleDiscoverMatch p1 WHERE p1.from_person = SHA2(@id,256) AND p1.answer = TRUE AND p1.to_person IN (" + + "SELECT p2.from_person FROM PeopleDiscoverMatch p2 WHERE p2.from_person = p1.to_person AND p2.to_person = SHA2(@id,256) AND p2.answer = TRUE" + "))"; var dictionary = new Dictionary { From f0eb7b63b145178b1f6b559686a55c3d67f72f46 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 15:01:39 +0200 Subject: [PATCH 09/17] Update AccountExport.cs, DiscoverInfoLink.cs, and MatchController.cs --- Backend/Source/Controllers/Accounts/AccountExport.cs | 2 +- .../Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs | 8 ++++---- .../Source/Controllers/DiscoverPeople/MatchController.cs | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Backend/Source/Controllers/Accounts/AccountExport.cs b/Backend/Source/Controllers/Accounts/AccountExport.cs index dd94915c..708935d2 100644 --- a/Backend/Source/Controllers/Accounts/AccountExport.cs +++ b/Backend/Source/Controllers/Accounts/AccountExport.cs @@ -44,7 +44,7 @@ public FileContentResult ExportData() var accountType = q?.Rows[0]["account_type"]?.ToString() ?? ""; var expiresDays = int.Parse(q?.Rows[0]["expires_days"]?.ToString() ?? "0"); - JObject other = new JObject + var other = new JObject { ["discover_bio"] = q?.Rows[0]["discover_bio"]?.ToString(), ["discover_link"] = q?.Rows[0]["discover_link"]?.ToString() diff --git a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs index 268869d7..2fde5d91 100644 --- a/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs +++ b/Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs @@ -51,7 +51,7 @@ private static ActionResult SetLink(string tempSub, string stringLink, Controlle { if (IsValidHttpOrHttpsLink(stringLink) == false) return discoverInfo.BadRequest(); - + const string q = "UPDATE Users SET discover_link = @link WHERE user_id = SHA2(@id,256)"; var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary { @@ -60,10 +60,10 @@ private static ActionResult SetLink(string tempSub, string stringLink, Controlle }); return discoverInfo.Ok(i); } - - static bool IsValidHttpOrHttpsLink(string input) + + private static bool IsValidHttpOrHttpsLink(string input) { - return Uri.TryCreate(input, UriKind.Absolute, out Uri? result) && + return Uri.TryCreate(input, UriKind.Absolute, out var result) && (result.Scheme == Uri.UriSchemeHttp || result.Scheme == Uri.UriSchemeHttps); } } \ No newline at end of file diff --git a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs index fb6f66c3..b9025ce1 100644 --- a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs +++ b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs @@ -54,7 +54,8 @@ public ActionResult SetAnswerMatchNo(string id) private static ActionResult SetAnswerMatch(string fromUser, string toUser, bool yesOrNo, ControllerBase discoverPeopleController) { - const string q = "INSERT IGNORE INTO PeopleDiscoverMatch (from_person, to_person, answer) VALUES (SHA2(@p1,256),SHA2(@p2,256),@a)"; + const string q = + "INSERT IGNORE INTO PeopleDiscoverMatch (from_person, to_person, answer) VALUES (SHA2(@p1,256),SHA2(@p2,256),@a)"; var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary { { "@p1", fromUser }, From 29c9bb58932e0756a6c57c1b727e849fbfb36bf2 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 15:04:06 +0200 Subject: [PATCH 10/17] Update AccountController.cs and Test.cs --- Backend/Source/Controllers/Accounts/AccountController.cs | 7 ++++++- Backend/Source/Test/Test.cs | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Backend/Source/Controllers/Accounts/AccountController.cs b/Backend/Source/Controllers/Accounts/AccountController.cs index 4da2eca2..75beaba3 100644 --- a/Backend/Source/Controllers/Accounts/AccountController.cs +++ b/Backend/Source/Controllers/Accounts/AccountController.cs @@ -36,6 +36,8 @@ public class ArticleByIdController : ControllerBase return null; var userid = GetUserId(tempSub); + if (string.IsNullOrEmpty(userid)) + return null; var permissions = AccountAuthUtil.GetPermissions(tempSub); var permArray = Grant.GetFormattedPerms(permissions); @@ -48,8 +50,11 @@ public class ArticleByIdController : ControllerBase }); } - public string GetUserId(string sub) + public string? GetUserId(string sub) { + if (string.IsNullOrEmpty(sub)) + return null; + var sourceBytes = Encoding.UTF8.GetBytes(sub); var hashBytes = SHA256.HashData(sourceBytes); var userid = BitConverter.ToString(hashBytes).Replace("-", string.Empty); diff --git a/Backend/Source/Test/Test.cs b/Backend/Source/Test/Test.cs index 0efc591f..3695031c 100644 --- a/Backend/Source/Test/Test.cs +++ b/Backend/Source/Test/Test.cs @@ -22,11 +22,11 @@ internal static void RunTest() } } - private static async Task TestMain() + private static Task TestMain() { Console.WriteLine("Test"); - //FixGlobalDbConfig(); + FixGlobalDbConfig(); try { @@ -40,6 +40,8 @@ private static async Task TestMain() throw; } + return Task.CompletedTask; + //DbConfig.InitializeDbConfig(); //ArticleContentUpgrade.ArticleContentUpgradeMethod(); From 2f49a5be7761fc98a2fa084dd4ac15aa15c62161 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 15:25:00 +0200 Subject: [PATCH 11/17] Update AccountExport.cs --- .../Source/Controllers/Accounts/AccountExport.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Backend/Source/Controllers/Accounts/AccountExport.cs b/Backend/Source/Controllers/Accounts/AccountExport.cs index 708935d2..e9a768ca 100644 --- a/Backend/Source/Controllers/Accounts/AccountExport.cs +++ b/Backend/Source/Controllers/Accounts/AccountExport.cs @@ -39,15 +39,15 @@ public FileContentResult ExportData() { "@sub", sub } }; var q = DB.ExecuteSelect(query, GlobalVariables.DbConfigVar, parameters); - var lastActivity = DateTime.Parse(q?.Rows[0]["last_activity"]?.ToString() ?? ""); - var id = q?.Rows[0]["user_id"]?.ToString() ?? ""; - var accountType = q?.Rows[0]["account_type"]?.ToString() ?? ""; - var expiresDays = int.Parse(q?.Rows[0]["expires_days"]?.ToString() ?? "0"); + var lastActivity = DateTime.Parse(q?.Rows[0]["last_activity"].ToString() ?? ""); + var id = q?.Rows[0]["user_id"].ToString() ?? ""; + var accountType = q?.Rows[0]["account_type"].ToString() ?? ""; + var expiresDays = int.Parse(q?.Rows[0]["expires_days"].ToString() ?? "0"); var other = new JObject { - ["discover_bio"] = q?.Rows[0]["discover_bio"]?.ToString(), - ["discover_link"] = q?.Rows[0]["discover_link"]?.ToString() + ["discover_bio"] = q?.Rows[0]["discover_bio"].ToString(), + ["discover_link"] = q?.Rows[0]["discover_link"].ToString() }; query = "SELECT * FROM RoomOccupancyReports WHERE user_id = SHA2(@sub, 256)"; @@ -77,7 +77,8 @@ private FileContentResult FileExport(string id, DateTime lastActivity, string ac account_type = accountType, expires_days = eDays, permissions = Grant.GetFormattedPerms(AccountAuthUtil.GetPermissions(sub)), - room_occupancy_reports = roc + room_occupancy_reports = roc, + other }); var fileContents = Encoding.UTF8.GetBytes(fromObject.ToString()); var fileDownloadName = id + ".json"; From 92e30b75454d987f5bcb0b914d61b609d4352f8f Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 16:42:18 +0200 Subject: [PATCH 12/17] Update MatchController.cs and UserUtil.cs --- .../DiscoverPeople/MatchController.cs | 66 +++++++++++++++++-- .../Controllers/DiscoverPeople/UserUtil.cs | 6 +- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs index b9025ce1..e3354542 100644 --- a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs +++ b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs @@ -1,6 +1,8 @@ #region using System.Data; +using System.Security.Cryptography; +using System.Text; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; @@ -54,25 +56,75 @@ public ActionResult SetAnswerMatchNo(string id) private static ActionResult SetAnswerMatch(string fromUser, string toUser, bool yesOrNo, ControllerBase discoverPeopleController) { + var sRandom = GenerateRandomHash(20); const string q = - "INSERT IGNORE INTO PeopleDiscoverMatch (from_person, to_person, answer) VALUES (SHA2(@p1,256),SHA2(@p2,256),@a)"; + "INSERT IGNORE INTO PeopleDiscoverMatch " + + "(from_person, to_person, answer, mn, ms) " + + "VALUES " + + "(SHA2(@p1,256)," + + "SHA2(@p2,256)," + + "@a," + + "(SELECT COALESCE(MAX(ms), 0) + 1 FROM PeopleDiscoverMatch), " + + "@ms" + + ")"; + var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary { { "@p1", fromUser }, { "@p2", toUser }, - { "@a", yesOrNo } + { "@a", yesOrNo }, + {"@mn", 0}, + {"@ms", sRandom} }); return discoverPeopleController.Ok(new JObject { { "r", i } }); } + static string GenerateRandomHash(int length) + { + var randomBytes = new byte[length]; + using (var rng = RandomNumberGenerator.Create()) + { + rng.GetBytes(randomBytes); + } + + var hashBytes = SHA256.HashData(randomBytes); + + var hashStringBuilder = new StringBuilder(); + foreach (var b in hashBytes) + { + hashStringBuilder.Append(b.ToString("x2")); + } + + return hashStringBuilder.ToString()[..length]; + } private static JArray? GetMatched(string tempSub) { - const string q = "SELECT user_id, discover_bio, discover_link " + - "FROM Users u " + - "WHERE u.user_id IN (SELECT p1.to_person FROM PeopleDiscoverMatch p1 WHERE p1.from_person = SHA2(@id,256) AND p1.answer = TRUE AND p1.to_person IN (" + - "SELECT p2.from_person FROM PeopleDiscoverMatch p2 WHERE p2.from_person = p1.to_person AND p2.to_person = SHA2(@id,256) AND p2.answer = TRUE" + - "))"; + /* + tu = {tempSub} (@id) + + u.id = altro + p1.from = tu + p1.to = altro + p2.from = altro + p1.to = tu + + u.id = p1.to + p1.to = p2.from + p1.from = @id + p1.from = p2.to + + */ + const string q = "SELECT u.user_id, u.discover_bio, u.discover_link, p1.mn as mn1, p1.ms as ms1, p2.mn as mn2, p2.ms as ms2 " + + "FROM Users u, PeopleDiscoverMatch p1, PeopleDiscoverMatch p2 " + + "WHERE u.user_id = p1.to_person " + + "AND p1.from_person = SHA2(@id,256) " + + "AND p1.answer = TRUE " + + "AND p1.to_person = p2.from_person " + + "AND p2.from_person = p1.to_person " + + "AND p2.to_person = SHA2(@id,256) " + + "AND p2.answer = TRUE"; + var dictionary = new Dictionary { { "@id", tempSub } diff --git a/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs b/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs index 3024fa39..dd11b0fa 100644 --- a/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs +++ b/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs @@ -15,7 +15,11 @@ public static JObject GetUser(DataRow row) { ["user_id"] = row.ItemArray[0]?.ToString(), ["discover_bio"] = row.ItemArray[1]?.ToString(), - ["discover_link"] = row.ItemArray.Length > 2 ? row.ItemArray[2]?.ToString() : null + ["discover_link"] = row.ItemArray.Length > 2 ? row.ItemArray[2]?.ToString() : null, + ["mn1"] = row.ItemArray.Length > 3 ? row.ItemArray[3]?.ToString() : null, + ["ms1"] = row.ItemArray.Length > 4 ? row.ItemArray[4]?.ToString() : null, + ["mn2"] = row.ItemArray.Length > 5 ? row.ItemArray[5]?.ToString() : null, + ["ms2"] = row.ItemArray.Length > 6 ? row.ItemArray[6]?.ToString() : null, }; return r; } From ab4a3f1f5a60beb269a7c4c85609dc858b58100b Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 16:43:14 +0200 Subject: [PATCH 13/17] Update MatchController.cs and UserUtil.cs --- .../DiscoverPeople/MatchController.cs | 35 +++++++++---------- .../Controllers/DiscoverPeople/UserUtil.cs | 8 ++--- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs index e3354542..815faa15 100644 --- a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs +++ b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs @@ -67,19 +67,18 @@ private static ActionResult SetAnswerMatch(string fromUser, string toUser, bool "(SELECT COALESCE(MAX(ms), 0) + 1 FROM PeopleDiscoverMatch), " + "@ms" + ")"; - + var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary { { "@p1", fromUser }, { "@p2", toUser }, { "@a", yesOrNo }, - {"@mn", 0}, - {"@ms", sRandom} + { "@ms", sRandom } }); return discoverPeopleController.Ok(new JObject { { "r", i } }); } - static string GenerateRandomHash(int length) + private static string GenerateRandomHash(int length) { var randomBytes = new byte[length]; using (var rng = RandomNumberGenerator.Create()) @@ -90,10 +89,7 @@ static string GenerateRandomHash(int length) var hashBytes = SHA256.HashData(randomBytes); var hashStringBuilder = new StringBuilder(); - foreach (var b in hashBytes) - { - hashStringBuilder.Append(b.ToString("x2")); - } + foreach (var b in hashBytes) hashStringBuilder.Append(b.ToString("x2")); return hashStringBuilder.ToString()[..length]; } @@ -102,7 +98,7 @@ static string GenerateRandomHash(int length) { /* tu = {tempSub} (@id) - + u.id = altro p1.from = tu p1.to = altro @@ -115,16 +111,17 @@ static string GenerateRandomHash(int length) p1.from = p2.to */ - const string q = "SELECT u.user_id, u.discover_bio, u.discover_link, p1.mn as mn1, p1.ms as ms1, p2.mn as mn2, p2.ms as ms2 " + - "FROM Users u, PeopleDiscoverMatch p1, PeopleDiscoverMatch p2 " + - "WHERE u.user_id = p1.to_person " + - "AND p1.from_person = SHA2(@id,256) " + - "AND p1.answer = TRUE " + - "AND p1.to_person = p2.from_person " + - "AND p2.from_person = p1.to_person " + - "AND p2.to_person = SHA2(@id,256) " + - "AND p2.answer = TRUE"; - + const string q = + "SELECT u.user_id, u.discover_bio, u.discover_link, p1.mn as mn1, p1.ms as ms1, p2.mn as mn2, p2.ms as ms2 " + + "FROM Users u, PeopleDiscoverMatch p1, PeopleDiscoverMatch p2 " + + "WHERE u.user_id = p1.to_person " + + "AND p1.from_person = SHA2(@id,256) " + + "AND p1.answer = TRUE " + + "AND p1.to_person = p2.from_person " + + "AND p2.from_person = p1.to_person " + + "AND p2.to_person = SHA2(@id,256) " + + "AND p2.answer = TRUE"; + var dictionary = new Dictionary { { "@id", tempSub } diff --git a/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs b/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs index dd11b0fa..a4ef4886 100644 --- a/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs +++ b/Backend/Source/Controllers/DiscoverPeople/UserUtil.cs @@ -16,10 +16,10 @@ public static JObject GetUser(DataRow row) ["user_id"] = row.ItemArray[0]?.ToString(), ["discover_bio"] = row.ItemArray[1]?.ToString(), ["discover_link"] = row.ItemArray.Length > 2 ? row.ItemArray[2]?.ToString() : null, - ["mn1"] = row.ItemArray.Length > 3 ? row.ItemArray[3]?.ToString() : null, - ["ms1"] = row.ItemArray.Length > 4 ? row.ItemArray[4]?.ToString() : null, - ["mn2"] = row.ItemArray.Length > 5 ? row.ItemArray[5]?.ToString() : null, - ["ms2"] = row.ItemArray.Length > 6 ? row.ItemArray[6]?.ToString() : null, + ["mn1"] = row.ItemArray.Length > 3 ? row.ItemArray[3]?.ToString() : null, + ["ms1"] = row.ItemArray.Length > 4 ? row.ItemArray[4]?.ToString() : null, + ["mn2"] = row.ItemArray.Length > 5 ? row.ItemArray[5]?.ToString() : null, + ["ms2"] = row.ItemArray.Length > 6 ? row.ItemArray[6]?.ToString() : null }; return r; } From c7b61809b711ce15f915647021303b063ed9b0f6 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 16:46:06 +0200 Subject: [PATCH 14/17] Update MatchController.cs and MatchUtil.cs --- .../DiscoverPeople/MatchController.cs | 46 +-------------- .../Controllers/DiscoverPeople/MatchUtil.cs | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 44 deletions(-) create mode 100644 Backend/Source/Controllers/DiscoverPeople/MatchUtil.cs diff --git a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs index 815faa15..82f55d3d 100644 --- a/Backend/Source/Controllers/DiscoverPeople/MatchController.cs +++ b/Backend/Source/Controllers/DiscoverPeople/MatchController.cs @@ -1,8 +1,6 @@ #region using System.Data; -using System.Security.Cryptography; -using System.Text; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; @@ -26,7 +24,7 @@ public class MatchController : ControllerBase public ActionResult SetAnswerMatchYes(string id) { var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); - return string.IsNullOrEmpty(tempSub) ? new EmptyResult() : SetAnswerMatch(tempSub, id, true, this); + return string.IsNullOrEmpty(tempSub) ? new EmptyResult() : MatchUtil.SetAnswerMatch(tempSub, id, true, this); } @@ -50,49 +48,9 @@ public ActionResult GetMatched() public ActionResult SetAnswerMatchNo(string id) { var tempSub = AuthUtil.GetSubjectFromHttpRequest(Request); - return string.IsNullOrEmpty(tempSub) ? new EmptyResult() : SetAnswerMatch(tempSub, id, false, this); + return string.IsNullOrEmpty(tempSub) ? new EmptyResult() : MatchUtil.SetAnswerMatch(tempSub, id, false, this); } - private static ActionResult SetAnswerMatch(string fromUser, string toUser, bool yesOrNo, - ControllerBase discoverPeopleController) - { - var sRandom = GenerateRandomHash(20); - const string q = - "INSERT IGNORE INTO PeopleDiscoverMatch " + - "(from_person, to_person, answer, mn, ms) " + - "VALUES " + - "(SHA2(@p1,256)," + - "SHA2(@p2,256)," + - "@a," + - "(SELECT COALESCE(MAX(ms), 0) + 1 FROM PeopleDiscoverMatch), " + - "@ms" + - ")"; - - var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary - { - { "@p1", fromUser }, - { "@p2", toUser }, - { "@a", yesOrNo }, - { "@ms", sRandom } - }); - return discoverPeopleController.Ok(new JObject { { "r", i } }); - } - - private static string GenerateRandomHash(int length) - { - var randomBytes = new byte[length]; - using (var rng = RandomNumberGenerator.Create()) - { - rng.GetBytes(randomBytes); - } - - var hashBytes = SHA256.HashData(randomBytes); - - var hashStringBuilder = new StringBuilder(); - foreach (var b in hashBytes) hashStringBuilder.Append(b.ToString("x2")); - - return hashStringBuilder.ToString()[..length]; - } private static JArray? GetMatched(string tempSub) { diff --git a/Backend/Source/Controllers/DiscoverPeople/MatchUtil.cs b/Backend/Source/Controllers/DiscoverPeople/MatchUtil.cs new file mode 100644 index 00000000..c0f2b2ec --- /dev/null +++ b/Backend/Source/Controllers/DiscoverPeople/MatchUtil.cs @@ -0,0 +1,58 @@ +namespace PoliFemoBackend.Source.Controllers.DiscoverPeople; + +using System.Security.Cryptography; +using System.Text; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; +using Data; +using DB = PoliNetwork.Db.Utils.Database; + +public class MatchUtil +{ + private static string GenerateRandomHash(int length) + { + var randomBytes = new byte[length]; + using (var rng = RandomNumberGenerator.Create()) + { + rng.GetBytes(randomBytes); + } + + var hashBytes = SHA256.HashData(randomBytes); + + var hashStringBuilder = new StringBuilder(); + foreach (var b in hashBytes) hashStringBuilder.Append(b.ToString("x2")); + + return hashStringBuilder.ToString()[..length]; + } + + public static ActionResult SetAnswerMatch( + string fromUser, + string toUser, + bool yesOrNo, + ControllerBase discoverPeopleController + ) + { + var sRandom = GenerateRandomHash(20); + const string q = + "INSERT IGNORE INTO PeopleDiscoverMatch " + + "(from_person, to_person, answer, mn, ms) " + + "VALUES " + + "(SHA2(@p1,256)," + + "SHA2(@p2,256)," + + "@a," + + "(SELECT COALESCE(MAX(ms), 0) + 1 FROM PeopleDiscoverMatch), " + + "@ms" + + ")"; + + var dictionary = new Dictionary + { + { "@p1", fromUser }, + { "@p2", toUser }, + { "@a", yesOrNo }, + { "@ms", sRandom } + }; + var i = DB.Execute(q, GlobalVariables.DbConfigVar, dictionary); + var jObject = new JObject { { "r", i } }; + return discoverPeopleController.Ok(jObject); + } +} \ No newline at end of file From 94d148bf474b2889f516f87d4b7a14f661392305 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 16:46:42 +0200 Subject: [PATCH 15/17] Update MatchUtil.cs --- .../Controllers/DiscoverPeople/MatchUtil.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Backend/Source/Controllers/DiscoverPeople/MatchUtil.cs b/Backend/Source/Controllers/DiscoverPeople/MatchUtil.cs index c0f2b2ec..95917a8f 100644 --- a/Backend/Source/Controllers/DiscoverPeople/MatchUtil.cs +++ b/Backend/Source/Controllers/DiscoverPeople/MatchUtil.cs @@ -1,11 +1,21 @@ -namespace PoliFemoBackend.Source.Controllers.DiscoverPeople; +#region using System.Security.Cryptography; using System.Text; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; -using Data; -using DB = PoliNetwork.Db.Utils.Database; +using PoliFemoBackend.Source.Data; +using PoliNetwork.Db.Utils; + +#endregion + +namespace PoliFemoBackend.Source.Controllers.DiscoverPeople; + +#region + +using DB = Database; + +#endregion public class MatchUtil { From 6740a013f253f9db633e532c233048d0a5790e42 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 16:48:25 +0200 Subject: [PATCH 16/17] Update DbConfigUtilPoliFemo.cs --- .../Utils/Database/DbConfigUtilPoliFemo.cs | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Backend/Source/Utils/Database/DbConfigUtilPoliFemo.cs b/Backend/Source/Utils/Database/DbConfigUtilPoliFemo.cs index 9aa8da1d..036d13ed 100644 --- a/Backend/Source/Utils/Database/DbConfigUtilPoliFemo.cs +++ b/Backend/Source/Utils/Database/DbConfigUtilPoliFemo.cs @@ -15,7 +15,7 @@ namespace PoliFemoBackend.Source.Utils.Database; public static class DbConfigUtilPoliFemo { - public static DbConfig? DbConfigVar { get; set; } + public static DbConfig? DbConfigVar { get; private set; } public static void InitializeDbConfig() @@ -49,33 +49,33 @@ public static void InitializeDbConfig() var connectionString = DbConfigUtils.GetConnectionString(Data.GlobalVariables.DbConfigVar); - if (!string.IsNullOrEmpty(connectionString)) + if (string.IsNullOrEmpty(connectionString)) + return; + + Data.GlobalVariables.DbConnection = new MySqlConnection(connectionString); + try { - Data.GlobalVariables.DbConnection = new MySqlConnection(connectionString); - try - { - Data.GlobalVariables.DbConnection.Open(); - if (Data.GlobalVariables.DbConnection.State == ConnectionState.Open) - GlobalVariables.DefaultLogger.Info( - "Connection to db on start works! Performing table checks..."); - - if (Data.GlobalVariables.SkipDbSetup is null or false) - { - var sql = File.ReadAllText(Constants.SqlCommandsPath); - DB.ExecuteSelect(sql, Data.GlobalVariables.DbConfigVar); - } - + Data.GlobalVariables.DbConnection.Open(); + if (Data.GlobalVariables.DbConnection.State == ConnectionState.Open) GlobalVariables.DefaultLogger.Info( - "Table checks completed! Starting application..."); - } - catch (Exception ex) - { - GlobalVariables.DefaultLogger.Emergency( - "An error occurred while initializing the database. Check the details and try again."); - GlobalVariables.DefaultLogger.Emergency(ex.Message); + "Connection to db on start works! Performing table checks..."); - Environment.Exit(1); + if (Data.GlobalVariables.SkipDbSetup is null or false) + { + var sql = File.ReadAllText(Constants.SqlCommandsPath); + DB.ExecuteSelect(sql, Data.GlobalVariables.DbConfigVar); } + + GlobalVariables.DefaultLogger.Info( + "Table checks completed! Starting application..."); + } + catch (Exception ex) + { + GlobalVariables.DefaultLogger.Emergency( + "An error occurred while initializing the database. Check the details and try again."); + GlobalVariables.DefaultLogger.Emergency(ex.Message); + + Environment.Exit(1); } } From dd8c770e02e744c01a1a393126e96baf7df5e703 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 6 Aug 2023 16:56:50 +0200 Subject: [PATCH 17/17] Update DbConfigUtilPoliFemo.cs --- Backend/Source/Utils/Database/DbConfigUtilPoliFemo.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Backend/Source/Utils/Database/DbConfigUtilPoliFemo.cs b/Backend/Source/Utils/Database/DbConfigUtilPoliFemo.cs index 036d13ed..a33b205a 100644 --- a/Backend/Source/Utils/Database/DbConfigUtilPoliFemo.cs +++ b/Backend/Source/Utils/Database/DbConfigUtilPoliFemo.cs @@ -49,9 +49,9 @@ public static void InitializeDbConfig() var connectionString = DbConfigUtils.GetConnectionString(Data.GlobalVariables.DbConfigVar); - if (string.IsNullOrEmpty(connectionString)) + if (string.IsNullOrEmpty(connectionString)) return; - + Data.GlobalVariables.DbConnection = new MySqlConnection(connectionString); try {