Skip to content

Commit

Permalink
mask email instead of filtering it;
Browse files Browse the repository at this point in the history
  • Loading branch information
ctcac00 committed Jul 31, 2024
1 parent 79f18af commit 9ddd75e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
17 changes: 1 addition & 16 deletions rest_service/Controllers/PlayersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,19 @@ public async Task<List<PlayerResponse>> GetPlayers([FromQuery] PlayerRequest pla
if (!string.IsNullOrEmpty(playerRequest.Id))
filter &= Builders<Player>.Filter.Eq("Id", playerRequest.Id);

// Projection to only include Name and Age fields
var projection = Builders<Player>.Projection
.Exclude("Email");

// If Name but no Location, then get Location from players_unique
if (!string.IsNullOrEmpty(playerRequest.Name) && string.IsNullOrEmpty(playerRequest.Location))
{

// Projection to only include Name and Age fields
var projectionUnique = Builders<PlayerUnique>.Projection
.Exclude("Email");

var playerUnique = _playersUniqueCollection
.Find(Builders<PlayerUnique>
.Filter.Eq(x => x.Name, playerRequest.Name))
.Project<PlayerUnique>(projectionUnique)
.FirstOrDefault<PlayerUnique>();

if (playerUnique != null)
filter &= Builders<Player>.Filter.Eq(x => x.Location, playerUnique.Location);
}

var players = await _playersCollection.FindAsync(filter, new FindOptions<Player, Player>() { Limit = 10, Projection = projection });
var players = await _playersCollection.FindAsync(filter, new FindOptions<Player, Player>() { Limit = 10 });

var playersResponse =
players.ToList().Select(player => new PlayerResponse(player)).ToList();
Expand Down Expand Up @@ -318,11 +308,6 @@ public async Task<List<PlayerResponse>> PlayerSearch([FromQuery] PlayerRequest p
}
}
}
}),
new BsonDocument("$project", new BsonDocument
{
{ "Email", 0 },

})
}
},
Expand Down
21 changes: 20 additions & 1 deletion rest_service/Dtos/ResponseObjects/PlayerResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,27 @@ public PlayerResponse(Player player)
{
Id = player.Id.ToString();
Name = player.Name;
Email = player.Email;
Email = MaskEmail(player.Email ?? string.Empty);
Team = player.Team;
Location = player.Location;
}

public string MaskEmail(string email)
{
if (string.IsNullOrEmpty(email) || !email.Contains("@"))
return email;

string[] emailArr = email.Split('@');
string domainExt = Path.GetExtension(email);

string maskedEmail = string.Format("{0}****{1}@{2}****{3}{4}",
emailArr[0][0],
emailArr[0].Substring(emailArr[0].Length - 1),
emailArr[1][0],
emailArr[1].Substring(emailArr[1].Length - domainExt.Length - 1, 1),
domainExt
);

return maskedEmail;
}
}

0 comments on commit 9ddd75e

Please sign in to comment.