Skip to content

Commit 937f713

Browse files
Add in RepositoryPaths (#42)
- Add in RepositoryPaths to get a default repo root. - Update to C# 10.0
1 parent 06e5ec8 commit 937f713

File tree

6 files changed

+119
-68
lines changed

6 files changed

+119
-68
lines changed

Directory.Build.props

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
<Project>
22
<PropertyGroup Label="Package information">
3-
<PackageLicenseExpression>MIT</PackageLicenseExpression>
4-
<PackageProjectUrl>https://github.com/IntelliTect/Multitool</PackageProjectUrl>
5-
<RepositoryUrl>https://github.com/IntelliTect/Multitool</RepositoryUrl>
6-
<Authors>IntelliTect</Authors>
7-
<!-- Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
8-
<PublishRepositoryUrl>true</PublishRepositoryUrl>
9-
10-
<!-- Embed source files that are not tracked by the source control manager in the PDB -->
11-
<EmbedUntrackedSources>true</EmbedUntrackedSources>
12-
13-
<!-- Build symbol package (.snupkg) to distribute the PDB containing Source Link -->
14-
<IncludeSymbols>true</IncludeSymbols>
15-
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
3+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
4+
<PackageProjectUrl>https://github.com/IntelliTect/Multitool</PackageProjectUrl>
5+
<RepositoryUrl>https://github.com/IntelliTect/Multitool</RepositoryUrl>
6+
<Authors>IntelliTect</Authors>
7+
<!-- Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
8+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
9+
10+
<!-- Embed source files that are not tracked by the source control manager in the PDB -->
11+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
12+
13+
<!-- Build symbol package (.snupkg) to distribute the PDB containing Source Link -->
14+
<IncludeSymbols>true</IncludeSymbols>
15+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
16+
</PropertyGroup>
17+
<PropertyGroup>
18+
<LangVersion>10.0</LangVersion>
19+
<ImplicitUsings>true</ImplicitUsings>
20+
<Nullable>enable</Nullable>
1621
</PropertyGroup>
1722
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
18-
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
23+
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
1924
</PropertyGroup>
2025
<ItemGroup>
21-
<SourceRoot Include="$(MSBuildThisFileDirectory)/"/>
22-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
23-
<PrivateAssets>all</PrivateAssets>
24-
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
25-
</PackageReference>
26+
<SourceRoot Include="$(MSBuildThisFileDirectory)"/>
27+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
28+
<PrivateAssets>all</PrivateAssets>
29+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
30+
</PackageReference>
2631
</ItemGroup>
27-
</Project>
32+
</Project>

IntelliTect.Multitool.Tests/ClaimsPrincipalGetRolesTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class ClaimsPrincipalGetRolesTests
1111
[Fact]
1212
public void WhenClaimsPrincipalNull_Should_Throw()
1313
{
14-
ClaimsPrincipal sut = null;
14+
ClaimsPrincipal? sut = null;
1515

16-
Assert.Throws<ArgumentNullException>(() => sut.GetRoles());
16+
Assert.Throws<ArgumentNullException>(() => sut!.GetRoles());
1717
}
1818

1919
[Fact]

IntelliTect.Multitool.Tests/ClaimsPrincipalGetUserIdTests.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,31 @@
44
using IntelliTect.Multitool.Security;
55
using Xunit;
66

7-
namespace IntelliTect.Multitool.Tests
7+
namespace IntelliTect.Multitool.Tests;
8+
9+
public class ClaimsPrincipalGetUserIdTests
810
{
9-
public class ClaimsPrincipalGetUserIdTests
11+
[Fact]
12+
public void WhenClaimsPrincipalNull_Should_Throw()
1013
{
11-
[Fact]
12-
public void WhenClaimsPrincipalNull_Should_Throw()
13-
{
14-
ClaimsPrincipal sut = null;
14+
ClaimsPrincipal? sut = null;
1515

16-
Assert.Throws<ArgumentNullException>(() => sut.GetUserId());
17-
}
16+
Assert.Throws<ArgumentNullException>(() => sut!.GetUserId());
17+
}
1818

19-
[Fact]
20-
public void WhenClaimsPrincipalHasNoProperty_Should_ReturnNull()
21-
{
22-
ClaimsPrincipal sut = new ClaimsPrincipal();
19+
[Fact]
20+
public void WhenClaimsPrincipalHasNoProperty_Should_ReturnNull()
21+
{
22+
ClaimsPrincipal sut = new ClaimsPrincipal();
2323

24-
Assert.Null(sut.GetUserId());
25-
}
24+
Assert.Null(sut.GetUserId());
25+
}
2626

27-
[Fact]
28-
public void WhenClaimsPrincipalHasId_Should_ReturnString()
29-
{
30-
ClaimsPrincipal sut = new GenericPrincipal(new GenericIdentity("Taki The Frog"), new []{ "Bar" } );
27+
[Fact]
28+
public void WhenClaimsPrincipalHasId_Should_ReturnString()
29+
{
30+
ClaimsPrincipal sut = new GenericPrincipal(new GenericIdentity("Taki The Frog"), new []{ "Bar" } );
3131

32-
Assert.Equal("Taki The Frog", sut.GetUserId());
33-
}
32+
Assert.Equal("Taki The Frog", sut.GetUserId());
3433
}
3534
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Security.Claims;
3+
using System.Security.Principal;
4+
using Xunit;
5+
6+
namespace IntelliTect.Multitool.Tests;
7+
8+
public class RepositoryPathsTests
9+
{
10+
[Fact]
11+
public void GetDefaultRepoRoot_ReturnsRepoRootDirectory()
12+
{
13+
// Makes the assumption that the repository directory for this solution is named the same as the solution
14+
Assert.EndsWith(nameof(Multitool), RepositoryPaths.GetDefaultRepoRoot());
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace IntelliTect.Multitool;
2+
/// <summary>
3+
/// Provides normalized paths.
4+
/// </summary>
5+
public static class RepositoryPaths
6+
{
7+
/// <summary>
8+
/// Finds the root of the repository by looking for the .git folder.
9+
/// </summary>
10+
/// <returns>Full path to repo root</returns>
11+
public static string GetDefaultRepoRoot()
12+
{
13+
DirectoryInfo? currentDirectory = new(Directory.GetCurrentDirectory());
14+
15+
while (currentDirectory is not null)
16+
{
17+
DirectoryInfo[] subDirectories = currentDirectory.GetDirectories(".git");
18+
if (subDirectories.Length > 0)
19+
{
20+
return currentDirectory.FullName;
21+
}
22+
23+
currentDirectory = currentDirectory.Parent;
24+
}
25+
26+
throw new InvalidOperationException("Could not find the repo root directory from the current directory. Current directory is expected to be the repoRoot sub directory.");
27+
}
28+
}

IntelliTect.Multitool/Security/ClaimsPrincipalExtensions.cs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,45 @@
33
using System.Linq;
44
using System.Security.Claims;
55

6-
namespace IntelliTect.Multitool.Security
6+
namespace IntelliTect.Multitool.Security;
7+
8+
/// <summary>
9+
/// Gets information from a <see cref="ClaimsPrincipal"/>
10+
/// </summary>
11+
public static class ClaimsPrincipalExtensions
712
{
813
/// <summary>
9-
/// Gets information from a <see cref="ClaimsPrincipal"/>
14+
/// Gets the user ID from a <see cref="ClaimsPrincipal"/>.
1015
/// </summary>
11-
public static class ClaimsPrincipalExtensions
16+
/// <param name="principal">The <see cref="ClaimsPrincipal"/> to find a user ID for.</param>
17+
/// <returns>A <see cref="string"/>, or null if the <see cref="ClaimsPrincipal"/> doesn't contain a <see cref="ClaimTypes.NameIdentifier"/>.</returns>
18+
/// <exception cref="ArgumentNullException">principal is null.</exception>
19+
public static string? GetUserId(this ClaimsPrincipal principal)
1220
{
13-
/// <summary>
14-
/// Gets the user ID from a <see cref="ClaimsPrincipal"/>.
15-
/// </summary>
16-
/// <param name="principal">The <see cref="ClaimsPrincipal"/> to find a user ID for.</param>
17-
/// <returns>A <see cref="string"/>, or null if the <see cref="ClaimsPrincipal"/> doesn't contain a <see cref="ClaimTypes.NameIdentifier"/>.</returns>
18-
/// <exception cref="ArgumentNullException">principal is null.</exception>
19-
public static string GetUserId(this ClaimsPrincipal principal)
20-
{
21-
if (principal == null) throw new ArgumentNullException(nameof(principal));
21+
if (principal is null) throw new ArgumentNullException(nameof(principal));
2222

23-
Claim claim = principal.FindFirst(ClaimTypes.NameIdentifier);
24-
if (claim != null) return claim.Value;
23+
Claim? claim = principal.FindFirst(ClaimTypes.NameIdentifier);
24+
if (claim is null)
25+
{
2526
claim = principal.FindFirst(ClaimTypes.Name);
2627
return claim?.Value;
2728
}
2829

30+
return claim.Value;
31+
}
2932

30-
/// <summary>
31-
/// Gets all the roles a <see cref="ClaimsPrincipal"/> belongs to.
32-
/// </summary>
33-
/// <param name="principal">The <see cref="ClaimsPrincipal"/> to find roles for.</param>
34-
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="string"/> if found, or an empty set.</returns>
35-
/// <exception cref="ArgumentNullException">principal is null.</exception>
36-
public static IEnumerable<string> GetRoles(this ClaimsPrincipal principal)
37-
{
38-
if (principal == null) throw new ArgumentNullException(nameof(principal));
3933

40-
IEnumerable<Claim> claims = principal.FindAll(ClaimTypes.Role);
41-
return claims?.Select(claim => claim.Value) ?? Enumerable.Empty<string>();
42-
}
34+
/// <summary>
35+
/// Gets all the roles a <see cref="ClaimsPrincipal"/> belongs to.
36+
/// </summary>
37+
/// <param name="principal">The <see cref="ClaimsPrincipal"/> to find roles for.</param>
38+
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="string"/> if found, or an empty set.</returns>
39+
/// <exception cref="ArgumentNullException">principal is null.</exception>
40+
public static IEnumerable<string> GetRoles(this ClaimsPrincipal principal)
41+
{
42+
if (principal is null) throw new ArgumentNullException(nameof(principal));
43+
44+
IEnumerable<Claim> claims = principal.FindAll(ClaimTypes.Role);
45+
return claims.Select(claim => claim.Value);
4346
}
4447
}

0 commit comments

Comments
 (0)