Was this helpful?

See all application entities

You can add users to groups from client code using cURL commands or one of the SDKs, as described here.

When setting up your application on the server, you might find it easier and more convenient to create and populate groups with the admin portal. There, you can create groups, create roles, and define permission rules that govern user access to data and services in your application. For more information, see App security.

Use the POST method to add a user to a group. If the named group does not yet exist, an error message is returned.

Request syntax

curl -X POST https://api.usergrid.com/<org_id>/<app_id>/groups/<uuid | groupname>/users/<uuid | username>

Request URI

POST /<org_id>/<app_id>/groups/<uuid | groupname>/users/<uuid | username>

Parameters

Parameter Description
arg uuid | string org_id Organization UUID or organization name
arg uuid | string app_id Application UUID or application name
arg uuid | string groupname UUID or name of the group
arg uuid | string username UUID or username of user

Example

Note: Although not shown in the API examples below, you need to provide a valid access token with each API call. See Authenticating users and application clients for details.

Request

curl -X POST "https://api.usergrid.com/my-org/my-app/groups/mynewgroup/users/john.doe"

Response

{
    "action": "post",
    "application": "7fb8d891-477d-11e1-b2bd-22000a1c4e22",
    "params": {},
    "path": "/groups/a668717b-67cb-11e1-8223-12313d14bde7/users",
    "uri": "https://api.usergrid.com/22000a1c4e22-7fb8d891-477d-11e1-b2bd/7fb8d891-477d-11e1-b2bd-22000a1c4e22/groups/a668717b-67cb-11e1-8223-12313d14bde7/users",
    "entities": [{
        "uuid": "6fbc8157-4786-11e1-b2bd-22000a1c4e22",
        "type": "user",
        "nanme": "John Doe",
        "created": 1327517852364015,
        "modified": 1327517852364015,
        "activated": true,
        "email": "john.doe@mail.com",
        "metadata": {
            "connecting": {
                "owners":"/groups/a668717b-67cb-11e1-8223-12313d14bde7/users/6fbc8157-4786-11e1-b2bd-22000a1c4e22/connecting/owners"
            },
            "path": "/groups/a668717b-67cb-11e1-8223-12313d14bde7/users/6fbc8157-4786-11e1-b2bd-22000a1c4e22",
            "sets": {
                "rolenames": "/groups/a668717b-67cb-11e1-8223-12313d14bde7/users/6fbc8157-4786-11e1-b2bd-22000a1c4e22/rolenames",
                "permissions": "/groups/a668717b-67cb-11e1-8223-12313d14bde7/users/6fbc8157-4786-11e1-b2bd-22000a1c4e22/permissions"
            },
            "collections":{
                "activities":"/groups/a668717b-67cb-11e1-8223-12313d14bde7/users/6fbc8157-4786-11e1-b2bd-22000a1c4e22/activities",
                "devices": "/groups/a668717b-67cb-11e1-8223-12313d14bde7/users/6fbc8157-4786-11e1-b2bd-22000a1c4e22/devices",
                "feed":"/groups/a668717b-67cb-11e1-8223-12313d14bde7/users/6fbc8157-4786-11e1-b2bd-22000a1c4e22/feed",
                "groups": "/groups/a668717b-67cb-11e1-8223-12313d14bde7/users/6fbc8157-4786-11e1-b2bd-22000a1c4e22/groups",
                "roles":"/groups/a668717b-67cb-11e1-8223-12313d14bde7/users/6fbc8157-4786-11e1-b2bd-22000a1c4e22/roles",
                "following": "/groups/a668717b-67cb-11e1-8223-12313d14bde7/users/6fbc8157-4786-11e1-b2bd-22000a1c4e22/following",
                "followers": "/groups/a668717b-67cb-11e1-8223-12313d14bde7/users/6fbc8157-4786-11e1-b2bd-22000a1c4e22/followers"
            }
        },
        "picture": "https://www.gravatar.com/avatar/90f823ba15655b8cc8e3b4d63377576f",
        "username": "john.doe"
    }],
    "timestamp": 1331066031380,
    "duration": 64,
    "organization" : "my-org",
    "applicationName": "my-app"
}

These are ApigeeDataClient methods for adding users to groups.

Synchronous

-(ApigeeClientResponse *)addUserToGroup:(NSString *)userID
                                  group:(NSString *)groupID;

Asynchronous

-(ApigeeClientResponse *)addUserToGroup:(NSString *)userID
                                  group:(NSString *)groupID
                      completionHandler:(ApigeeDataClientCompletionHandler)completionHandler;

Parameters

Parameter Description
completionHandler A handler to receive the response from an asynchronous call.
groupId A path, optionally showing hierarchical relationships. This must be unique in your data store.
userId The ID for the user to add.

Example

This example uses the iOS SDK.

NSString *groupId = @"mygroup";
NSString *userName = "joe";

// Add the user through a call to an SDK method.
[[self.apigeeClient dataClient] addUserToGroup:userName group:self.groupId
    completionHandler:^(ApigeeClientResponse *response)
{
    if ([response completedSuccessfully]) {
        // This might be a good place to show the 
        // updated list of users in the group.
    } else {
        // You might log or display a message here.
    }
}];

These are DataClient methods for adding users to groups.

Synchronous

public ApiResponse addUserToGroup(String userId, String groupId)

Asynchronous

public void addUserToGroupAsync(final String userId, 
                                final String groupId,
                                final ApiResponseCallback callback)

Parameters

Parameter Description
callback A handler to receive the response from an asynchronous call.
groupId A path, optionally showing hierarchical relationships. This must be unique in your data store.
userId The ID of the user to add.

Example

This example uses the Android SDK.

String groupId = "myGroup";
String userId = "joe";

// Call a data client method to asynchronously add the user to the 
// group selected in the UI. Handle the result with methods of the 
// callback object created here.
dataClient.addUserToGroupAsync(userId, groupId,
        new ApiResponseCallback() {

    @Override
    public void onException(Exception ex) {
        // Handle an exception, if any.
    }

    @Override
    public void onResponse(ApiResponse response) {
        // Do something with the response.
    }
});

This is the Usergrid.Group method for adding users to a group. (Usergrid is the open source project on which these features are based.)

Asynchronous

group.add(options, callback)

Parameters

Parameter Description
callback A handler to receive the response from an asynchronous call.
options A JSON object containing values for the group's properties.

Example

This example uses the JavaScript SDK.

// Current user set from an input on the home page.
var userName = "joe";

// Group selected from the dropdown.
var selectedGroupPath = "mygroup"

// Prepare an Apigee.Group entity for the 
// group to which you'll add the user.
var groupOptions = {
    client:apigeeClient, 
    path:selectedGroupPath
}
var group = new Apigee.Group(groupOptions);

// Get the group data.
group.fetch();

// Options to define the user you'll be adding to 
// the group.
var options = {
    'username':currentUser,
    'type':'users'
}

// Call an SDK method to get an entity representing
// the user so it can be used when adding the 
// user to the group.
apigeeClient.getEntity(options, function(error, entity, data){
    if (error) {
        // Display or log a message.
    } else {
        // Options that specify the user entity. 
        // The method for adding a user to a group requires
        // an entity, rather than just user properties.
        var addUserOptions = {
            user : entity
        }
        // Call an SDK method to add the user.
        group.add(addUserOptions, function(error, data, entities){
            if (error){
                // Display or log a message.
            } else {
                // Having succeeded, you might refresh a list 
                // the group's users.
            }
        });
    }        
});

The example assumes use of the Ruby SDK.

app = Usergrid::Application.new 'https://api.usergrid.com/my-org/my-app/'
app['groups/mynewgroup/users/john.doe'].post nil

The example assumes use of the Node.js module.

var options = {
    method:'POST',
    endpoint:'groups/mynewgroup/users/john.doe'    
};
client.request(options, function (err, data) {
    if (err) {
        //error — POST failed
    } else {
        //success — data will contain raw results from API call        
    }
});

 

コメントを追加

Provide your email address if you wish to be contacted offline about your comment.
We will not display your email address as part of your comment.

We'd love your feedback and perspective! Please be as specific as possible.
Type the characters you see in this picture. (verify using audio)

Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.