1. ユーザー登録後スクリプト

ユーザー登録後スクリプトの例

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

これは、新しいユーザーが Sitecore Content Hub に登録された後に実行されるユーザー登録後スクリプトの例です。外部プロバイダーが Google の場合、ユーザーを特定のユーザー グループに追加します。

ユース ケース

  1. 新しいユーザーが登録されました。
  2. スクリプトは、プロバイダーが Google かどうかを確認します。この場合、ユーザーは Google ユーザー グループに追加されます。

スクリプト

using System.Linq;

if (Context.ExternalUserInfo?.Provider != "Google") return;

var query = Query.CreateQuery(entities =>
from e in entities
where e.DefinitionName == "Usergroup" && e.Property("GroupName") == "Google"
select e);

var googleGroupId = await MClient.Querying.SingleIdAsync(query);
if (!googleGroupId.HasValue) throw new InvalidOperationException("Google usergroup not found.");

var relation = await Context.User.GetRelationAsync<IChildToManyParentsRelation>("UserGroupToUser");
relation.Parents.Add(googleGroupId.Value);

await MClient.Entities.SaveAsync(Context.User);

スクリプトの説明

  1. スクリプトで使用するライブラリをインクルードします。

    using System.Linq;
  2. 外部ユーザー プロバイダーを確認します。「Google」プロバイダーのみが対象です。

    if (Context.ExternalUserInfo?.Provider != "Google") return;
  3. Google ユーザー グループを取得するクエリを作成します。

    var query = Query.CreateQuery(entities =>
    from e in entities
    where e.DefinitionName == "Usergroup" && e.Property("GroupName") == "Google"
    select e);
    ヒント

    クエリの作成の詳細については、「スクリプト開発」のセクションを参照してください。

  4. クエリを実行して、Google グループ ID を取得します。

    var googleGroupId = await MClient.Querying.SingleIdAsync(query);
  5. ユーザー グループが見つからない場合は、InvalidOperationException をスローします。

    if (!googleGroupId.HasValue) throw new InvalidOperationException("Google usergroup not found.");
  6. ユーザーのユーザー グループ リレーションを取得します。

    var relation = await Context.User.GetRelationAsync<IChildToManyParentsRelation>("UserGroupToUser");
  7. ユーザー グループ リレーションを使用して、Google グループをユーザーのオブジェクトに追加します。

    relation.Parents.Add(googleGroupId.Value);
  8. ユーザー エンティティを保存します。

    await MClient.Entities.SaveAsync(Context.User);

セットアップ

  1. "Google" という名前のユーザー グループを作成します。

  2. 上記のソース コードを使用したユーザー登録後スクリプトを作成し、パブリッシュして有効にします。

この記事を改善するための提案がある場合は、 お知らせください!