Documentation

Roles API Basics

You can Create and Manage Roles from the Application menu, but you can also use Liferay’s REST APIs. Call these services to manage Roles.

Associate a User to a Regular Role

Start a new Liferay DXP instance by running

docker run -it -m 8g -p 8080:8080 liferay/dxp:7.4.13-u29

Sign in to Liferay at http://localhost:8080 using the email address test@liferay.com and the password test. When prompted, change the password to learn.

Then, follow these steps:

  1. Download and unzip Roles API Basics.

    curl https://learn.liferay.com/dxp/latest/en/users-and-permissions/developer-guide/liferay-z3v5.zip -O
    
    unzip liferay-z3v5.zip
    
  2. Use the Users_GET_FromInstance to get a list of User IDs. Make note of the User ID you wish to associate with a Regular Role.

  3. Use the Roles_GET_FromInstance to get a list of all Role IDs. Make note of the Role ID you wish to associate that is roleType: regular. For example, the Analytics Administrator Regular Role type.

  4. Use the cURL script to associate a User to a Regular Role. On the command line, navigate to the curl folder. Execute the RoleUserAssociation_POST_ToInstance.sh script. Replace 1234 with a Regular Role’s ID. Replace 5678 with a User’s ID.

    ./RoleUserAssociation_POST_ToInstance.sh 1234 5678
    
  5. Navigate to Global MenuControl PanelRoles. Under the Regular Roles tab, click the specific Role you used to a associate the User. Click the Assignees tab. See that the User has been associated with the selected Role.

    See that the User has been associated.

  6. The REST service can also be called using the Java client. Navigate out of the curl folder and into the java folder. Compile the source files with the following command:

    javac -classpath .:* *.java
    
  7. Run the RoleUserAssociation_POST_ToInstance.java class with the following command.

    java -classpath .:* -DroleId=1234 -DuserAccountId=5678 RoleUserAssociation_POST_ToInstance
    

Examine the cURL Command

The RoleUserAssociation_POST_ToInstance.sh script calls the REST service with a cURL command.

curl \
	-H "Content-Type: application/json" \
	-X POST \
	"http://localhost:8080/o/headless-admin-user/v1.0/roles/${1}/association/user-account/${2}" \
	-u "[email protected]:learn"

Here are the command’s arguments:

Arguments

Description

-H "Content-Type: application/json"

Indicates that the request body format is JSON.

-X POST

The HTTP method to invoke at the specified endpoint

"http://localhost:8080/o/headless-admin-user/v1.0/roles/${1}/association/user-account/${2}"

The REST service endpoint

-u "test@liferay.com:learn"

Basic authentication credentials

Note

Basic authentication is used here for demonstration purposes. For production, you should authorize users via OAuth2.

The other cURL commands use similar JSON arguments.

Examine the Java Class

The RoleUserAssociation_POST_ToInstance.java class associated a User to a Regular Role by calling the Role-related service.

public static void main(String[] args) throws Exception {
	RoleResource.Builder builder = RoleResource.builder();

	RoleResource roleResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	roleResource.postRoleUserAccountAssociation(
		Long.valueOf(System.getProperty("roleId")),
		Long.valueOf(System.getProperty("userAccountId")));
}

This class invokes the REST service using only three lines of code:

Line (abbreviated)

Description

RoleResource.Builder builder = ...

Gets a Builder for generating an RoleResource service instance.

RoleResource roleResource = builder.authentication(...).build();

Specifies basic authentication and generates a RoleResource service instance.

roleResource.postRoleUserAccountAssociation(...);

Calls the postRoleUserAccountAssociation method and passes the data to post.

Note that the project includes the com.liferay.headless.admin.user.client.jar file as a dependency. You can find client JAR dependency information for all REST applications in the API explorer in your installation at /o/api.

Note

The main method’s comment demonstrates running the class.

The other example Java classes are similar to this one, but call different RoleResource methods.

Important

See RoleResource for service details.

Below are examples of calling other Role REST services using cURL and Java.

Get Roles from Instance

You can list Roles by executing the following cURL or Java command.

Roles_GET_FromInstance.sh

Command:

./Roles_GET_FromInstance.sh

Code:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/roles" \
	-u "[email protected]:learn"

Roles_GET_FromInstance.java

Command:

java -classpath .:* Roles_GET_FromInstance

Code:

public static void main(String[] args) throws Exception {
	RoleResource.Builder builder = RoleResource.builder();

	RoleResource roleResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	Page<Role> role = roleResource.getRolesPage(Pagination.of(1, 2));

	System.out.println(role);
}

The Instance’s Roles objects appear in JSON.

Get a Role

Get a specific Role with the following cURL or Java command.

Tip

Use Roles_GET_FromInstance.[java|sh] to get instance Role IDs.

Role_GET_ById.sh

Command:

./Role_GET_ById.sh 1234

Code:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/roles/${1}" \
	-u "[email protected]:learn"

Role_GET_ById.java

Command:

java -classpath .:* -DroleId=1234 Role_GET_ById

Code:

public static void main(String[] args) throws Exception {
	RoleResource.Builder builder = RoleResource.builder();

	RoleResource roleResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	Role role = roleResource.getRole(
		Long.valueOf(System.getProperty("roleId")));

	System.out.println(role);
}

The Role fields appear in JSON.

Associate a User to a Site Role

You can associate a User with a specific Site Role. Replace 1234 with the Role’s ID. Replace 5678 with your Site’s ID. Replace 9012 with your User’s ID.

RoleUserAssociation_POST_ToSite.sh

Command:

./RoleUserAssociation_POST_ToSite.sh 1234 5678 9012

Code:

curl \
	-H "Content-Type: application/json" \
	-X POST \
	"http://localhost:8080/o/headless-admin-user/v1.0/roles/${1}/association/user-account/${2}/site/${3}" \
	-u "[email protected]:learn"

RoleUserAssociation_POST_ToSite.java

Command:

java -classpath .:* -DroleId=1234 -DsiteId=5678 -DuserAccountId=9012 RoleUserAssociation_POST_ToSite

Code:

public static void main(String[] args) throws Exception {
	RoleResource.Builder builder = RoleResource.builder();

	RoleResource roleResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	roleResource.postSiteRoleUserAccountAssociation(
		Long.valueOf(System.getProperty("roleId")),
		Long.valueOf(System.getProperty("userAccountId")),
		Long.valueOf(System.getProperty("siteId")));
}

Associate a User to a Organization Role

You can associate a User with a specific Organization Role. Replace 1234 with your Organization’s ID. Replace 5678 with your Role’s ID. Replace 9012 with your User’s ID.

RoleUserAssociation_POST_ToOrganization.sh

Command:

./RoleUserAssociation_POST_ToOrganization.sh 1234 5678 9012

Code:

curl \
	-H "Content-Type: application/json" \
	-X POST \
	"http://localhost:8080/o/headless-admin-user/v1.0/roles/${1}/association/user-account/${2}/organization/${3}" \
	-u "[email protected]:learn"

RoleUserAssociation_POST_ToOrganization.java

Command:

java -classpath .:* -DorganizationId=1234 -DroleId=5678 -DuserAccountId=9012 RoleUserAssociation_POST_ToOrganization

Code:

Remove Regular Role Association

Remove a Regular Role association from a specific User. Replace 1234 with the Role’s ID. Replace 5678 with your User’s ID.

RoleUserAssociation_DELETE_FromInstance.sh

Command:

./RoleUserAssociation_DELETE_FromInstance.sh 1234 5678

Code:

curl \
	-H "Content-Type: application/json" \
	-X DELETE \
	"http://localhost:8080/o/headless-admin-user/v1.0/roles/${1}/association/user-account/${2}" \
	-u "[email protected]:learn"

RoleUserAssociation_DELETE_FromInstance.java

Command

java -classpath .:* -DroleId=1234 -DuserAccountId=5678 RoleUserAssociation_DELETE_FromInstance

Code:

public static void main(String[] args) throws Exception {
	RoleResource.Builder builder = RoleResource.builder();

	RoleResource roleResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	roleResource.deleteRoleUserAccountAssociation(
		Long.valueOf(System.getProperty("roleId")),
		Long.valueOf(System.getProperty("userAccountId")));
}

Remove Site Role Association

Remove a Site Role association from a specific User. Replace 1234 with the Role’s ID. Replace 5678 with your Site’s ID. Replace 9012 with your User’s ID.

RoleUserAssociation_DELETE_FromSite.sh

Command:

./RoleUserAssociation_DELETE_FromSite.sh 1234 5678 9012

Code:

curl \
	-H "Content-Type: application/json" \
	-X DELETE \
	"http://localhost:8080/o/headless-admin-user/v1.0/roles/${1}/association/user-account/${2}/site/${3}" \
	-u "[email protected]:learn"

RoleUserAssociation_DELETE_FromSite.java

Command

java -classpath .:* -DroleId=1234 -DsiteId=5678 -DuserAccountId=9012 RoleUserAssociation_DELETE_FromSite

Code:

public static void main(String[] args) throws Exception {
	RoleResource.Builder builder = RoleResource.builder();

	RoleResource roleResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	roleResource.deleteSiteRoleUserAccountAssociation(
		Long.valueOf(System.getProperty("roleId")),
		Long.valueOf(System.getProperty("userAccountId")),
		Long.valueOf(System.getProperty("siteId")));
}

Remove Organization Role Association

Remove an Organization Role association from a specific User. Replace 1234 with the Organization’s ID. Replace 5678 with your Role’s ID. Replace 9012 with your User’s ID.

RoleUserAssociation_DELETE_FromOrganization.sh

Command:

./RoleUserAssociation_DELETE_FromOrganization.sh 1234 5678 9012

Code:

curl \
	-H "Content-Type: application/json" \
	-X DELETE \
	"http://localhost:8080/o/headless-admin-user/v1.0/roles/${1}/association/user-account/${2}/organization/${3}" \
	-u "[email protected]:learn"

RoleUserAssociation_DELETE_FromOrganization.java

Command

java -classpath .:* -DorganizationId=1234 -DroleId=5678 -DuserAccountId=9012 RoleUserAssociation_DELETE_FromOrganization

Code:

public static void main(String[] args) throws Exception {
	RoleResource.Builder builder = RoleResource.builder();

	RoleResource roleResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	roleResource.deleteOrganizationRoleUserAccountAssociation(
		Long.valueOf(System.getProperty("roleId")),
		Long.valueOf(System.getProperty("userAccountId")),
		Long.valueOf(System.getProperty("organizationId")));
}

The API Explorer shows all of the Role services and schemas and has an interface to try out each service.