1<?php 2 3// Access the cluster that is running on the local host, authenticating with 4// the username and password of the Full Administrator. This 5// provides all privileges. 6 7$cluster = new \Couchbase\Cluster('couchbase://localhost'); 8 9// Make sure that 'default' bucket exists. 10echo("Authenticating as administrator.\n"); 11$cluster->authenticateAs("Administrator", "password"); 12 13 14// Create a user and assign roles. 15echo("Upserting new user.\n"); 16$userSettings = new \Couchbase\UserSettings(); 17$userSettings 18 ->password('cbtestuserpwd') 19 // Roles required for the reading of data from the bucket. 20 ->role('data_reader', 'travel-sample') 21 ->role('query_select', 'travel-sample') 22 // Roles required for the writing data into the bucket. 23 ->role('data_writer', 'travel-sample') 24 ->role('query_insert', 'travel-sample') 25 ->role('query_delete', 'travel-sample') 26 // Role required for the creation of indexes on the bucket. 27 ->role('query_manage_index', 'travel-sample'); 28$cluster->manager()->upsertUser('cbtestuser', $userSettings); 29 30 31// List current users. 32echo("Listing current users.\n"); 33$listOfUsers = $cluster->manager()->listUsers(); 34for ($j = 0; $j < count($listOfUsers); $j++) { 35 $currentUser = $listOfUsers[$j]; 36 37 echo("\n\nUSER #" . $j .":\n\n"); 38 39 if (array_key_exists('name', $currentUser)) { 40 echo("User's name is: " . $currentUser['name'] . "\n"); 41 } 42 echo("User's id is: " . $currentUser['id'] . "\n"); 43 echo("User's domain is: " . $currentUser['domain'] . "\n"); 44 45 $currentRoles = $currentUser['roles']; 46 47 for ($i = 0; $i < count($currentRoles); $i++) { 48 echo("User has the role: " . $currentRoles[$i]['role'] . ", applicable to bucket " . 49 $currentRoles[$i]['bucket_name'] . "\n"); 50 } 51} 52 53 54// Access the cluster that is running on the local host, specifying 55// the username and password already assigned by the administrator 56echo("Authenticating as user.\n"); 57$cluster->authenticateAs("cbtestuser", "cbtestuserpwd"); 58 59// Open a known, existing bucket (created by the administrator). 60echo("Opening travel-sample bucket as user.\n"); 61$travelSample = $cluster->openBucket("travel-sample"); 62 63// Create a N1QL Primary Index (but ignore if one already exists). 64$travelSample->manager()->createN1qlPrimaryIndex('', true, false); 65 66// Read out a known, existing document within the bucket (created 67// by the administrator). 68echo("Reading out airline_10 document\n"); 69$returnedAirline10doc = $travelSample->get("airline_10"); 70echo("Found: \n"); 71print_r($returnedAirline10doc); 72 73// Create a new document. 74echo("Creating new document as user.\n"); 75$airline11Object = [ 76 "callsign" => "MILE-AIR", 77 "iata" => "Q5", 78 "icao" => "MLA", 79 "id" => 11, 80 "name" => "40-Mile Air", 81 "type" => "airline" 82]; 83 84// Upsert the document to the bucket. 85echo("Upserting new document as user.\n"); 86$travelSample->upsert("airline_11", $airline11Object); 87 88echo("Reading out airline11Document as user.\n"); 89$returnedAirline11Doc = $travelSample->get("airline_11"); 90echo("Found: \n"); 91print_r($returnedAirline11Doc); 92 93// Perform a N1QL Query. 94echo("Performing query as user.\n"); 95 96$result = $travelSample->query( 97 \Couchbase\N1qlQuery::fromString("SELECT * FROM `travel-sample` LIMIT 5") 98); 99 100echo("Query-results are:\n"); 101// Print each row returned by the query. 102foreach ($result->rows as $row) { 103 print_r($row); 104} 105 106// Access the cluster that is running on the local host, authenticating with 107// the username and password of the Full Administrator. This 108// provides all privileges. 109echo("Re-authenticating as administrator.\n"); 110$cluster->authenticateAs("Administrator", "password"); 111 112// Remove known user. 113echo("Removing user as administrator.\n"); 114$userToBeRemoved = "cbtestuser"; 115try { 116 $cluster->manager()->removeUser($userToBeRemoved); 117 echo("Deleted user " . $userToBeRemoved . ".\n"); 118} catch (\Couchbase\Exception $ex) { 119 echo("Could not delete user " . $userToBeRemoved . ".\n"); 120} 121