Skip to content

Commit

Permalink
Update two services
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthagen committed Oct 23, 2020
1 parent 90c5f5a commit ac8047b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
9 changes: 4 additions & 5 deletions Backend/Interfaces/IUserService.cs
Expand Up @@ -9,14 +9,13 @@ public interface IUserService
{
Task<List<User>> GetAllUsers();
Task<User> GetUser(string userId);
Task<string> GetUserAvatar(string userId);
Task<User> Create(User user);
Task<string?> GetUserAvatar(string userId);
Task<User?> Create(User user);
Task<ResultOfUpdate> Update(string userId, User user, bool updateIsAdmin = false);
Task<bool> Delete(string userId);
Task<bool> DeleteAllUsers();
Task<User> Authenticate(string username, string password);
Task<User> MakeJwt(User user);
Task<User?> Authenticate(string username, string password);
Task<User?> MakeJwt(User user);
Task<ResultOfUpdate> ChangePassword(string userId, string password);
void Sanitize(User user);
}
}
13 changes: 8 additions & 5 deletions Backend/Services/SemDomParser.cs
Expand Up @@ -84,17 +84,20 @@ public async Task<List<SemanticDomainWithSubdomains>> ParseSemanticDomains(strin
/// <summary> Sorts semantic domains by string length of the number, then numerically </summary>
private class SemDomComparer : IComparer<SemanticDomain>
{
public int Compare(SemanticDomain x, SemanticDomain y)
public int Compare(SemanticDomain? x, SemanticDomain? y)
{
if (x == null || y == null)
{
return 0;
}

var lengthComparison = x.Id.Length.CompareTo(y.Id.Length);
if (lengthComparison == 0)
{
return x.Id.CompareTo(y.Id);
}
else
{
return lengthComparison;
}

return lengthComparison;
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions Backend/Services/UserApiServices.cs
Expand Up @@ -104,7 +104,7 @@ private static byte[] CreateSalt()

/// <summary> Confirms login credentials are valid. </summary>
/// <returns> User when credentials are correct, null otherwise. </returns>
public async Task<User> Authenticate(string username, string password)
public async Task<User?> Authenticate(string username, string password)
{
// Fetch the stored user.
var userList = await _userDatabase.Users.FindAsync(x =>
Expand All @@ -124,11 +124,11 @@ public async Task<User> Authenticate(string username, string password)
return PasswordHash.ValidatePassword(hashedPassword, password) ? await MakeJwt(foundUser) : null;
}

public async Task<User> MakeJwt(User user)
public async Task<User?> MakeJwt(User user)
{
const int tokenExpirationMinutes = 60 * 4;
var tokenHandler = new JwtSecurityTokenHandler();
var secretKey = Environment.GetEnvironmentVariable("COMBINE_JWT_SECRET_KEY");
var secretKey = Environment.GetEnvironmentVariable("COMBINE_JWT_SECRET_KEY")!;
var key = Encoding.ASCII.GetBytes(secretKey);

// Fetch the projects Id and the roles for each Id
Expand Down Expand Up @@ -200,7 +200,7 @@ public async Task<User> GetUser(string userId)
}

/// <summary> Finds <see cref="User"/> with specified userId and returns avatar filepath </summary>
public async Task<string> GetUserAvatar(string userId)
public async Task<string?> GetUserAvatar(string userId)
{
var filterDef = new FilterDefinitionBuilder<User>();
var filter = filterDef.Eq(x => x.Id, userId);
Expand Down Expand Up @@ -236,7 +236,7 @@ public async Task<ResultOfUpdate> ChangePassword(string userId, string password)

/// <summary> Adds a <see cref="User"/> </summary>
/// <returns> The <see cref="User"/> created, or null if the user could not be created. </returns>
public async Task<User> Create(User user)
public async Task<User?> Create(User user)
{
// Check if collection is not empty
var users = await GetAllUsers();
Expand Down Expand Up @@ -267,12 +267,12 @@ public async Task<bool> Delete(string userId)
}

/// <summary> Removes avatar path, password, and token from <see cref="User"/> </summary>
public void Sanitize(User user)
private static void Sanitize(User user)
{
// .Avatar or .Token set to "" or null will not be updated in the database
user.Avatar = null;
user.Password = null;
user.Token = null;
user.Avatar = "";
user.Password = "";
user.Token = "";
}

/// <summary> Updates <see cref="User"/> with specified userId </summary>
Expand Down

0 comments on commit ac8047b

Please sign in to comment.