A newer version of this documentation is available.

View Latest

User Management

    +
    The Java 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 Java 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
    User user = new User(testUsername).password(testPassword).displayName("Constance Lambert");
    user.roles(
        // Roles required for the reading of data from the bucket
        new Role("data_reader", "*"),
        new Role("query_select", "*"),
        // Roles required for the writing of data into the bucket.
        new Role("data_writer", bucketName),
        new Role("query_insert", bucketName),
        new Role("query_delete", bucketName),
        // Role required for the creation of indexes on the bucket.
        new Role("query_manage_index", bucketName));
    
    cluster.users().upsertUser(user);
    Listing Users
    List<UserAndMetadata> listOfUsers = cluster.users().getAllUsers();
    for (int j = 0; j < listOfUsers.size(); j++) {
      UserAndMetadata currentUser = listOfUsers.get(j);
      System.out.println("User's display name is: " + currentUser.user().displayName() );
      Set<Role> currentRoles = currentUser.user().roles();
      for (Role role : currentRoles) {
        System.out.println("   User has the role: " + role.name() + ", applicable to bucket " + role.bucket() );
      }
    }

    Using a user created in the SDK to access data:

    ClusterEnvironment environment = ClusterEnvironment.builder().build();
    Cluster userCluster = Cluster.connect(connectionString,
        ClusterOptions.clusterOptions(testUsername, testPassword).environment(environment));
    Bucket userBucket = userCluster.bucket(bucketName);
    Collection userCollection = userBucket.defaultCollection();
    
    cluster.queryIndexes().createPrimaryIndex(bucketName, // create index if needed
        CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions().ignoreIfExists(true));
    
    JsonObject returnedAirline10doc = userCollection.get("airline_10").contentAsObject();
    
    JsonObject airline11Object = JsonObject.create().put("callsign", "MILE-AIR").put("iata", "Q5").put("icao", "MLA")
        .put("id", 11).put("name", "40-Mile Air").put("type", "airline");
    
    userCollection.upsert("airline_11", airline11Object);
    
    JsonObject returnedAirline11Doc = userCollection.get("airline_11").contentAsObject();
    
    QueryResult result = userCluster.query("SELECT * FROM `travel-sample` LIMIT 5");
    
    userCluster.disconnect();

    Further Reading

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