Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat: enhance collaborator management by filtering allowed users with… (#6380)

WEI ZHANG41164d70369b
diff --git a/packages/sheets/src/facade/f-permission.ts b/packages/sheets/src/facade/f-permission.ts
index 884d7075f8..bbb4a078bb 100644
--- a/packages/sheets/src/facade/f-permission.ts
+++ b/packages/sheets/src/facade/f-permission.ts
@@ -186,7 +186,31 @@ export class FPermission extends FBase {
// }
let collaborators: ICollaborator[] = [];
if (options?.allowedUsers) {
- collaborators = options.allowedUsers.map((id) => ({ id, role: UnitRole.Editor, subject: undefined }));
+ // Fetch existing collaborators first
+ const existingCollaborators = await this._authzIoService.listCollaborators({
+ objectID: unitId,
+ unitID: unitId,
+ });
+
+ // Create a map of allowed user IDs for easy lookup
+ const allowedUserIds = new Set(options.allowedUsers);
+
+ // Filter and include only the allowed users with complete information
+ collaborators = existingCollaborators
+ .filter((c) => allowedUserIds.has(c.subject?.userID || c.id))
+ .map((c) => ({
+ id: c.id,
+ role: UnitRole.Editor,
+ subject: c.subject,
+ }));
+
+ // Check for any user IDs that weren't found in existing collaborators
+ const foundUserIds = new Set(collaborators.map((c) => c.subject?.userID || c.id));
+ options.allowedUsers.forEach((userId) => {
+ if (!foundUserIds.has(userId)) {
+ console.error(`User ${userId} not found in collaborators list`);
+ }
+ });
}
const permissionId = await this._authzIoService.create({
@@ -308,12 +332,18 @@ export class FPermission extends FBase {
const actionNumber = instance.subType;
// Set strategy: Reader role = deny, Owner role = allow
await this._authzIoService.update({
+ objectType: UnitObject.Worksheet,
objectID: permissionId,
strategies: [{
action: actionNumber,
role: value ? UnitRole.Owner : UnitRole.Reader,
}],
- } as any);
+ unitID: unitId,
+ share: undefined,
+ name: '',
+ scope: undefined,
+ collaborators: undefined,
+ });
}
// Now update permissionPoint, which triggers pointChange$ and _initWorksheetPermissionPointsChange()
// At this point, authzIoService.allowed() will find the override and return the correct value
@@ -389,15 +419,46 @@ export class FPermission extends FBase {
* }]);
* ```
*/
+ // eslint-disable-next-line max-lines-per-function
async addRangeBaseProtection(unitId: string, subUnitId: string, ranges: FRange[], options?: IRangeProtectionOptions): Promise<{
permissionId: string;
ruleId: string;
} | undefined> {
+ // Fetch existing collaborators if allowedUsers is provided
+ let collaborators: ICollaborator[] = [];
+ if (options?.allowedUsers) {
+ // Fetch existing collaborators first
+ const existingCollaborators = await this._authzIoService.listCollaborators({
+ objectID: unitId,
+ unitID: unitId,
+ });
+
+ // Create a map of allowed user IDs for easy lookup
+ const allowedUserIds = new Set(options.allowedUsers);
+
+ // Filter and include only the allowed users with complete information
+ collaborators = existingCollaborators
+ .filter((c) => allowedUserIds.has(c.subject?.userID || c.id))
+ .map((c) => ({
+ id: c.id,
+ role: UnitRole.Editor,
+ subject: c.subject,
+ }));
+
+ // Check for any user IDs that weren't found in existing collaborators
+ const foundUserIds = new Set(collaborators.map((c) => c.subject?.userID || c.id));
+ options.allowedUsers.forEach((userId) => {
+ if (!foundUserIds.has(userId)) {
+ console.error(`User ${userId} not found in collaborators list`);
+ }
+ });
+ }
+
// Create permission ID with collaborators support
const permissionId = await this._authzIoService.create({
objectType: UnitObject.SelectRange,
selectRangeObject: {
- collaborators: options?.allowedUsers?.map((id) => ({ id, role: UnitRole.Editor, subject: undefined })) ?? [],
+ collaborators,
unitID: unitId,
name: options?.name || '',
scope: undefined,
diff --git a/packages/sheets/src/facade/permission/f-worksheet-permission.ts b/packages/sheets/src/facade/permission/f-worksheet-permission.ts
index d6390fe867..429ecd215a 100644
--- a/packages/sheets/src/facade/permission/f-worksheet-permission.ts
+++ b/packages/sheets/src/facade/permission/f-worksheet-permission.ts
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+import type { ICollaborator } from '@univerjs/protocol';
import type {
IRangeProtectionRule,
} from '@univerjs/sheets';
@@ -739,6 +740,7 @@ export class FWorksheetPermission {
* console.log(rules);
* ```
*/
+ // eslint-disable-next-line max-lines-per-function
async protectRanges(
configs: Array<{
ranges: FRange[];
@@ -749,19 +751,52 @@ export class FWorksheetPermission {
throw new Error('Configs cannot be empty');
}
+ // Fetch existing collaborators once if any config has allowedUsers
+ let existingCollaborators: ICollaborator[] = [];
+ const hasAllowedUsers = configs.some((c) => c.options?.allowedUsers?.length);
+ if (hasAllowedUsers) {
+ existingCollaborators = await this._authzIoService.listCollaborators({
+ objectID: this._unitId,
+ unitID: this._unitId,
+ });
+ }
+
// 1. Create permissionId in parallel
const permissionIds = await Promise.all(
- configs.map((c) =>
- this._authzIoService.create({
+ configs.map((c) => {
+ let collaborators: ICollaborator[] = [];
+ if (c.options?.allowedUsers) {
+ // Create a map of allowed user IDs for easy lookup
+ const allowedUserIds = new Set(c.options.allowedUsers);
+
+ // Filter and include only the allowed users with complete information
+ collaborators = existingCollaborators
+ .filter((collab) => allowedUserIds.has(collab.subject?.userID || collab.id))
+ .map((collab) => ({
+ id: collab.id,
+ role: UnitRole.Editor,
+ subject: collab.subject,
+ }));
+
+ // Check for any user IDs that weren't found in existing collaborators
+ const foundUserIds = new Set(collaborators.map((collab) => collab.subject?.userID || collab.id));
+ c.options.allowedUsers.forEach((userId) => {
+ if (!foundUserIds.has(userId)) {
+ console.error(`User ${userId} not found in collaborators list`);
+ }
+ });
+ }
+
+ return this._authzIoService.create({
objectType: UnitObject.SelectRange,
selectRangeObject: {
- collaborators: c.options?.allowedUsers?.map((id) => ({ id, role: UnitRole.Editor, subject: undefined })) ?? [],
+ collaborators,
unitID: this._unitId,
name: c.options?.name || '',
scope: undefined,
},
- })
- )
+ });
+ })
);
// 2. Build rule parameters with proper viewState and editState