1. Cloud development

ユーザーサインインスクリプト

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

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

以下のウェブスクリプトの例は、ユーザーがSitecore Content Hubにログインした際に実行されます。提供されたクレームに基づいてユーザーのグループを更新します。

!重要この例ではEveryoneユーザーグループを使用しています。組織のユーザーグループ名に基づいてスクリプトをカスタマイズせずに、自分のContent Hubインスタンスで使用しないでください。

!注始める前に

  • サインイン用のクレームマッピング設定はスキーマエディタで設定されていることを確認してください。

文字

using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;

if (Context.AuthenticationSource != AuthenticationSource.External) { return; }

// collect group names from the claims List groups = new List (); if (Context.ExternalUserInfo?.Claims == null) { groups.Add(Stylelabs.M.Sdk.Constants.UserGroup.Groups.Everyone); } else { foreach (Stylelabs.M.Scripting.Types.V1_0.User.Claim group in Context.ExternalUserInfo.Claims) { if (group.Type == "MySpecialGroupType") { groups.Add(group.Value); } } }

// load the group ids List groupIds = (await MClient.Users.GetUserGroupIdsAsync(groups).ConfigureAwait(false))?.Values.ToList();

var user = Context.User;

// ensure UserGroupToUser relation is loaded await user.LoadMembersAsync(null, new RelationLoadOption("UserGroupToUser")).ConfigureAwait(false);

// update the relation UserGroupToUser with ids of the collected groups user.GetRelation("UserGroupToUser").SetIds(groupIds);

// update the user await MClient.Entities.SaveAsync(user).ConfigureAwait(false);

脚本の説明

このセクションでは、スクリプトを実行順に順に進め、各パートについて説明します。番号付きの項目はシーケンスを記述しており、実行命令ではありません。

  1. 認証ソースが外部であれば、外部の主張に基づいてユーザーグループが更新されていることを確認してください。

    if (Context.AuthenticationSource != AuthenticationSource.External) { return; }

  2. 外部のクレームからグループ情報を収集するためにグループ名のリストを作成しましょう。クレームがない場合は、デフォルトのEveryoneグループを追加します。

    List groups = new List (); if (Context.ExternalUserInfo?.Claims == null) { groups.Add(Stylelabs.M.Sdk.Constants.UserGroup.Groups.Everyone); } else { foreach (Stylelabs.M.Scripting.Types.V1_0.User.Claim group in Context.ExternalUserInfo.Claims) { if (group.Type == "MySpecialGroupType") { groups.Add(group.Value); } } }

  3. GetUserGroupIdsAsyncメソッドを使って収集されたユーザーグループのIDを取得します。これらのIDはユーザーのグループ関係の更新に使用されます。

    List groupIds = (await MClient.Users.GetUserGroupIdsAsync(groups).ConfigureAwait(false))?.Values.ToList();

  4. コンテキストからターゲットユーザーエンティティを取得し、UserGroupToUser関係が読み込まれます。この関係は、ユーザーが特定のグループに所属していることを表します。

    var user = Context.User;

    // ensure UserGroupToUser relation is loaded await user.LoadMembersAsync(null, new RelationLoadOption("UserGroupToUser")).ConfigureAwait(false);

  5. 収集されたグループのIDでUserGroupToUser関係を更新し、ユーザーを適切なグループにリンクさせます。

    user.GetRelation("UserGroupToUser").SetIds(groupIds);

  6. ユーザーエンティティを保存し、更新されたグループメンバーシップを永続化します。

    await MClient.Entities.SaveAsync(user).ConfigureAwait(false);

セットアップ

  • ユーザーサインインスクリプトを作成、公開、有効化します

サインインスクリプトを無効にする

サインインスクリプトにランタイムエラーやユーザー検証の不一致が含まれている場合、ユーザーはロックアウトされることがあります。もしそうなったら、REST APIやSDKを使ってスクリプトを無効化し、M.Script.Enabledをfalseに変更してください。

以下の例は、Web SDKを使ってスクリプトを無効化しています:

var loadConfig = new EntityLoadConfiguration( CultureLoadOption.None, new PropertyLoadOption( ScriptingConstants.Scripting.Properties.Enabled), RelationLoadOption.None);

var script = await MClient.Entities.GetAsync( your script id , loadConfig).ConfigureAwait(false); script?.SetPropertyValue("M.Script.Enabled", false);

await MClient.Entities.SaveAsync(script).ConfigureAwait(false);

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