---
title: "Action script with a security trigger"
slug: "action-script-with-a-security-trigger"
abstract: "Web script example with a security trigger to validate whether a user creating or modifying an asset belongs to an appropriate security group."
---
# Action script with a security trigger

This example shows how to enforce custom security policies by automatically restricting or granting access to specific entities or properties based on a user's roles and permissions. The web script is connected to a *security* trigger that enforces access control by validating user permissions before executing actions.

> [!note]
> **Before you begin**
> -   Confirm that security policies are defined for the entity types involved in the script, such as `M.Asset` or `M.Content`.
>
> -   Ensure the schema includes properties required for security checks, such as `Owner` or `UserGroup`.
>
> -   Test the permissions of the user or role to validate the enforcement of security rules.

## Script

```csharp
var loadConfig = new EntityLoadConfiguration
{
  CultureLoadOption = CultureLoadOption.None,
  RelationLoadOption = new RelationLoadOption("UserGroupToUser"),
  PropertyLoadOption = PropertyLoadOption.None
};

var user = await MClient.Entities.GetAsync(Context.TriggeringUserId.Value, loadConfig);
if (user == null) throw new InvalidOperationException("Triggering user could not be found.");

var webAgencyGroup = await MClient.Users.GetUserGroupAsync("Web agency users");
if (webAgencyGroup == null) throw new InvalidOperationException("Web agency usergroup not found.");

var userGroups = await user.GetRelationAsync<IChildToManyParentsRelation>("UserGroupToUser");

if (!userGroups.Parents.Contains(webAgencyGroup.Id.Value))
{
  throw new ForbiddenException("Only users of usergroup 'Web agency users' are allowed to create or modify assets of image-type 'Web'.");
}

```

## Script explanation

This section steps through the script in execution order, explaining each part. The numbered items describe the sequence, not instructions to perform.

1. Create an `EntityLoadConfiguration` object to specify which properties should be loaded with the entity. In this example, we only need to load the `UserGroupToUser` relation, which specifies which groups the user belongs to.

    ```csharp
    var loadConfig = new EntityLoadConfiguration
    {
      CultureLoadOption = CultureLoadOption.None,
      RelationLoadOption = new RelationLoadOption("UserGroupToUser"),
      PropertyLoadOption = PropertyLoadOption.None
    };
    ```

2. Retrieve the `user` entity by specifying the user's *ID* ( `Context.TriggeringUserId.Value` ) and the load configuration.

    ```csharp
    var user = await MClient.Entities.GetAsync(Context.TriggeringUserId.Value, loadConfig);
    ```

    > [!note]
    > The `MClient` object is always available and can be used by all script types. For more information about `MClient`, please refer to the [SDK API reference](https://doc.sitecore.com/ch/en/developers/api-reference/index-en.html). `Entities` (a property of `MClient` with type `IEntitiesClient` ) has a `GetAsync` method that accepts an entity *ID* and optionally a load configuration object, and then returns the entity object.

3. If no user object is returned, the script throws an `InvalidOperationException`.

    ```csharp
    if (user == null) throw new InvalidOperationException("Triggering user could not be found.");
    ```

4. Get the *Web agency users* group. If that group isn't found, it throws an `InvalidOperationException`.

    ```csharp
    var webAgencyGroup = await MClient.Users.GetUserGroupAsync("Web agency users");
    if (webAgencyGroup == null) throw new InvalidOperationException("Web agency usergroup not found.");
    ```

5. Get the user groups of the user is part of.

    ```csharp
    var userGroups = await user.GetRelationAsync<IChildToManyParentsRelation>("UserGroupToUser");
    ```

    > [!warning]
    > To retrieve a relation object using `GetRelationAsync`, you need to specify the relation's type. In this case, it is `IChildToManyParentsRelation`. For more information about the relation's types, refer to [Stylelabs.M.Sdk.Contracts.Base](https://doc.sitecore.com/ch/en/developers/cloud-dev/overview.html "Overview") in the API reference.

6. Verify if the current user belongs to `webAgencyGroup` before allowing them to create or modify certain assets. If the user is not in this group, the script throws a `ForbiddenException`. This means that only members of *the Web agency* user group can create or modify image-type web assets.

    ```csharp
    if (!userGroups.Parents.Contains(webAgencyGroup.Id.Value))
    {
      throw new ForbiddenException("Only users of usergroup 'Web agency users' are allowed to create or modify assets of image-type 'Web'.");
    }
    ```

## Setup

1. [Add a new class](https://doc.sitecore.com/ch/en/users/content-hub/add-values-to-a-taxonomy.html) to the the `M.AssetType` taxonomy with the label `Web`.
2. [Create](https://doc.sitecore.com/ch/en/developers/cloud-dev/work-with-scripts.html "Work with scripts"), publish, and enable an *Action* script.
3. [Create an action](https://doc.sitecore.com/ch/en/developers/cloud-dev/create-an-action.html "Create an action") of type *Action script* and link it with the script.
4. [Create a new trigger](https://doc.sitecore.com/ch/en/developers/cloud-dev/create-a-trigger.html "Create a trigger") and set its objectives to *Entity creation* and *Entity modification*.
    - In the trigger conditions, add the entity definition *Asset* and add a new condition. Set that condition to *Type (AssetTypeToAsset) current value contains any Web*.
    - In the trigger actions, add the new action under **Security actions**.
5. Save and activate the trigger.