User Management

  • how-to
    The PHP 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 PHP 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
    $roles = [
        // Roles required for reading data from bucket
        // Roles required for writing data to bucket
        // Roles required for idx creation on bucket
    $user = new User();
    $user->setDisplayName("Test User");
    Listing Users
    $userMetadata = $userMgr->getAllUsers();
    foreach ($userMetadata as &$u) {
        printf("User's display name: %s\n", $u->user()->displayName());
        $userRoles = $u->user()->roles();
        foreach ($userRoles as &$role) {
            printf("\tUser has role %s, applicable to bucket %s\n", $role->name(), $role->bucket());

    Using a user created in the SDK to access data:

    $options = new ClusterOptions();
    $options->credentials("test-user", "test-passw0rd!");
    $userCluster = new Cluster("couchbase://localhost", $options);
    # For Server versions 6.5 or later you do not need to open a bucket here
    $userBucket = $userCluster->bucket("travel-sample");
    $collection = $userBucket->defaultCollection();
    # create primary idx for testing purposes
    $createPrimaryQueryIndexOpts = new CreateQueryPrimaryIndexOptions();
    $userCluster->queryIndexes()->createPrimaryIndex("travel-sample", $createPrimaryQueryIndexOpts);
    # test k/v operations
    $airline10 = $collection->get("airline_10");
    printf("Airline 10: %s", $airline10->contentAs(RawJsonTranscoder::getInstance()));
    $airline11 = [
        "callsign" => "MILE-AIR",
        "iata" => "Q5", "id" => 11,
        "name" => "40-Mile Air",
        "type" => "airline"
    $collection->upsert("airline11", $airline11);
    # test query operations
    $queryResult = $userCluster->query("SELECT * FROM `travel-sample` LIMIT 5;");
    foreach ($queryResult->rows() as &$row) {
        print("\nQuery Row:\n");

    Further Reading

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