New to Telerik UI for WinForms? Download free 30-day trial

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 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:

this.radRichTextEditor1.Users = new PermissionInfoCollection()
    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"),

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.

this.radRichTextEditor1.CurrentUser = new UserInfo("Users", "James Miller", "jmiller", "");

Me.radRichTextEditor1.CurrentUser = New UserInfo("Users", "James Miller", "jmiller", "")

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:

List<UserInfo> CurrentUsers = new List<UserInfo>()
    new UserInfo("Users", "James Miller", "jmiller", ""),
    new UserInfo("Administrators", "John Smith", "jsmith", ""),
    new UserInfo("Administrators", "Robert Brown", "rbrown", ""),

Dim CurrentUsers As New List(Of UserInfo)() From {
    New UserInfo("Users", "James Miller", "jmiller", ""),
    New UserInfo("Administrators", "John Smith", "jsmith", ""),
    New UserInfo("Administrators", "Robert Brown", "rbrown", "")

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

RadDropDownList dropdown1 = new RadDropDownList();
dropdown1.Location = new Point(10, 10);
dropdown1.SelectedIndexChanged += this.dropdown1_SelectedIndexChanged;

Dim dropdown1 As New RadDropDownList()
dropdown1.Location = New Point(10, 10)
AddHandler dropdown1.SelectedIndexChanged, AddressOf Me.dropdown1_SelectedIndexChanged

and populate it like this:

dropdown1.DataSource = CurrentUsers;
dropdown1.DisplayMember = "Username";

dropdown1.DataSource = CurrentUsers
dropdown1.DisplayMember = "Username"

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

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

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:

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

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

The reverse operations can be executed like this:



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:

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

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

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.

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

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

See Also

In this article