User sign-in script example
Version: 3.4
The following script example is executed when a user logs into Sitecore Content Hub. It updates the user’s groups based on the provided claims.
Important
Because this is an example, do not use it on your own Content Hub instance without making appropriate modifications.
RequestResponse
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
if (Context.AuthenticationSource != AuthenticationSource.External)
{
return;
}
// collect group names from the claims
List<string> groups = new List<string>();
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<long> 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);
Disable a script
A sign-in script might lock users out if it contains runtime errors or inconsistent user validation. To resolve this, disable the script by using the REST API or SDK to change M.Script.Enabled
to false.
The following example disables a script using the web SDK:
RequestResponse
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);