Skip to content

Commit

Permalink
Ban user functionality (#101)
Browse files Browse the repository at this point in the history
* Add ban functionality

Add properties to set BanDuration on update user and BannedUtil to verify the datetime of the ban applied to the user. Updated the projects from .Net7 to .Net8 (LTS).

* Fix indentation type.
  • Loading branch information
celestebyte committed Jul 13, 2024
1 parent b7ccac5 commit 9d3bbdb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Gotrue/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public class User
[JsonProperty("updated_at")]
public DateTime? UpdatedAt { get; set; }

[JsonProperty("banned_until")]
public DateTime? BannedUntil { get; set; }

[JsonProperty("is_anonymous")]
public bool IsAnonymous { get; set; }

Expand Down Expand Up @@ -103,6 +106,19 @@ public class AdminUserAttributes : UserAttributes
/// </summary>
[JsonProperty("phone_confirm")]
public bool? PhoneConfirm { get; set; }

/// <summary>
/// <para>Determines how long a user is banned for. </para>
/// <para>This property is ignored when creating a user.
/// If you want to create a user banned, first create the user then update it sending this property.</para>
/// <para>The format for the ban duration follows a strict sequence of decimal numbers with a unit suffix.
/// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".</para>
/// <para>For example, some possible durations include: '300ms', '2h45m', '1200s'.</para>
/// <para>Setting the ban duration to "none" lifts the ban on the user.</para>
/// <para>Only a service role can modify.</para>
/// </summary>
[JsonProperty("ban_duration")]
public string? BanDuration { get; set; }
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion GotrueExample/GotrueExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion GotrueTests/GotrueTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
24 changes: 24 additions & 0 deletions GotrueTests/ServiceRoleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ public async Task UpdateUserById()
AreNotEqual(createdUser.Email, updatedUser.Email);
}

[TestMethod("Service Role: Ban User by Id")]
public async Task BanUserById()
{
var createdUser = await _client.CreateUser($"{RandomString(12)}@supabase.io", PASSWORD);

IsNotNull(createdUser);

int banDurationSeconds = RandomNumber();
DateTime bannedUntil = DateTime.UtcNow + TimeSpan.FromSeconds(banDurationSeconds);
var updatedUser = await _client.UpdateUserById(createdUser.Id ?? throw new InvalidOperationException(), new AdminUserAttributes { BanDuration = $"{banDurationSeconds}s" });

IsNotNull(updatedUser);

AreEqual(createdUser.Id, updatedUser.Id);
IsNotNull(updatedUser.BannedUntil);
IsTrue((updatedUser.BannedUntil.Value - bannedUntil).Duration().TotalSeconds < 1);

updatedUser = await _client.UpdateUserById(createdUser.Id ?? throw new InvalidOperationException(), new AdminUserAttributes { BanDuration = "none" });
IsNotNull(updatedUser);

AreEqual(createdUser.Id, updatedUser.Id);
IsFalse(updatedUser.BannedUntil.HasValue);
}

[TestMethod("Service Role: Delete User")]
public async Task DeletesUser()
{
Expand Down
11 changes: 11 additions & 0 deletions GotrueTests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ public static string GetRandomPhoneNumber()
return $"+1{inner}";
}

/// <summary>
/// Returns a random number within the limits specified via parameters.
/// </summary>
/// <param name="minValue">Minimum value. Default 0.</param>
/// <param name="maxValue">Maximum value. Default 1000.</param>
/// <returns>Integer within the range.</returns>
public static int RandomNumber(int minValue = 0, int maxValue = 1000)
{
return Random.Next(minValue, maxValue);
}

public static string GenerateServiceRoleToken()
{
var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("37c304f8-51aa-419a-a1af-06154e63707a")); // using GOTRUE_JWT_SECRET
Expand Down

0 comments on commit 9d3bbdb

Please sign in to comment.