Skip to content

Commit a0154e1

Browse files
Bold Create Agentsuriyaprakasamr
andcommitted
Implement user-based document editing and restriction workflow sample
Co-authored-by: suriyaprakasamr <302142999+suriyaprakasamr@users.noreply.github.com> Bold-Create-Session: https://app.boldcreate.ai/agent/chat/?threadId=9d3d6252-eff3-44c5-a6d4-a92b3677d941
1 parent b8033ca commit a0154e1

5 files changed

Lines changed: 371 additions & 0 deletions

File tree

‎document-protection/README.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# User-based restrict editing and approval workflow in JavaScript (ES5) DOCX Editor (Document Editor)
2+
3+
This sample demonstrates the user-based document restrictions and approval workflows of the [Syncfusion&reg; JavaScript DOCX Editor](https://www.syncfusion.com/docx-editor-sdk/javascript-docx-editor?utm_source=github&utm_medium=github&utm_campaign=github-javascript-es5-docx-editor-examples) (Document Editor). The loaded Adventure Works Cycles document (`data-document-protection.js`) contains user-based editable ranges for Nancy's price values, and each user's restrictions are applied on the fly when a user is selected from the dropdown.
4+
5+
## Demo
6+
7+
* **Nancy** (nancy@example.com) – Price Editor: can edit only the price values that are marked as editable ranges for her in the loaded document.
8+
* **Andrew** (andrew@example.com) – Reviewer: all edits are tracked as revisions (`RevisionsOnly` protection), and he cannot accept or reject revisions.
9+
* **Steven** (steven@example.com) – Commenter: can insert comments only (`CommentsOnly` protection) and cannot edit the document content.
10+
* **Michael** (michael@example.com) – Editor: full editing access on the entire document (no protection applied) and reviews the tracked changes.
11+
* **Laura** (laura@example.com) – Viewer: read-only access to the document.
12+
13+
Select a user in the dropdown on the right to switch the document restrictions between the roles, and use the table below the dropdown to see each user's permissions at a glance.
14+
15+
![User-based-restriction-in-JavaScript-DOCX-Editor](./images/User-based-restriction-in-JavaScript-DOCX-Editor.gif)
16+
17+
## How it works
18+
19+
1. The sample loads the Adventure Works Cycles SFDT document from `data-document-protection.js`. The attached SFDT already holds the user-based editable ranges (Nancy's price values) along with a ReadOnly protection (hash and salt), so it opens in the protected mode.
20+
2. When a user is selected, the previous document protection is stopped and the protection type of the new user is enforced through `documentEditor.editorModule.enforceProtection`:
21+
* `RevisionsOnly` – for Andrew, so all his edits are tracked.
22+
* `CommentsOnly` – for Steven, so he can only add comments.
23+
* `NoProtection` – for Michael, so he has full editing access.
24+
* `ReadOnly` – for Nancy (only her editable ranges can be edited) and Laura (no editable ranges, so the document is fully read-only).
25+
3. For Laura, the container is also set to read-only (`documentEditor.isReadOnly = true`), so the document content cannot be modified.
26+
4. The approval workflow is demonstrated with the `beforeAcceptRejectChanges` event of the Document Editor: Andrew's accept/reject actions are cancelled, so the tracked changes can only be approved by a user with approval rights (Michael).
27+
28+
## How to run this sample
29+
30+
1. Clone the [JavaScript (ES5) DOCX Editor examples](https://github.com/SyncfusionExamples/javascript-es5-docx-editor-examples) repository.
31+
2. Navigate to the `document-protection` directory.
32+
3. Since this sample is built on plain HTML and JavaScript (ES5), no build tools or package installation are required. Open `index.html` in a web browser, or serve it using any local static file server.
33+
4. Select a user in the dropdown to apply that user's document restrictions:
34+
* Nancy – try editing a price value (allowed) and any other text (blocked).
35+
* Andrew – type anywhere; the edits appear as tracked changes, and the accept/reject actions are cancelled.
36+
* Steven – insert comments from the Review tab; editing the content is blocked.
37+
* Michael – edit and format anywhere, and accept or reject Andrew's revisions.
38+
* Laura – the document is read-only; the content cannot be modified.
39+
40+
> Note: The sample uses the Syncfusion demo web service (`https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/`) for demo purposes. For production, set up your own web service as described in the [web services documentation](https://help.syncfusion.com/document-processing/word/word-processor/javascript-es6/web-services-overview?utm_source=github&utm_medium=listing&utm_campaign=github-javascript-es5-docx-editor-examples).

‎document-protection/data-document-protection.js‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.35 MB
Loading

‎document-protection/index.html‎

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<title>User based restrict editing and approval workflow - JavaScript (ES5) DOCX Editor</title>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
7+
<meta name="description" content="Sample demonstrating user-based document restrictions and approval workflows with the Syncfusion JavaScript (ES5) DOCX Editor" />
8+
<meta name="author" content="Syncfusion" />
9+
<!-- EJ2 Document Editor dependent theme -->
10+
<link href="https://cdn.syncfusion.com/ej2/33.2.3/ej2-base/styles/tailwind3.css" rel="stylesheet" type="text/css" rel="nofollow" />
11+
<link href="https://cdn.syncfusion.com/ej2/33.2.3/ej2-buttons/styles/tailwind3.css" rel="stylesheet" type="text/css" rel="nofollow" />
12+
<link href="https://cdn.syncfusion.com/ej2/33.2.3/ej2-inputs/styles/tailwind3.css" rel="stylesheet" type="text/css" rel="nofollow" />
13+
<link href="https://cdn.syncfusion.com/ej2/33.2.3/ej2-popups/styles/tailwind3.css" rel="stylesheet" type="text/css" rel="nofollow" />
14+
<link href="https://cdn.syncfusion.com/ej2/33.2.3/ej2-lists/styles/tailwind3.css" rel="stylesheet" type="text/css" rel="nofollow" />
15+
<link href="https://cdn.syncfusion.com/ej2/33.2.3/ej2-navigations/styles/tailwind3.css" rel="stylesheet" type="text/css" rel="nofollow" />
16+
<link href="https://cdn.syncfusion.com/ej2/33.2.3/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet" type="text/css" rel="nofollow" />
17+
<link href="https://cdn.syncfusion.com/ej2/33.2.3/ej2-dropdowns/styles/tailwind3.css" rel="stylesheet" type="text/css" rel="nofollow" />
18+
<!-- EJ2 Document Editor theme -->
19+
<link href="https://cdn.syncfusion.com/ej2/33.2.3/ej2-documenteditor/styles/tailwind3.css" rel="stylesheet" type="text/css" rel="nofollow" />
20+
<!-- EJ2 Document Editor dependent scripts -->
21+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
22+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-data/dist/global/ej2-data.min.js" type="text/javascript"></script>
23+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-svg-base/dist/global/ej2-svg-base.min.js" type="text/javascript"></script>
24+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-file-utils/dist/global/ej2-file-utils.min.js" type="text/javascript"></script>
25+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-compression/dist/global/ej2-compression.min.js" type="text/javascript"></script>
26+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-pdf-export/dist/global/ej2-pdf-export.min.js" type="text/javascript"></script>
27+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-buttons/dist/global/ej2-buttons.min.js" type="text/javascript"></script>
28+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-popups/dist/global/ej2-popups.min.js" type="text/javascript"></script>
29+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-splitbuttons/dist/global/ej2-splitbuttons.min.js" type="text/javascript"></script>
30+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
31+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-lists/dist/global/ej2-lists.min.js" type="text/javascript"></script>
32+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-navigations/dist/global/ej2-navigations.min.js" type="text/javascript"></script>
33+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-dropdowns/dist/global/ej2-dropdowns.min.js" type="text/javascript"></script>
34+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-calendars/dist/global/ej2-calendars.min.js" type="text/javascript"></script>
35+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-charts/dist/global/ej2-charts.min.js" type="text/javascript"></script>
36+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-office-chart/dist/global/ej2-office-chart.min.js" type="text/javascript"></script>
37+
<!-- EJ2 Document Editor script -->
38+
<script src="https://cdn.syncfusion.com/ej2/33.2.3/ej2-documenteditor/dist/global/ej2-documenteditor.min.js" type="text/javascript"></script>
39+
40+
<style>
41+
body {
42+
margin: 0;
43+
font-family: 'Segoe UI', Helvetica, Arial, sans-serif;
44+
background-color: #f4f5f7;
45+
}
46+
47+
.sample-header {
48+
padding: 16px 24px 8px;
49+
background-color: #ffffff;
50+
border-bottom: 1px solid #e0e0e0;
51+
}
52+
53+
.sample-header h1 {
54+
margin: 0 0 4px;
55+
font-size: 20px;
56+
font-weight: 600;
57+
color: #1a202c;
58+
}
59+
60+
.sample-header p {
61+
margin: 0;
62+
font-size: 13px;
63+
color: #5f6b7a;
64+
}
65+
66+
.sample-toolbar {
67+
display: flex;
68+
align-items: center;
69+
gap: 10px;
70+
padding: 10px 24px;
71+
background-color: #ffffff;
72+
border-bottom: 1px solid #e0e0e0;
73+
}
74+
75+
.sample-toolbar label {
76+
font-size: 13px;
77+
font-weight: 600;
78+
color: #1a202c;
79+
}
80+
81+
.sample-toolbar select {
82+
min-width: 160px;
83+
height: 30px;
84+
padding: 2px 6px;
85+
border: 1px solid #c3cdd5;
86+
border-radius: 4px;
87+
font-size: 13px;
88+
}
89+
90+
.permission-banner {
91+
font-size: 12px;
92+
color: #5f6b7a;
93+
}
94+
95+
.sample-content {
96+
display: flex;
97+
align-items: flex-start;
98+
}
99+
100+
.editor-holder {
101+
flex: 1;
102+
min-width: 0;
103+
}
104+
105+
.permission-holder {
106+
flex: 1;
107+
padding: 0;
108+
}
109+
110+
.permission-table {
111+
width: 100%;
112+
border-collapse: collapse;
113+
font-size: 12px;
114+
}
115+
116+
.permission-table th,
117+
.permission-table td {
118+
padding: 6px 8px;
119+
border: 1px solid #e0e0e0;
120+
text-align: left;
121+
vertical-align: top;
122+
}
123+
124+
.permission-table th {
125+
background-color: #f4f5f7;
126+
font-weight: 600;
127+
color: #1a202c;
128+
}
129+
130+
.permission-table td:nth-child(3) {
131+
word-break: break-all;
132+
}
133+
</style>
134+
</head>
135+
<body>
136+
<div class="sample-header">
137+
<h1>User based restrict editing and approval workflow in JavaScript DOCX Editor</h1>
138+
<p>
139+
Select a user to switch between role-based editing restrictions: price-only editing with editable
140+
ranges, tracked changes review, comments-only review, full editing and read-only access.
141+
</p>
142+
</div>
143+
<div class="sample-toolbar">
144+
<div class="permission-holder">
145+
<table class="permission-table">
146+
<thead>
147+
<tr>
148+
<th>User</th>
149+
<th>Role</th>
150+
<th>Email</th>
151+
<th>Permissions</th>
152+
</tr>
153+
</thead>
154+
<tbody>
155+
<tr>
156+
<td>Nancy</td>
157+
<td>Price Editor</td>
158+
<td>nancy@example.com</td>
159+
<td>Can edit only the price values (marked editable ranges)</td>
160+
</tr>
161+
<tr>
162+
<td>Andrew</td>
163+
<td>Reviewer</td>
164+
<td>andrew@example.com</td>
165+
<td>All edits are tracked; cannot accept/reject revisions</td>
166+
</tr>
167+
<tr>
168+
<td>Steven</td>
169+
<td>Commenter</td>
170+
<td>steven@example.com</td>
171+
<td>Can insert comments only; cannot edit content</td>
172+
</tr>
173+
<tr>
174+
<td>Michael</td>
175+
<td>Editor</td>
176+
<td>michael@example.com</td>
177+
<td>Full editing (approves the tracked changes)</td>
178+
</tr>
179+
<tr>
180+
<td>Laura</td>
181+
<td>Viewer</td>
182+
<td>laura@example.com</td>
183+
<td>Read-only access to the document</td>
184+
</tr>
185+
</tbody>
186+
</table>
187+
</div>
188+
</div>
189+
<div class="sample-toolbar">
190+
<label for="userSelect">Signed in user:</label>
191+
<select id="userSelect" title="Choose the user whose permissions are applied"></select>
192+
<span id="permissionBanner" class="permission-banner"></span>
193+
</div>
194+
<div class="sample-content">
195+
<div class="editor-holder">
196+
<!-- Element which will render as Document Editor -->
197+
<div id="DocumentEditor"></div>
198+
</div>
199+
</div>
200+
201+
<script src="data-document-protection.js" type="text/javascript"></script>
202+
<script src="index.js" type="text/javascript"></script>
203+
</body>
204+
</html>

‎document-protection/index.js‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
var users = [
2+
{
3+
name: 'Nancy',
4+
email: 'nancy@example.com',
5+
role: 'Price Editor',
6+
protectionType: 'ReadOnly',
7+
description: 'Can edit only the price values (editable ranges marked in the document)'
8+
},
9+
{
10+
name: 'Andrew',
11+
email: 'andrew@example.com',
12+
role: 'Reviewer',
13+
protectionType: 'RevisionsOnly',
14+
description: 'All edits are tracked as revisions; cannot accept or reject revisions'
15+
},
16+
{
17+
name: 'Steven',
18+
email: 'steven@example.com',
19+
role: 'Commenter',
20+
protectionType: 'CommentsOnly',
21+
description: 'Can insert comments only; cannot edit document content'
22+
},
23+
{
24+
name: 'Michael',
25+
email: 'michael@example.com',
26+
role: 'Editor',
27+
protectionType: 'NoProtection',
28+
description: 'Full editing access on the entire document'
29+
},
30+
{
31+
name: 'Laura',
32+
email: 'laura@example.com',
33+
role: 'Viewer',
34+
protectionType: 'ReadOnly',
35+
description: 'Read-only access to the document'
36+
}
37+
];
38+
39+
// Injects the dependent modules into DocumentEditorContainer
40+
ej.documenteditor.DocumentEditorContainer.Inject(
41+
ej.documenteditor.Toolbar,
42+
ej.documenteditor.Ribbon
43+
);
44+
45+
// Initializes the DocumentEditorContainer component
46+
var container = new ej.documenteditor.DocumentEditorContainer({
47+
enableToolbar: true,
48+
toolbarMode: 'Ribbon',
49+
height: '590px',
50+
// Use the following service URL only for demo purposes
51+
serviceUrl: 'https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/'
52+
});
53+
54+
// Renders the DocumentEditorContainer component
55+
container.appendTo('#DocumentEditor');
56+
57+
var documentEditor = container.documentEditor;
58+
var currentUser = users[0];
59+
60+
documentEditor.open(JSON.stringify(documentProtectionData));
61+
applyUserPermissions(currentUser);
62+
63+
var userSelect = document.getElementById('userSelect');
64+
users.forEach(function (user) {
65+
var option = document.createElement('option');
66+
option.value = user.email;
67+
option.text = user.name;
68+
userSelect.appendChild(option);
69+
});
70+
71+
userSelect.addEventListener('change', function () {
72+
var selected = users.find(function (user) {
73+
return user.email === userSelect.value;
74+
});
75+
if (selected) {
76+
currentUser = selected;
77+
applyUserPermissions(currentUser);
78+
}
79+
});
80+
81+
/**
82+
* Enforces the document protection for the selected user.
83+
* The Document Editor checks `documentHelper.isDocumentProtected` along with
84+
* the editable ranges of `currentUser`, so switching the user re-applies the
85+
* restrictions of the loaded document instantly.
86+
*/
87+
function applyUserPermissions(user) {
88+
// Clear the previous protection before applying the new user's rights
89+
if (documentEditor.documentHelper.isDocumentProtected) {
90+
documentEditor.editorModule.unProtectDocument();
91+
}
92+
// Reset read-only restriction of the container (used for Laura)
93+
documentEditor.isReadOnly = false;
94+
documentEditor.currentUser = user.email;
95+
if (user.protectionType !== 'NoProtection') {
96+
// Enforces ReadOnly, RevisionsOnly or CommentsOnly without a password
97+
documentEditor.editorModule.enforceProtection('', user.protectionType);
98+
}
99+
if (user.email === 'laura@example.com') {
100+
// Laura: read-only access to the document
101+
documentEditor.isReadOnly = true;
102+
}
103+
// Close the built-in restrict editing pane; the sample shows the effective
104+
// permissions in its own panel on the right
105+
documentEditor.documentHelper.restrictEditingPane.showHideRestrictPane(false);
106+
updatePermissionBanner(user);
107+
}
108+
109+
/**
110+
* Andrew works in `RevisionsOnly` protection: every edit is tracked, but the
111+
* built-in accept/reject actions are blocked, so a reviewer with approval
112+
* rights (e.g. Michael, the full editor) reviews and approves the changes.
113+
*/
114+
documentEditor.beforeAcceptRejectChanges = function (args) {
115+
if (currentUser.email === 'andrew@example.com') {
116+
// Cancel the accept/reject action for Andrew
117+
args.cancel = true;
118+
}
119+
};
120+
121+
function updatePermissionBanner(user) {
122+
var banner = document.getElementById('permissionBanner');
123+
if (banner) {
124+
banner.textContent = 'Signed in as ' + user.name + ' (' + user.email + ') - ' + user.role;
125+
}
126+
}

0 commit comments

Comments
 (0)