User Privilege Access with C# SDK and Realm - Please put in Realm Docs

To allow different levels of user privileges after signing in with C# and MongoDB Realm, you can use MongoDB Realm’s data access controls to define access rules based on user roles.

Here are the general steps to implement this functionality:

  1. Define the different roles that users can have in your application, such as “admin”, “editor”, and “viewer”. You can do this by creating a new collection in your MongoDB database and adding documents that define each role.

  2. Create a function in your MongoDB Realm app that returns the user’s role based on their credentials. You can use the context object to access the user’s credentials and query your role collection to determine their role.

exports.getRole = function() {
  const users = context.services.mongodb.db("my-db").collection("users");
  const user = users.findOne({ email: context.user.data.email });
  return user.role;
};
  1. Define access rules for your MongoDB collections that restrict access based on the user’s role. You can do this by navigating to the “Data Access” tab in the MongoDB Realm dashboard and creating your rules. For example, you might create a rule that allows “admin” users to read and write all documents in a collection, while “viewer” users can only read documents.
{
  "%%read": "role == 'admin'",
  "%%write": "role == 'admin'",
  "%%clean": "role == 'admin'",
  "field": {
    "%%read": "role == 'viewer'"
  }
}
  1. In your C# application, retrieve the user’s role using the MongoDB.Driver.Realm namespace and use it to determine their level of access. For example, you might display different menus or UI elements based on the user’s role.
using MongoDB.Driver.Realm;

var app = App.Create("<your-app-id>");
var emailPasswordCredentials = Credentials.EmailPassword("<email>", "<password>");
var user = await app.LogInAsync(emailPasswordCredentials);

var role = await user.Functions.CallAsync<string>("getRole");

if (role == "admin") {
  // display admin menu
} else {
  // display user menu
}

By following these steps, you can allow different levels of user privileges after signing in with C# and MongoDB Realm.

Modifications and improvements to the above guide, for some reason Visual Studio Code will change “using realms;” to “using MongoDB.Driver.Realm;” and I’m not sure why it defaults an autocorrect to this. If you are using any .net auto completes to help speed up your line writes, make sure you’re looking for this autocorrect. especially if you use anything to “pretty” your code.

But here you go with the fixes, I’ll provide screenshots in the next edit.

To provide different levels of user privileges in your C# and MongoDB Realm application, MongoDB Realm’s data access controls can be used to define access rules based on user roles. Here’s how you can implement this functionality:

  1. Define the different roles that users can have in your application, such as “admin”, “editor”, and “viewer”. You can create a new collection in your MongoDB database and add documents that define each role.
  2. Create a function in your MongoDB Realm app that returns the user’s role based on their credentials. Use the context object to access the user’s credentials and query your role collection to determine their role.

Example:

JavaScript

exports.getRole = function() {
  const users = context.services.mongodb.db("my-db").collection("users");
  const user = users.findOne({ email: context.user.data.email });
  return user.role;
};

  1. Define access rules for your MongoDB collections that restrict access based on the user’s role. Navigate to the “Data Access” tab in the MongoDB Realm dashboard and create your rules. For example, you might create a rule that allows “admin” users to read and write all documents in a collection, while “viewer” users can only read documents.

Example:

This is the JSON

{
  "%%read": "role == 'admin'",
  "%%write": "role == 'admin'",
  "%%clean": "role == 'admin'",
  "field": {
    "%%read": "role == 'viewer'"
  }
}

Repaste this with any roles you want to add, and make the changes appropriately.

  1. Retrieve the user’s role in your C# application using the MongoDB.Driver.Realm namespace and use it to determine their level of access. You can display different menus or UI elements based on the user’s role.

Example:
CSharp

using realms;

var app = App.Create("<your-app-id>");
var emailPasswordCredentials = Credentials.EmailPassword("<email>", "<password>");
var user = await app.LogInAsync(emailPasswordCredentials);

var role = await user.Functions.CallAsync<string>("getRole");

if (role == "admin") {
  // display admin menu
} else {
  // display user menu
}

By following these steps, you can easily allow different levels of user privileges in your C# and MongoDB Realm application.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.