oo

アカウントユーザーとロールのAPI

アプリケーションメニューから アカウントユーザーアカウントロール を管理することができますが、LiferayのREST APIを利用することもできます。 これらのサービスを呼び出して、アカウントユーザーとロールの作成と管理を行います。

アカウントユーザーの追加

新しいLiferay DXPインスタンスを起動し、以下を実行します。

docker run -it -m 8g -p 8080:8080 liferay/dxp:2024.q1.1。

メールアドレス test@liferay.com とパスワード test を使用して、http://localhost:8080でLiferayにサインインしてください。 プロンプトが表示されたら、パスワードを learn に変更します。

次に、以下の手順を実行します。

  1. Accounts API Basics をダウンロードし、解凍します。

    curl https://resources.learn.liferay.com/dxp/latest/en/users-and-permissions/developer-guide/liferay-t5p9.zip -O
    
    unzip liferay-t5p9.zip
    
  2. アカウントIDの一覧を取得するには、 Accounts_GET_FromInstance リソースを使用します。 ユーザーを追加したいアカウントIDをメモしてください。

  3. cURLスクリプトを使用して、新しいアカウントユーザーをアカウントに追加します。 コマンドラインで、curlフォルダに移動します。 アカウントIDをパラメータとして、 AccountUser_POST_ToAccount.sh スクリプトを実行します。

    ./AccountUser_POST_ToAccount.sh 1234
    

    JSONのレスポンスには、新しいアカウントが追加されたことが示されています。

    {
      "accountBriefs": [
        {
          "id": 38935,
          "name": "Able",
          "roleBriefs": []
        }
      ],
      "additionalName": "",
      "alternateName": "dog",
      "birthDate": "1977-01-01T00:00:00Z",
      "customFields": [],
      "dashboardURL": "",
      "dateCreated": "2021-10-12T21:00:59Z",
      "dateModified": "2021-10-12T21:01:01Z",
      "emailAddress": "dog@liferay.com",
      "externalReferenceCode": "",
      "familyName": "Easy",
      "givenName": "Dog",
      "id": 39005,
      "jobTitle": "",
      "keywords": [],
      "name": "Dog Easy",
      "organizationBriefs": [],
      "profileURL": "",
      "roleBriefs": [
        {
          "id": 20111,
          "name": "User"
        }
      ],
      "siteBriefs": [
        {
          "id": 20125,
          "name": "Global"
        },
        {
          "id": 20123,
          "name": "Guest"
        }
      ],
      "userAccountContactInformation": {
        "emailAddresses": [],
        "facebook": "",
        "jabber": "",
        "postalAddresses": [],
        "skype": "",
        "sms": "",
        "telephones": [],
        "twitter": "",
        "webUrls": []
      }
    }
    
  4. グローバルメニュー → [アプリケーション] → [Accounts] に移動します。 ユーザーを作成したアカウントをクリックします。 [Users] タブをクリックすると、作成された新しいユーザーが表示されます。

新しいアカウントユーザーが追加されたことを確認します。

  1. RESTサービスは、Javaクライアントを使って呼び出すこともできます。 curl フォルダから、 java フォルダに移動します。 以下のコマンドでソースファイルをコンパイルします。

    javac -classpath .:* *.java
    
  2. AccountUser_POST_ToAccount.java クラスを以下のコマンドで実行します。 1234 をアカウントのIDに置き換えてください。

    java -classpath .:* -DaccountId=1234 AccountUser_POST_ToAccount
    

cURLコマンドの検証

AccountUser_POST_ToAccount.sh スクリプトは、cURLコマンドでRESTサービスを呼び出します。

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/accounts/${1}/user-accounts" \
	--data-raw '
		{
			"alternateName": "Dog",
			"emailAddress": "dog@liferay.com",
			"familyName": "Easy",
			"givenName": "Dog"
		}' \
	--header "Content-Type: application/json" \
	--request "POST" \
	--user "test@liferay.com:learn"

コマンドの引数は次のとおりです。

引数 説明
-H "Content-Type: application/json" リクエストボディのフォーマットがJSONであることを示します。
-X POST 指定されたエンドポイントで起動するHTTPメソッド
"http://localhost:8080/o/headless-admin-user/v1.0/accounts/${1}/user-accounts" RESTサービスのエンドポイント
-d "{\"alternateName\": \"Dog\", \"emailAddress\": \"dog@liferay.com\", \"familyName\": \"Easy\", \"givenName\": \"Dog\"}" お客様が掲載を希望するデータ
-u "test@liferay.com:learn" 基本的な認証情報
Note

ここでは、デモのために基本的な認証を使用しています。 本番環境では、 OAuth2 を使ってユーザーを認証する必要があります。 OAuth2を利用したReactアプリケーションのサンプルは、OAuth2を利用したユーザー認証 をご参照ください。

他のcURLコマンドも同様のJSON引数を使用します。

Javaクラスを調べる

Account_POST_ToInstance.java クラスは、関連するサービスを呼び出して、アカウントユーザーを追加します。

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

	AccountResource accountResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Account account = accountResource.postAccount(
		new Account() {
			{
				description = "Foo";
				name = "Fox";
			}
		});

	System.out.println(account);
}

このクラスは、次の3行のコードのみを使用してRESTサービスを呼び出します。

行(省略形) 説明
UserAccountResource.Builder builder = ... UserAccountResourceサービスインスタンスを生成するためのBuilderを取得します。
UserAccountResource userAccountResource = builder.authentication(...).build(); ベーシック認証を指定し、 UserAccountResource サービスインスタンスを生成します。
UserAccount userAccount = userAccountResource.postAccountUserAccount(...); userAccountResource.postAccountUserAccount メソッドを呼び出し、データをpostに渡します。

プロジェクトには、依存関係としてcom.liferay.headless.admin.user.client.jarファイルが含まれていることに注意してください。 すべてのRESTアプリケーションのクライアントJAR依存関係情報は、/o/apiでインストール先のAPIエクスプローラーで確認できます。

Note

mainメソッドのコメントでは、クラスの実行を実演しています。

他のJavaクラスの例は、このクラスと似ていますが、異なるメソッドを呼び出しています。

Important

サービスの詳細は、 AccountResource を参照してください。

以下は、cURLとJavaを使って、関連する他のRESTサービスを呼び出す例です。

アカウントからアカウントユーザーを取得

以下のcURLまたはJavaコマンドを実行すると、アカウントのユーザーを一覧表示できます。 上記と同様に、 1234 をアカウントのIDに置き換えてください。

AccountUsers_GET_FromAccount.sh

コマンド:

./AccountUsers_GET_FromAccount 1234

コード:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/accounts/${1}/user-accounts" \
	--user "test@liferay.com:learn"

AccountUsers_GET_FromAccount.java

コマンド:

java -classpath .:* -DaccountId=1234 AccountUsers_GET_FromAccount

コード:

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

	UserAccountResource userAccountResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Page<UserAccount> page = userAccountResource.getAccountUserAccountsPage(
		Long.valueOf(System.getProperty("accountId")), null, null,
		Pagination.of(1, 2), null);

	System.out.println(page);
}

アカウントの UserAccountの オブジェクトがJSONで表示されます。

アカウントロールの投稿

特定のアカウントに対して新しいアカウントロールを作成します。 なお、 1234 は、アカウントのIDに置き換えてください。

AccountRole_POST_ToAccount.sh

コマンド:

./AccountRole_POST_ToAccount.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/accounts/${1}/account-roles" \
	--data-raw '
		{
			"name": "Charlie"
		}' \
	--header "Content-Type: application/json" \
	--request "POST" \
	--user "test@liferay.com:learn"

AccountRole_POST_ToAccount.java

コマンド:

java -classpath .:* -DaccountId=1234 AccountRole_POST_ToAccount

コード:

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

	AccountRoleResource accountRoleResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	AccountRole accountRole = accountRoleResource.postAccountAccountRole(
		Long.valueOf(System.getProperty("accountId")),
		new AccountRole() {
			{
				name = "How";
			}
		});

	System.out.println(accountRole);
}

ユーザーをアカウントロールに関連付ける

ユーザーを特定のアカウントロールに関連付けることができます。 1234 をアカウントのIDに置き換えてください。 お使いのアカウントのロールのIDを5678 に置き換えます。 9012 は、アカウントユーザーのIDに置き換えてください。

AccountRole_POST_UserAssociation.sh

コマンド:

./AccountRole_POST_UserAssociation.sh 1234 5678 9012

コード:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/accounts/${1}/account-roles/${2}/user-accounts/${3}" \
	--header "Content-Type: application/json" \
	--request "POST" \
	--user "test@liferay.com:learn"

AccountRole_POST_UserAssociation.java

コマンド:

java -classpath .:* -DaccountId=1234 -DaccountRoleId=5678 -DuserAccountId=9012 AccountRole_POST_UserAssociation

コード:

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

	AccountRoleResource accountRoleResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	accountRoleResource.postAccountAccountRoleUserAccountAssociation(
		Long.valueOf(System.getProperty("accountId")),
		Long.valueOf(System.getProperty("accountRoleId")),
		Long.valueOf(System.getProperty("userAccountId")));
}

アカウントからのアカウントロールの取得

以下のcURLまたはJavaコマンドを実行することで、アカウントのアカウントロールを一覧表示することができます。 1234 をアカウントのIDに置き換えてください。

AccountRoles_GET_FromAccount.sh

コマンド:

./AccountRoles_GET_FromAccount.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/accounts/${1}/account-roles" \
	--user "test@liferay.com:learn"

AccountRoles_GET_FromAccount.java

コマンド:

java -classpath .:* -DaccountId=1234 AccountRoles_GET_FromAccount

コード:

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

	AccountRoleResource accountRoleResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Page<AccountRole> page = accountRoleResource.getAccountAccountRolesPage(
		Long.valueOf(System.getProperty("accountId")), null,
		Pagination.of(1, 2), null);

	System.out.println(page);
}

アカウントの AccountRoleの オブジェクトがJSONで表示されます。

アカウントロール・ユーザー・アソシエーションの削除

特定のアカウントユーザーからアカウントロールの関連付けを削除します。 1234 をアカウントのIDに置き換えてください。 お使いのアカウントのロールのIDを5678 に置き換えます。 9012 は、アカウントユーザーのIDに置き換えてください。

AccountRole_DELETE_UserAssociation.sh

コマンド:

./AccountRole_DELETE_UserAssociation.sh 1234 5678 9012

コード:

curl \
	"http://localhost:8080/o/headless-admin-user/v1.0/accounts/${1}/account-roles/${2}/user-accounts/${3}" \
	--header "Content-Type: application/json" \
	--request "DELETE" \
	--user "test@liferay.com:learn"

AccountRole_DELETE_UserAssociation.java

コマンド

java -classpath .:* -DaccountId=1234 -DaccountRoleId=5678 -DuserAccountId=9012 AccountRole_DELETE_UserAssociation

コード:

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

	AccountRoleResource accountRoleResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	accountRoleResource.deleteAccountAccountRoleUserAccountAssociation(
		Long.valueOf(System.getProperty("accountId")),
		Long.valueOf(System.getProperty("accountRoleId")),
		Long.valueOf(System.getProperty("userAccountId")));
}

APIエクスプローラーには、アカウントのすべてのサービスとスキーマが一覧表示され、各サービスを試すためのインターフェースがあります。