# Document Protection

**Document Protection** allows defining and enforcing different editing restrictions in the document for a set of users and groups.

This article describes how this feature can be used in terms of UI and API of **RadRichTextEditor**.

* [Defining Users](https://www.telerik.com/products/winforms/documentation/controls/richtexteditor/features/document-protection#defining-the-users)

* [Changing the Current User](https://www.telerik.com/products/winforms/documentation/controls/richtexteditor/features/document-protection#changing-the-current-user)

* [Toggling Document Protection](https://www.telerik.com/products/winforms/documentation/controls/richtexteditor/features/document-protection#toggling-document-protection)

* [Creating And Modifying Document Protection Regions](https://www.telerik.com/products/winforms/documentation/controls/richtexteditor/features/document-protection#creating-and-modifying-document-protection-regions)

## Defining the Users

The editing restrictions can be used with a set of users or groups. When document protection is enforced, each user will be able to edit only the parts of the document he/she has rights for and the parts that are editable by the group he/she belongs to.

The users that will be able to edit the document and the groups they are in can be declared like this:

```C#
this.radRichTextEditor1.Users = new PermissionInfoCollection()
{
    PermissionInfo.CreateEveryonePermissionInfo(),
    new PermissionInfo("jmiller", PermissionType.Individual, "James Miller"),
    new PermissionInfo("jsmith", PermissionType.Individual, "John Smith"),
    new PermissionInfo("rbrown", PermissionType.Individual, "Robert Brown"),
    new PermissionInfo("Administrators", PermissionType.Group, "Administrators"),
};

```

```VB.NET
Me.radRichTextEditor1.Users = New PermissionInfoCollection() From {PermissionInfo.CreateEveryonePermissionInfo(), New PermissionInfo("jmiller", PermissionType.Individual, "James Miller"), New PermissionInfo("jsmith", PermissionType.Individual, "John Smith"), New PermissionInfo("rbrown", PermissionType.Individual, "Robert Brown"), New PermissionInfo("Administrators", PermissionType.Group, "Administrators")}

```

This code determines 3 users and two groups but does not specify the relationship between the users and the groups. This is so because users have the potential to change their groups.

The collection kept in the **Users** property of **RadRichTextEditor** is also used in the default   **ChangeEditingPermissionsDialog** which creates the **PermissionRangeInfos** to be inserted in the document.

## Changing the Current User

The current user of the document is set using the **CurrentUser** property of the editor.  It is of type **UserInfo** - here in addition to the name of the user, the group that the user belongs to can also be passed.

```C#
this.radRichTextEditor1.CurrentUser = new UserInfo("Users", "James Miller", "jmiller", "jmiller@example.com");
    
```

```VB.NET
Me.radRichTextEditor1.CurrentUser = New UserInfo("Users", "James Miller", "jmiller", "jmiller@example.com")

```

You can also create a collection with the **UserInfos** that will be interacting with the document and wire it with   some UI of yours. For example, if you set-up the collection as follows:

```C#
List<UserInfo> CurrentUsers = new List<UserInfo>()
{
    new UserInfo("Users", "James Miller", "jmiller", "jmiller@example.com"),
    new UserInfo("Administrators", "John Smith", "jsmith", "jsmith@example.com"),
    new UserInfo("Administrators", "Robert Brown", "rbrown", "rbrown@example.com"),
};

```

```VB.NET
Dim CurrentUsers As New List(Of UserInfo)() From {
    New UserInfo("Users", "James Miller", "jmiller", "jmiller@example.com"),
    New UserInfo("Administrators", "John Smith", "jsmith", "jsmith@example.com"),
    New UserInfo("Administrators", "Robert Brown", "rbrown", "rbrown@example.com")
}

```

You can declare a **RadDropDownList** which will be used to change the current user in the following way:

```C#
RadDropDownList dropdown1 = new RadDropDownList();
dropdown1.Location = new Point(10, 10);
this.Controls.Add(dropdown1);
dropdown1.SelectedIndexChanged += this.dropdown1_SelectedIndexChanged;

```

```VB.NET
Dim dropdown1 As New RadDropDownList()
dropdown1.Location = New Point(10, 10)
Me.Controls.Add(dropdown1)
AddHandler dropdown1.SelectedIndexChanged, AddressOf Me.dropdown1_SelectedIndexChanged

```

and populate it like this:

```C#
dropdown1.DataSource = CurrentUsers;
dropdown1.DisplayMember = "Username";
        
```

```VB.NET
dropdown1.DataSource = CurrentUsers
dropdown1.DisplayMember = "Username"

```

Then, when the selected item of the combo changes, the **CurrentUser** of the editor will be updated:

```C#
private void dropdown1_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
    this.radRichTextEditor1.CurrentUser = ((RadDropDownList)sender).SelectedValue as UserInfo;
}

```

```VB.NET
    Private Sub dropdown1_SelectedIndexChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.Data.PositionChangedEventArgs)
        Me.radRichTextEditor1.CurrentUser = TryCast(DirectCast(sender, RadDropDownList).SelectedValue, UserInfo)
    End Sub

```

## Toggling Document Protection

When document protection is enforced, the current user can modify the rights for editing the part of the document that he/she is entitled to editing. When the protection is disabled, the editing rights for the document can be freely modified by all users.

The protection of the document can be turned on and off using the **ToggleDocumentProtectionCommand** command of **RadRichTextEditor**.

The command opens a dialog that allows you to enter a password. By clicking OK, the document protection starts to be enforced.

It is also possible to toggle document protection in code-behind, without showing a dialog:

```C#
this.radRichTextEditor1.Document.ProtectDocument(DocumentProtectionMode.ReadOnly, "password");

```

```VB.NET
Me.radRichTextEditor1.Document.ProtectDocument(DocumentProtectionMode.ReadOnly, "password")

```

The reverse operations can be executed like this:

```C#
this.radRichTextEditor1.Document.UnprotectDocument("password");
    
```

```VB.NET
Me.radRichTextEditor1.Document.UnprotectDocument("password")

```

Note that the **UnprotectDocument** method will succeed only if the password you have passed as parameter matches the one used to protect the document.

## Creating and Modifying Document Protection Regions

**DocumentProtection** regions can be most easily created and modified through the **ChangeEditingPermissionsDialog** which can be invoked through the  **ShowChangeEditingPermissionsDialogCommand** of **RadRichTextEditor**. They can also be edited in code-behind, using the API of **RadRichTextEditor** and **RadDocument**.

Here is an example:

```C#
PermissionRangeInfo info = new PermissionRangeInfo();
info.Type = PermissionType.Individual;
info.Name = this.radRichTextEditor1.CurrentUser.Username;
List<PermissionRangeInfo> infos = new List<PermissionRangeInfo>() { info };
this.radRichTextEditor1.InsertPermissionRange(infos);

```

```VB.NET
Dim info As New PermissionRangeInfo()
info.Type = PermissionType.Individual
info.Name = Me.radRichTextEditor1.CurrentUser.Username
Dim infos As New List(Of PermissionRangeInfo)() From {info}
Me.radRichTextEditor1.InsertPermissionRange(infos)

```

This code will insert a permission range around the current selection in the document. The type of the permission is *Individual* - meaning that only the **CurrentUser** will be able to edit the region.

To change the highlight color of the permission range, we can use the **EnforcedPermissionRangeBrush** property.

```C#
this.radRichTextEditor1.RichTextBoxElement.EnforcedPermissionRangeBrush = new SolidBrush(System.Drawing.Color.Red);
```

```VB.NET
Me.radRichTextEditor1.RichTextBoxElement.EnforcedPermissionRangeBrush = New SolidBrush(System.Drawing.Color.Red)

```

## See Also

* [Positioning](https://www.telerik.com/products/winforms/documentation/controls/richtexteditor/features/positioning)

* [Selection](https://www.telerik.com/products/winforms/documentation/controls/richtexteditor/features/selection)
