Skip to content

Commit 3fc0165

Browse files
committed
- Improved the Attribute Editor so that you can set a default value for newly added attributes whose field type contributes security grant rules (such as Asset, or Defined Value with the "Allow Adding New Values" option enabled).
1 parent 638a689 commit 3fc0165

4 files changed

Lines changed: 59 additions & 2 deletions

File tree

Rock.JavaScript.Obsidian/Framework/Controls/fieldTypeEditor.obs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@
8585
const configurationValues = ref<Record<string, string>>(props.modelValue?.configurationValues ?? {});
8686
const editConfigurationValues = ref<Record<string, string>>({});
8787

88+
/**
89+
* Security grant token minted by the server for the currently selected
90+
* field type. Field types that implement ISecurityGrantFieldType (e.g.
91+
* AssetFieldType) need their rules in the grant before the default value
92+
* editor can talk to the corresponding API endpoints. Plumbed onto
93+
* defaultValueAttribute so RockField provides it to its children.
94+
*/
95+
const securityGrantToken = ref<string | null>(null);
96+
8897
const fieldType = computed((): IFieldType | null => {
8998
if (!fieldTypeValueGuidOrEmptyString.value) {
9099
return null;
@@ -165,7 +174,8 @@
165174
description: "",
166175
isRequired: false,
167176
order: 0,
168-
categories: []
177+
categories: [],
178+
securityGrantToken: securityGrantToken.value
169179
};
170180
});
171181

@@ -210,6 +220,7 @@
210220
configurationProperties.value = {};
211221
configurationValues.value = {};
212222
defaultValue.value = "";
223+
securityGrantToken.value = null;
213224
isInternalUpdate = false;
214225

215226
updateModelValue();
@@ -245,6 +256,7 @@
245256
editConfigurationValues.value = result.data.editConfigurationValues;
246257
valueFormat.value = result.data.valueFormat ?? "";
247258
defaultValue.value = result.data.defaultValue ?? "";
259+
securityGrantToken.value = result.data.securityGrantToken ?? null;
248260
isInternalUpdate = false;
249261

250262
updateModelValue();

Rock.JavaScript.Obsidian/Framework/ViewModels/Controls/fieldTypeEditorUpdateAttributeConfigurationResultBag.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ export type FieldTypeEditorUpdateAttributeConfigurationResultBag = {
5050
*/
5151
editConfigurationValues?: Record<string, string> | null;
5252

53+
/**
54+
* Gets or sets the security grant token that should be used by the
55+
* default value editor for the selected field type. Field types that
56+
* implement Rock.Field.ISecurityGrantFieldType contribute their rules
57+
* into this token so that controls bound to the default value
58+
* preview (e.g. the asset/file manager picker for an Asset field
59+
* type) have the access they need before the attribute has been
60+
* saved.
61+
*/
62+
securityGrantToken?: string | null;
63+
5364
/**
5465
* A string that describes the format of the raw value stored in the
5566
* database.

Rock.Rest/v2/ControlsController.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5777,13 +5777,36 @@ public IActionResult FieldTypeEditorUpdateAttributeConfiguration( [FromBody] Fie
57775777
valueFormat = hintFieldType.GetFieldHints( configurationValues )?.ValueFormat;
57785778
}
57795779

5780+
// If the field type contributes security grant rules (e.g. an
5781+
// Asset field that needs the asset/file manager picker to work),
5782+
// mint an attribute-specific token here so the default value
5783+
// editor has the access it needs before the attribute has been
5784+
// saved. Mirrors the per-attribute grant generated in
5785+
// PublicAttributeHelper.GetPublicAttributeForEdit and lets
5786+
// RockField.obs pick it up via the attribute's securityGrantToken.
5787+
string securityGrantToken = null;
5788+
5789+
if ( fieldType is ISecurityGrantFieldType securityGrantFieldType )
5790+
{
5791+
var securityGrant = new SecurityGrant();
5792+
5793+
securityGrantFieldType.AddRulesToSecurityGrant( securityGrant, configurationValues );
5794+
5795+
if ( securityGrant.Rules.Count > 0 )
5796+
{
5797+
securityGrant.SetLifetime( TimeSpan.FromDays( 1 ) );
5798+
securityGrantToken = securityGrant.ToToken();
5799+
}
5800+
}
5801+
57805802
return Ok( new FieldTypeEditorUpdateAttributeConfigurationResultBag
57815803
{
57825804
ConfigurationProperties = configurationProperties,
57835805
AdminConfigurationValues = publicAdminConfigurationValues,
57845806
EditConfigurationValues = publicEditConfigurationValues,
57855807
ValueFormat = valueFormat,
5786-
DefaultValue = fieldType.GetPublicEditValue( privateDefaultValue, configurationValues )
5808+
DefaultValue = fieldType.GetPublicEditValue( privateDefaultValue, configurationValues ),
5809+
SecurityGrantToken = securityGrantToken
57875810
} );
57885811
}
57895812

Rock.ViewModels/Controls/FieldTypeEditorUpdateAttributeConfigurationResultBag.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,16 @@ public class FieldTypeEditorUpdateAttributeConfigurationResultBag
6767
/// </summary>
6868
/// <value>The default value information.</value>
6969
public string DefaultValue { get; set; }
70+
71+
/// <summary>
72+
/// Gets or sets the security grant token that should be used by the
73+
/// default value editor for the selected field type. Field types that
74+
/// implement Rock.Field.ISecurityGrantFieldType contribute their rules
75+
/// into this token so that controls bound to the default value
76+
/// preview (e.g. the asset/file manager picker for an Asset field
77+
/// type) have the access they need before the attribute has been
78+
/// saved.
79+
/// </summary>
80+
public string SecurityGrantToken { get; set; }
7081
}
7182
}

0 commit comments

Comments
 (0)