Child pages
  • Manage your users
Skip to end of metadata
Go to start of metadata

Built-in user directory

When you create an Apstrata application, you automatically get a user directory, where you can create users and groups. A user of your application is defined by a login (username), a password and a name, which are mandatory fields. Other fields are also available but not required, such as the email or the groups the user belongs to and you are free to add as many custom fields as you need.

Creating users is done by invoking the SaveUser API, retrieve information on a specific user by calling the GetUser API or run sophisticated queries on your users through the invocation of ListUsers.

Using Apstrata's flexible and powerful authorization mechanisms, you can specify what content your users have access to, and what scripts they are able to execute. In addition, whenever you think that a set of users should have the same authorizations, you can create groups in your application and add those users to the group.

Back to the map   Next station: log in your users

Example 1: register a new player

In the mobile game that you are developing, you would like to allow your players to register as users so you can provide registered players with more content and/or game options, which are not accessible to unregistered users. In order to create a new user in your Apstrata user directory, all you need to do is invoke the SaveUser API that will automatically apply validation rules on the data that is sent and detect any duplicates.

 

curl -X POST  -F "https://varick.apstrata.com/apsdb/rest/O763A7F690/SaveUser?apsws.time=1420795837401 
-F apsws.responseType=json
-F login=user12345 
-F name=john doe 
-F password=123456 
-F email=john.doe@mail.com
-F apsws.authSig=1933c4b633b2ad05c40c2bd7c7ad0aa8
... 
// prepare the parameters to provide to the SaveUser API
var params = {
	"login" : request.parameters["login"], // retrieve the value of the "login" parameter from the request
	"name" : request.parameters["name"], // retrieve the value of the "name" parameter from the request
	"password" : request.parameters["password"], // retrieve the value of the "password" parameter from the request
	"email" : request.parameters["email"] // retrieve the value of the "email" parameter from the request
};
// invoke the SaveUser API using the callApi() function of the native apsdb object
// since we do not need to upload a file in this example, we set its files parameter to null
var response = apsdb.callApi("SaveUser", params, null);
return response.metadata;
...
String response = "no response yet";
try {
	
	String authKey = "O763A7F690"; // Replace with your Application key
    String baseURL = "https://varick.apstrata.com/apsdb/rest";
              
    // Sign your call with a signature based on your application key and secret  to authenticate against Apstrata
    // Simple signature is used in this example
	String secret = "S4A0120A6E64952FE75285BD0XD243S2"; // Replace with your application secret; 
	Connection ownerConnection = new OwnerConnection(baseUrl, authKey, secret);
	 
	// Create an instance of the Apstrata Android Client with the above Connection instance
	Client client = new Client(baseURL, authKey, ownerConnection);
	Client.AuthMode mode = Client.AuthMode.SIMPLE;
	List<NameValuePair> parameters = new ArrayList<NameValuePair>();
	
	// Prepare the parameters to send to the SaveUser API    	
	parameters.add(new BasicNameValuePair("login","user12345"));
	parameters.add(new BasicNameValuePair("password","123456"));
	parameters.add(new BasicNameValuePair("name","john.doe"));
	parameters.add(new BasicNameValuePair("email","john.doe@mail.com"));
		
	response = client.callAPIJson(SaveUser, parameters, null, mode);	
} catch (Exception e) {
	response = "Error: " + e.getMessage();
	e.printStackTrace();
}
Try it!

Example 2: allow players to specify the type of games they prefer

In order to improve your users' experience, assume that you decide to allow them to specify their preferences in terms of game types (e.g. arcade, puzzles, card games, etc.) and that you need to store this information in their user profile. Once again, we can resort to the SaveUser API and pass it the "preference" parameter, which is a custom field that you define and that will be added to the user profile. Notice that when updating an existing profile, you need to pass the "apsdb.update" parameter set to "true".

 

curl -X POST  -F "https://varick.apstrata.com/apsdb/rest/O763A7F690/SaveUser?apsws.time=1420795837401 
-F apsws.responseType=json
-F login=user12345 
-F preferences=puzzles
-F preferences=card games
-F preferences.apsdb.fieldType=string
-F apsdb.multivalueAppend=preferences
-F apsdb.update=true
-F apsws.authSig=1933c4b633b2ad05c40c2bd7c7ad0aa8
... 
// prepare the parameters to provide to the SaveUser API
var params = {
	"login" : request.parameters["login"], // retrieve the value of the "login" parameter from the request
	"preferences" : request.parameters["preferences"], // retrieve the value of the "preferences" parameter from the request. Apstrata automatically creates an array if multiple instances of preferences are received
	"preferences.apsdb.fieldType": "string", // since preferences is not a predefined field, we specify its type
	"apsdb.multivalueAppend": "preferences", // By setting this field to "preferences" we inform Apstrata that preferences is an array of values
	"apsdb.update": "true" // We need to pass this parameter to "SaveUser" if we are updating an existing user profile
};
// invoke the SaveUser API using the callApi() function of the native apsdb object
// since we do not need to upload a file in this example, we set its files parameter to null
var response = apsdb.callApi("SaveUser", params, null);
return response.metadata;
...
String response = "no response yet";
try {
	
	String authKey = "O763A7F690"; // Replace with your Application key
    String baseURL = "https://varick.apstrata.com/apsdb/rest";
              
    // Sign your call with a signature based on your user's username and password to authenticate him against Apstrata
    // Simple signature is used in this example
	String password = "someP@55word"; // Replace with the user's password
	String username = "user1@mail.com"; // Replace with the user's login
	Connection userConnection = new UserConnection(baseUrl, authKey, username, password);
	 
	// Create an instance of the Apstrata Android Client with the above Connection instance
	Client client = new Client(baseURL, authKey, userConnection);
	Client.AuthMode mode = Client.AuthMode.SIMPLE;
	List<NameValuePair> parameters = new ArrayList<NameValuePair>();
	
	// Prepare the parameters to send to the SaveUser API    	
	parameters.add(new BasicNameValuePair("login","user12345"));
	parameters.add(new BasicNameValuePair("preferences","puzzles"));
	parameters.add(new BasicNameValuePair("preferences","card games"));
	parameters.add(new BasicNameValuePair("preferences.apsdb.fieldType","string")); // since preferences is not a predefined field, we specify its type
	parameters.add(new BasicNameValuePair("apsdb.multivalueAppend","preferences")); // By setting this field to "preferences" we inform Apstrata that preferences is an array of values
	parameters.add(new BasicNameValuePair("apsdb.update","true"));
		
	response = client.callAPIJson(SaveUser, parameters, null, mode);	
} catch (Exception e) {
	response = "Error: " + e.getMessage();
	e.printStackTrace();
}
Try it!

Example 3: find all players who are interested in puzzles

Building on the preceding example, you might need to get the list of all players that are interested in puzzles so you can promote about your brand new puzzle game. So let us see how to do this:

 

curl "https://varick.apstrata.com/apsdb/rest/O71307F690/ListUsers?&apsws.time=1420795837401
&apsws.authSig=1933c4b633b2ad05c40c2bd7c7ad0aa8
&apsws.responseType=json
&apsws.authMode=simple
&apsdb.attributes=name%2C%20email
&apsdb.query=preferences%20%3D%20%22puzzles%22
&apsdb.resultsPerPage=20
&apsdb.pageNumber=1
&apsdb.count=true&apsdb.resultsPerPage=20
&apsdb.pageNumber=1"
 
... 
var queryParams = {
	"apsdb.query": "preferences = \"puzzles\"", // the apsdb.query field is used to specify the request 
	"apsdb.attributes": "name, email" // the apsdb.attributes field is used to specify what fields from the user profile to return (here is the name and email)
	"apsdb.resultsPerPage": 20, //used for pagination, specify the maximum number of results per call of ListUsers (default is 50)
    "apsdb.pageNumber": "1", // used for pagination, specify the result set to return assuming there are more than one
    "apsdb.count": "true" // if true, specify to return the total count of users matching the request
};
var response = apsdb.callApi("ListUsers", queryParams, null);
if (response.metadata.status == "failure") {
	return response.metadata;
}
return response.result;
...
String response = "no response yet";
try {
	
	String authKey = "O763A7F690"; // Replace with your Application key
    String baseURL = "https://varick.apstrata.com/apsdb/rest";
              
    // Sign your call with a signature based on your application key and secret  to authenticate against Apstrata
    // Simple signature is used in this example
	String secret = "S4A0120A6E64952FE75285BD0XD243S2"; // Replace with your application secret; 
	Connection ownerConnection = new OwnerConnection(baseUrl, authKey, secret);
	 
	// Create an instance of the Apstrata Android Client with the above Connection instance
	Client client = new Client(baseURL, authKey, ownerConnection);
	Client.AuthMode mode = Client.AuthMode.SIMPLE;
	List<NameValuePair> parameters = new ArrayList<NameValuePair>();
	
	// Prepare the parameters to send to the SaveUser API    	
	parameters.add(new BasicNameValuePair("apsdb.query","preferences=\"puzzles\"")); // the apsdb.query field is used to specify the request 
	parameters.add(new BasicNameValuePair("apsdb.attributes","name,email")); // the apsdb.attributes field is used to specify what fields from the user profile to return (here the name and email)
	parameters.add(new BasicNameValuePair("apsdb.resultsPerPage","20")); // used for pagination, specify the maximum number of results per call of ListUsers (default is 50)
	parameters.add(new BasicNameValuePair("apsdb.pageNumber,"1")); // used for pagination, specify the result set to return assuming there are more than one
	parameters.add(new BasicNameValuePair("apsdb.count","true")); // if true, specify to return the total count of users matching the request values

	response = client.callAPIJson(SaveUser, parameters, null, mode);	
} catch (Exception e) {
	response = "Error: " + e.getMessage();
	e.printStackTrace();
}
Try it!

Dig deeper

Related tutorials