Skip to main content

Manage Group

In zkDatabase, managing users within a group or database is an essential part of permission and access control. The methods userAdd and userRemove allow the database owner or an authorized user to add or remove users from a database or group. These operations help streamline the management of user access and permissions.

userAdd: Adding Users to Group​

The userAdd method allows you to add a list of users to a group. This is useful for granting users access to specific resources within the zkDatabase.

Definition​

const result = await zkdb.db(databaseName).group('group-name').userAdd(userNames);

Parameters​

  • userNames string[]: An array of usernames that you want to add to the group.

Returns​

  • A Result that can be checked with .isOk() / .isErr().

Example​

const dbTest = zkdb.db('my-db');
const editorsGroup = dbTest.group('editors');

// Add users to group
const users = (await dbTest.userList()).unwrap();
if (users.data.length > 0) {
const userNames = users.data.map((u) => u.userName).slice(0, 2);
(await editorsGroup.userAdd(userNames)).unwrap();
console.log('Added users:', userNames);
}

userRemove: Removing Users from a Group​

The userRemove method allows you to remove a list of users from a group. This is useful for revoking access when a user is no longer authorized.

Definition​

const result = await zkdb.db(databaseName).group(groupName).userRemove(userNames);

Parameters​

  • userNames (Array of Strings): An array of usernames that you want to remove from the group.

Returns​

  • A Result that can be checked with .isOk() / .isErr().

Example​

const dbTest = zkdb.db('my-db');
const editorsGroup = dbTest.group('editors');

// Remove users from group
const result = await editorsGroup.userRemove(['alice', 'bob']);

if (result.isOk()) {
console.log('Users successfully removed.');
} else {
console.log('Failed to remove users:', result.unwrapErr().message);
}

Important Notes​

  • Permission Requirements: Only the database owner or a user with the appropriate permissions can call userAdd and userRemove to modify user access. Attempting to call these methods without the proper permissions will result in an error.

  • Error Handling: Both methods return a Promise<Result<boolean>>. Instead of try...catch, you should check if the operation was successful using .isErr() and handle the error message via .unwrapErr().message.

  • Batch Operations: Both userAdd and userRemove work with an array of usernames, allowing you to manage multiple users at once for efficiency.

Use Cases​

  • Group Management: These methods are useful for managing access to specific groups within a database. For example, you can use userAdd to grant a group of employees access to a specific resource or userRemove to revoke access from users who are no longer part of a project.

  • Security and Access Control: By adding and removing users, you can maintain tight control over who can access sensitive data, ensuring that only authorized users have permissions to interact with the database or its contents.

Summary​

The userAdd and userRemove methods in zkDatabase provide a simple, effective way to manage user access within a database. With the ability to handle multiple users at once, these methods allow for efficient group and permission management, ensuring that your database remains secure and accessible only to authorized users.