Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discover people #199

Open
wants to merge 19 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
23bc83e
Update Lib_CSharp
invalid-email-address Aug 4, 2023
d324409
Merge branch 'dev' of https://github.com/PoliNetworkOrg/PoliFemoBacke…
invalid-email-address Aug 4, 2023
797af1d
Update PoliFemoBackend.csproj
invalid-email-address Aug 6, 2023
5de8fcd
Update PoliFemoBackend.csproj, DiscoverPeopleController.cs, DiscoverI…
invalid-email-address Aug 6, 2023
e24eb8a
Merge remote-tracking branch 'refs/remotes/origin/dev'
invalid-email-address Aug 6, 2023
695ebb8
Update DiscoverPeopleController.cs, DiscoverInfoBio.cs, DiscoverInfoL…
invalid-email-address Aug 6, 2023
955739d
Update DiscoverPeopleController.cs
invalid-email-address Aug 6, 2023
6be7817
Update DiscoverInfoLink.cs
invalid-email-address Aug 6, 2023
c2e8686
Update AccountExport.cs
invalid-email-address Aug 6, 2023
dee6166
Update AccountController.cs, DiscoverPeopleController.cs, DiscoverInf…
invalid-email-address Aug 6, 2023
f0eb7b6
Update AccountExport.cs, DiscoverInfoLink.cs, and MatchController.cs
invalid-email-address Aug 6, 2023
29c9bb5
Update AccountController.cs and Test.cs
invalid-email-address Aug 6, 2023
2f49a5b
Update AccountExport.cs
invalid-email-address Aug 6, 2023
92e30b7
Update MatchController.cs and UserUtil.cs
invalid-email-address Aug 6, 2023
ab4a3f1
Update MatchController.cs and UserUtil.cs
invalid-email-address Aug 6, 2023
c7b6180
Update MatchController.cs and MatchUtil.cs
invalid-email-address Aug 6, 2023
94d148b
Update MatchUtil.cs
invalid-email-address Aug 6, 2023
6740a01
Update DbConfigUtilPoliFemo.cs
invalid-email-address Aug 6, 2023
dd8c770
Update DbConfigUtilPoliFemo.cs
invalid-email-address Aug 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions Backend/Source/Controllers/Accounts/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,38 @@ public class ArticleByIdController : ControllerBase
/// <response code="500">Can't connect to the server</response>
[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);
if (string.IsNullOrEmpty(userid))
return null;

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)
{
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);
return userid;
}


/// <summary>
/// Delete the user's account and data
Expand Down
36 changes: 23 additions & 13 deletions Backend/Source/Controllers/Accounts/AccountExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,29 @@ 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<string, object?>
{
{ "@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()
};

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
Expand All @@ -58,20 +64,24 @@ 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");
room_occupancy_reports = roc,
other
});
var fileContents = Encoding.UTF8.GetBytes(fromObject.ToString());
var fileDownloadName = id + ".json";
return File(fileContents, "application/json", fileDownloadName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#region

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
{
/// <summary>
/// Discover people
/// </summary>
/// <returns>A JSON object of people</returns>
/// <response code="200">Request completed successfully</response>
/// <response code="404">No available people</response>
/// <response code="500">Can't connect to the server</response>
[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 = SHA2(@id,256)) " +
"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<string, object?>
{
{ "@id", tempSub }
});

var row = results?.Rows[0];
return row == null ? null : UserUtil.GetUser(row);
}
}
60 changes: 60 additions & 0 deletions Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoBio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#region

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.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 = SHA2(@id,256)";
var i = DB.ExecuteSelect(q, GlobalVariables.DbConfigVar, new Dictionary<string, object?>
{
{ "@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 = SHA2(@id,256)";
var i = DB.Execute(q, GlobalVariables.DbConfigVar, new Dictionary<string, object?>
{
{ "@id", tempSub },
{ "@bio", stringBio }
});
return discoverInfo.Ok(i);
}
}
69 changes: 69 additions & 0 deletions Backend/Source/Controllers/DiscoverPeople/Info/DiscoverInfoLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#region

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.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 = SHA2(@id,256)";
var i = DB.ExecuteSelect(q, GlobalVariables.DbConfigVar, new Dictionary<string, object?>
{
{ "@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)
{
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<string, object?>
{
{ "@id", tempSub },
{ "@link", stringLink }
});
return discoverInfo.Ok(i);
}

private static bool IsValidHttpOrHttpsLink(string input)
{
return Uri.TryCreate(input, UriKind.Absolute, out var result) &&
(result.Scheme == Uri.UriSchemeHttp || result.Scheme == Uri.UriSchemeHttps);
}
}
Loading