User Management

  • how-to
    +
    The Node.js SDK lets you create users, assign them roles and associated privileges, and remove them from the system.

    User-Management APIs

    Users who have been assigned the Admin role for the cluster are able to create, edit, and remove users. The Node.js SDK provides APIs to support these activities. A high-level summary of the APIs can be found in User-Management, and details of all options in the UserManager API docs.

    Using the UserManager API

    The most common uses of the UserManager API are creating and listing users:

    Creating Users
    const userMgr = clusterAdm.users();
    
    await userMgr.upsertUser({
        username: testUsername,
        password: testPassword,
        displayName: "Constance Lambert",
        roles: [
            // Roles required for the reading of data from the bucket
            { name: "data_reader", bucket: "*" },
            { name: "query_select", bucket: "*" },
            
            // Roles required for the writing of data into the bucket. 
            { name: "data_writer", bucket: bucketName },
            { name: "query_insert", bucket: bucketName },
            { name: "query_delete", bucket: bucketName },
            
            // Role required for the creation of indexes on the bucket.
            { name: "query_manage_index", bucket: bucketName }
        ]
    })
    Listing Users
    const listOfUsers = await clusterAdm.users().getAllUsers();
    
    for (const currentUser of listOfUsers) {
        console.log(`User's display name is: ${ currentUser.displayName }`);
        const currentRoles = currentUser.effectiveRoles;
        for (const role of currentRoles) {
            console.log(`   User has the role: ${ role.name }, applicable to bucket ${ role.bucket }`);
        }
    }

    Using a user created in the SDK to access data:

    const userCluster = await couchbase.connect(
        "couchbase://localhost", {
        username: testUsername,
        password: testPassword,
    })
    const bucket = userCluster.bucket(bucketName)
    const scope = bucket.scope("inventory")
    const collection = scope.collection("airline")
    
    await collection.upsert(
        "airline_11", {
            callsign: "MILE-AIR",
            iata: "Q5",
            icao: "MLA",
            id: 11,
            name: "40-Mile Air",
            type: "airline"
        }
    )
    userCluster.close()

    Further Reading

    The SDK also contains management APIs for dealing with Cluster resources.