Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ban user functionality #101

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading