RoleModel
RoleModel
RoleModel holds the details of a role: its name, description, and rules. Each rule is an object of a Rule subclass that names one module, for example branches or content types. Pass the model to the Create and Update methods of Role.
When to Use This Class: Use this class to build the request for a new or changed role, or to read a fetched role as a typed object.
Conceptual Role: Acts as the data model that the Role class serializes under the role key of a request body.
Returns: RoleModel. The details of a role.
Properties
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| Name | string? | No | null | Name of the role. Serialized as name. |
| Description | string? | No | null | Description of the role. Serialized as description. |
| Rules | List<Rule>? | No | null | Rules of the role, one object per module. Use the Rule subclasses in the table below. Serialized as rules. |
| DeployContent | bool | No | true | Serialized as deploy_content. The SDK always sends it. |
The Rule base class and its subclasses below live in the Contentstack.Management.Core.Models namespace. Every subclass has the two base properties and a fixed Module value:
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| Rule.ACL | Dictionary<string, object>? | No | null | Permissions of the rule. Read-only, so it stays null. Serialized as acl. |
| Rule.Restrict | bool | No | false | Read-only, so it stays false. Serialized as restrict. |
| ContentTypeRules.Module | string | No | "content_type" | Read-only module name. Serialized as module. |
| ContentTypeRules.ContentTypes | List<string>? | No | null | UIDs of the content types the rule covers. Serialized as content_types. |
| BranchRules.Module | string | No | "branch" | Read-only module name. Serialized as module. |
| BranchRules.Branches | List<string>? | No | null | UIDs of the branches the rule covers. Serialized as branches. |
| BranchAliasRules.Module | string | No | "branch_alias" | Read-only module name. Serialized as module. |
| BranchAliasRules.BranchAliases | List<string>? | No | null | UIDs of the branch aliases the rule covers. Serialized as branch_aliases. |
| AssetRules.Module | string | No | "asset" | Read-only module name. Serialized as module. |
| AssetRules.Assets | List<string>? | No | null | UIDs of the assets the rule covers. Serialized as assets. |
| FolderRules.Module | string | No | "folder" | Read-only module name. Serialized as module. |
| FolderRules.Folders | List<string>? | No | null | UIDs of the asset folders the rule covers. Serialized as folders. |
| EnvironmentRules.Module | string | No | "environment" | Read-only module name. Serialized as module. |
| EnvironmentRules.Environments | List<string>? | No | null | Environments the rule covers. Serialized as environments. |
| TaxonomyRules.Module | string | No | "taxonomy" | Read-only module name. Serialized as module. |
| TaxonomyRules.Taxonomies | List<string>? | No | null | UIDs of the taxonomies the rule covers. Serialized as taxonomies. |
| TaxonomyRules.Terms | List<string>? | No | null | UIDs of the terms the rule covers. Serialized as terms. |
| TaxonomyRules.ContentTypes | List<TaxonomyContentType>? | No | null | Content types for the taxonomy rule. Serialized as content_types. |
| TaxonomyContentType.Uid | string? | No | null | UID of a content type. Serialized as uid. |
| TaxonomyContentType.ACL | Dictionary<string, object>? | No | null | Read-only, so it stays null. Serialized as acl. |
Class-Level Notes
Fields the SDK sends.
- The SDK leaves out every property that is null. It always sends DeployContent (true unless you set it to false) and restrict (false) on each rule.
- The client's serializer writes each rule with the properties of its subclass, so a BranchRules object sends module and branches.
- The SDK checks none of the properties. The Create a role request of the Content Management API reference describes the rules and permissions a role takes.
Known issue Rule.ACL and Rule.Restrict have no setter, so the SDK never sends rule permissions (acl) and always sends restrict as false. A role that you create or update through RoleModel has rules without permissions. Set the permissions of the role in the Contentstack web app after you create or update it.
Reading a fetched role.
- Deserialize the role node of a response with client.SerializerOptions. Those options read each rule into the subclass that its module names, and into Rule for any other module.
- ACL stays null and Restrict stays false on a deserialized rule, because the properties have no setter. Read the permissions from the JSON instead.
Method Index
RoleModel has no methods. Set and read its properties directly, and pass the object to the Create or Update method.
Class-Level Snippet
The snippet below builds a role with branch, content type, and environment rules, creates it, and reads the rule permissions of a fetched role from the JSON.
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
Stack stack = client.Stack("<API_KEY>");
RoleModel model = new RoleModel
{
Name = "Blog Editor",
Description = "Edits blog posts on the main branch",
DeployContent = false,
Rules = new List<Rule>
{
new BranchRules { Branches = new List<string> { "main" } },
new ContentTypeRules { ContentTypes = new List<string> { "blog_post" } },
new EnvironmentRules { Environments = new List<string> { "<ENVIRONMENT_NAME>" } }
}
};
ParameterCollection collection = new ParameterCollection();
collection.Add("include_rules", true);
try
{
stack.Role().Create(model);
ContentstackResponse response = stack.Role("<ROLE_UID>").Fetch(collection);
Console.WriteLine(response.OpenJsonObjectResponse()["role"]?["rules"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}