Skip to content

Commit

Permalink
Implement CodeQL recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Apr 8, 2024
1 parent b4f3f3f commit f02d3dc
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 120 deletions.
1 change: 0 additions & 1 deletion Backend.Tests/Mocks/PermissionServiceMock.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using BackendFramework.Interfaces;
using BackendFramework.Models;
Expand Down
1 change: 0 additions & 1 deletion Backend.Tests/Mocks/UserRepositoryMock.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using BackendFramework.Helper;
using BackendFramework.Interfaces;
Expand Down
2 changes: 1 addition & 1 deletion Backend/Controllers/InviteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public async Task<IActionResult> ValidateToken(string projectId, string token)
if (user.Email == tokenObj.Email)
{
currentUser = user;
if (!user.ProjectRoles.ContainsKey(projectId))
if (!user.ProjectRoles.TryGetValue(projectId, out var _roleId))
{
isUserRegisteredAndNotInProject = true;
}
Expand Down
2 changes: 1 addition & 1 deletion Backend/Controllers/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<IActionResult> GetAllProjectUsers(string projectId)
}

var allUsers = await _userRepo.GetAllUsers();
var projectUsers = allUsers.FindAll(user => user.ProjectRoles.ContainsKey(projectId));
var projectUsers = allUsers.FindAll(user => user.ProjectRoles.TryGetValue(projectId, out var _roleId));

return Ok(projectUsers);
}
Expand Down
11 changes: 3 additions & 8 deletions Backend/Controllers/UserRoleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public async Task<IActionResult> GetCurrentPermissions(string projectId)
return NotFound($"user: {userId}");
}

if (!user.ProjectRoles.ContainsKey(projectId))
if (!user.ProjectRoles.TryGetValue(projectId, out var roleId))
{
return Ok(new List<Permission>());
}
var userRole = await _userRoleRepo.GetUserRole(projectId, user.ProjectRoles[projectId]);
var userRole = await _userRoleRepo.GetUserRole(projectId, roleId);
if (userRole is null)
{
return Ok(new List<Permission>());
Expand Down Expand Up @@ -231,12 +231,7 @@ public async Task<IActionResult> UpdateUserRole(
return NotFound(userId);
}

string userRoleId;
if (changeUser.ProjectRoles.ContainsKey(projectId))
{
userRoleId = changeUser.ProjectRoles[projectId];
}
else
if (!changeUser.ProjectRoles.TryGetValue(projectId, out var userRoleId))
{
// Generate the userRole
var usersRole = new UserRole { ProjectId = projectId };
Expand Down
1 change: 0 additions & 1 deletion Backend/Helper/FileOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

Expand Down
1 change: 0 additions & 1 deletion Backend/Helper/FileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;

namespace BackendFramework.Helper
{
Expand Down
2 changes: 1 addition & 1 deletion Backend/Helper/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static List<WritingSystem> ConvertLangTagsToWritingSystems(IEnumerable<st
/// </summary>
public static List<WritingSystem> GetWritingSystems(string dirPath)
{
if (!Directory.GetFiles(dirPath, "*.ldml").Any())
if (Directory.GetFiles(dirPath, "*.ldml").Length == 0)
{
dirPath = FileStorage.GenerateWritingsSystemsSubdirPath(dirPath);
}
Expand Down
1 change: 0 additions & 1 deletion Backend/Helper/LiftHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using BackendFramework.Models;
using SIL.Lift.Parsing;

Expand Down
1 change: 0 additions & 1 deletion Backend/Helper/Sanitization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace BackendFramework.Helper
Expand Down
8 changes: 2 additions & 6 deletions Backend/Services/InviteService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using BackendFramework.Interfaces;
using BackendFramework.Models;
Expand Down Expand Up @@ -68,11 +67,8 @@ public async Task<bool> RemoveTokenAndCreateUserRole(Project project, User user,
user.ProjectRoles.Add(project.Id, userRole.Id);
await _userRepo.Update(user.Id, user);
// Generate the JWT based on those new userRoles
var updatedUser = await _permissionService.MakeJwt(user);
if (updatedUser is null)
{
throw new PermissionService.InvalidJwtTokenException("Unable to generate JWT.");
}
var updatedUser = await _permissionService.MakeJwt(user)
?? throw new PermissionService.InvalidJwtTokenException("Unable to generate JWT.");

await _userRepo.Update(updatedUser.Id, updatedUser);

Expand Down
36 changes: 12 additions & 24 deletions Backend/Services/LiftService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.Serialization;
using System.Security;
using System.Threading.Tasks;
using System.Xml;
Expand Down Expand Up @@ -33,7 +32,7 @@ public CombineLiftWriter(string path, ByteOrderStyle byteOrderStyle) : base(path
protected override void InsertPronunciationIfNeeded(
LexEntry entry, List<string> propertiesAlreadyOutput)
{
if (entry.Pronunciations.FirstOrDefault() is not null && entry.Pronunciations.First().Forms.Any())
if (entry.Pronunciations.Count != 0 && entry.Pronunciations.First().Forms.Length != 0)
{
foreach (var phonetic in entry.Pronunciations)
{
Expand Down Expand Up @@ -141,11 +140,8 @@ public void SetExportInProgress(string userId, bool isInProgress)
/// <summary> Query whether user has an in-progress export. </summary>
public bool IsExportInProgress(string userId)
{
if (!_liftExports.ContainsKey(userId))
{
return false;
}
return _liftExports[userId] == InProgress;
_liftExports.TryGetValue(userId, out var exportPath);
return exportPath == InProgress;
}

/// <summary> Store filePath for a user's Lift export. </summary>
Expand All @@ -159,12 +155,8 @@ public void StoreExport(string userId, string filePath)
/// <returns> Path to the Lift file on disk. </returns>
public string? RetrieveExport(string userId)
{
if (!_liftExports.ContainsKey(userId) || _liftExports[userId] == InProgress)
{
return null;
}

return _liftExports[userId];
_liftExports.TryGetValue(userId, out var exportPath);
return exportPath == InProgress ? null : exportPath;
}

/// <summary> Delete a stored Lift export path and its file on disk. </summary>
Expand All @@ -190,12 +182,8 @@ public void StoreImport(string userId, string filePath)
/// <returns> Path to the Lift file on disk. </returns>
public string? RetrieveImport(string userId)
{
if (!_liftImports.ContainsKey(userId))
{
return null;
}

return _liftImports[userId];
_liftImports.TryGetValue(userId, out var importPath);
return importPath;
}

/// <summary> Delete a stored Lift import path and its file on disk. </summary>
Expand All @@ -214,7 +202,7 @@ public bool DeleteImport(string userId)
/// <returns> A bool indicating whether a character set was added to the project. </returns>
public async Task<bool> LdmlImport(string dirPath, IProjectRepository projRepo, Project project)
{
if (!Directory.GetFiles(dirPath, "*.ldml").Any())
if (Directory.GetFiles(dirPath, "*.ldml").Length == 0)
{
dirPath = FileStorage.GenerateWritingsSystemsSubdirPath(dirPath);
}
Expand Down Expand Up @@ -478,11 +466,11 @@ private static void AddSenses(LexEntry entry, Word wordEntry, Dictionary<string,
var defDict = new Dictionary<string, string>();
foreach (var def in currentSense.Definitions)
{
if (defDict.ContainsKey(def.Language))
if (defDict.TryGetValue(def.Language, out var defText))
{
// This is an unexpected situation but rather than crashing or losing data we
// will just append extra definitions for the language with a separator.
defDict[def.Language] = $"{defDict[def.Language]}{sep}{def.Text}";
defDict[def.Language] = $"{defText}{sep}{def.Text}";
}
else
{
Expand All @@ -492,11 +480,11 @@ private static void AddSenses(LexEntry entry, Word wordEntry, Dictionary<string,
var glossDict = new Dictionary<string, string>();
foreach (var gloss in currentSense.Glosses)
{
if (glossDict.ContainsKey(gloss.Language))
if (glossDict.TryGetValue(gloss.Language, out var glossDef))
{
// This is an unexpected situation but rather than crashing or losing data we
// will just append extra definitions for the language with a separator.
glossDict[gloss.Language] = $"{glossDict[gloss.Language]}{sep}{gloss.Def}";
glossDict[gloss.Language] = $"{glossDef}{sep}{gloss.Def}";
}
else
{
Expand Down
1 change: 0 additions & 1 deletion Backend/Services/MergeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using BackendFramework.Helper;
using BackendFramework.Interfaces;
using BackendFramework.Models;
Expand Down
1 change: 0 additions & 1 deletion Backend/Services/PermissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Claims;
using System.Text;
using System.Text.Json;
Expand Down
Loading

0 comments on commit f02d3dc

Please sign in to comment.