Skip to content

AdminClient Class

The AdminClient class provides administrative functionalities for managing user accounts, roles, and custom data within the ByteCobra Accounts system.

Methods

  • SetRoleAsync(string accountId, string role): Assigns a role to a user account asynchronously.
  • RemoveRoleAsync(string accountId, string role): Removes a role from a user account asynchronously.
  • DeleteAccountAsync(string id, bool confirm): Deletes a user account asynchronously, if confirmed.
  • GetRolesAsync(string id): Fetches roles associated with a user account asynchronously.
  • SetCustomDataAsync<T>(string accountId, T data, string? key = null): Sets custom data for a user account asynchronously.
  • GetCustomDataAsync<T>(string accountId, string? key = null): Retrieves custom data for a user account asynchronously.
  • DeleteCustomDataAsync<T>(string accountId, string? key = null): Deletes custom data associated with a user account asynchronously. Overloaded to allow deletion by data type or key.
  • GetUserAsync(string accountId): Fetches details of a specific user account asynchronously.
  • GetUsersAsync(): Retrieves a list of all user accounts asynchronously.
  • SetBannedUntilAsync(string accountId, DateTime date): Sets a ban on a user account until a specific date asynchronously.
  • VerifyTokenAsync(string token): Verifies a user's authentication token asynchronously and returns user details if valid.

Special Roles

In the system, users designated as administrators are identified by the admin role. The assignment of admin roles is managed exclusively through the server's environment variables. To ensure security and integrity, it is not possible to add or remove the admin role directly through API endpoints. If you need to modify admin user assignments, please do so by adjusting the environment variables on the server. This approach helps maintain a secure and controlled process for managing administrative access.

Usage Notes

  • Role Management: The methods SetRoleAsync and RemoveRoleAsync are crucial for assigning or revoking user roles, enabling fine-grained access control within your application.
  • Account Deletion: Use DeleteAccountAsync with caution, as it permanently removes a user's account. The confirm parameter acts as a safeguard against accidental deletion.
  • Custom Data Management: The methods SetCustomDataAsync, GetCustomDataAsync, and DeleteCustomDataAsync facilitate the management of custom user-related data (in JSON format). Note: If a custom data type in C# is modified, you may need to migrate data from the old type to the new type.
  • User Information: GetUserAsync and GetUsersAsync provide access to user account details, useful for administrative dashboards or user management features.
  • Ban Management: SetBannedUntilAsync offers a mechanism to temporarily restrict access for specific user accounts, useful for enforcing community guidelines or terms of service.

Usage Example

This class is commonly accessed through the CobraAccounts class or the AccountService in Unity for a centralized approach. However, it's also designed to function independently, allowing direct usage without relying on a central class.

To use AdminClient, you'll need to instantiate it with an HttpClient instance configured with your API's base URL. After instantiation, you can call any of the provided asynchronous methods to perform operations against the account endpoints of the API.

var httpClient = new HttpClient { BaseAddress = new Uri("https://accounts.bytecobra.com") };
var adminClient = new AdminClient(httpClient);

// Assign a role to a user
await adminClient.SetRoleAsync("userAccountId", "administrator");

// Fetch and display all roles of a user
var roles = await adminClient.GetRolesAsync("userAccountId");
Console.WriteLine($"User Roles: {string.Join(", ", roles)}");