Beispiel #1
0
        public BlockActionResult GetEditAttributeValue(Guid attributeGuid)
        {
            if (GetAttributeValue(AttributeKey.AllowSettingofValues).AsBooleanOrNull() != true)
            {
                return(ActionBadRequest("Setting values is not enabled."));
            }

            var attribute = Rock.Web.Cache.AttributeCache.Get(attributeGuid);

            if (attribute == null)
            {
                return(ActionBadRequest());
            }

            var entityId = GetEntityId();

            var    attributeValue = new AttributeValueService(new RockContext()).GetByAttributeIdAndEntityId(attribute.Id, entityId);
            string value          = attributeValue != null && !string.IsNullOrWhiteSpace(attributeValue.Value) ? attributeValue.Value : attribute.DefaultValue;

            return(ActionOk(new
            {
                Attribute = PublicAttributeHelper.GetPublicAttributeForEdit(attribute),
                Value = PublicAttributeHelper.GetPublicEditValue(attribute, value)
            }));
        }
Beispiel #2
0
        public static void SetPublicAttributeValues(this IHasAttributes entity, Dictionary <string, string> attributeValues, Person currentPerson, bool enforceSecurity = true)
        {
            if (entity == null || entity.Attributes == null || entity.AttributeValues == null)
            {
                return;
            }

            foreach (var kvp in attributeValues)
            {
                if (!entity.Attributes.ContainsKey(kvp.Key) || !entity.AttributeValues.ContainsKey(kvp.Key))
                {
                    continue;
                }

                var attribute = entity.Attributes[kvp.Key];

                if (enforceSecurity && !attribute.IsAuthorized(Rock.Security.Authorization.EDIT, currentPerson))
                {
                    continue;
                }

                var value = PublicAttributeHelper.GetPrivateValue(attribute, kvp.Value);

                entity.SetAttributeValue(kvp.Key, value);
            }
        }
Beispiel #3
0
        public static Dictionary <string, PublicAttributeViewModel> GetPublicAttributesForEdit(this IHasAttributes entity, Person currentPerson, bool enforceSecurity = true)
        {
            if (entity == null || entity.Attributes == null)
            {
                return(new Dictionary <string, PublicAttributeViewModel>());
            }

            return(entity.Attributes
                   .Select(a => a.Value)
                   .Where(a => !enforceSecurity || a.IsAuthorized(Rock.Security.Authorization.VIEW, currentPerson))
                   .ToDictionary(a => a.Key, a => PublicAttributeHelper.GetPublicAttributeForEdit(a)));
        }
Beispiel #4
0
        public static Dictionary <string, string> GetPublicAttributeValuesForEdit(this IHasAttributes entity, Person currentPerson, bool enforceSecurity = true)
        {
            if (entity == null || entity.Attributes == null)
            {
                return(new Dictionary <string, string>());
            }

            return(entity.Attributes
                   .Select(a => new
            {
                Value = entity.GetAttributeValue(a.Key),
                Attribute = a.Value
            })
                   .Where(av => !enforceSecurity || av.Attribute.IsAuthorized(Rock.Security.Authorization.VIEW, currentPerson))
                   .ToDictionary(av => av.Attribute.Key, av => PublicAttributeHelper.GetPublicEditValue(av.Attribute, av.Value)));
        }
Beispiel #5
0
        /// <summary>
        /// Converts to an attribute and value into a custom poco that will
        /// be transmitted to the shell.
        /// </summary>
        /// <param name="attribute">The attribute.</param>
        /// <param name="value">The value.</param>
        /// <returns>The editable attribute including the value.</returns>
        private PublicEditableAttributeValueViewModel ToPublicEditableAttributeValue(AttributeCache attribute, string value)
        {
            var attr = PublicAttributeHelper.GetPublicAttributeForEdit(attribute);

            return(new PublicEditableAttributeValueViewModel
            {
                AttributeGuid = attr.AttributeGuid,
                Categories = attr.Categories,
                ConfigurationValues = attr.ConfigurationValues,
                Description = attr.Description,
                FieldTypeGuid = attr.FieldTypeGuid,
                IsRequired = attr.IsRequired,
                Key = attr.Key,
                Name = attr.Name,
                Order = attr.Order,
                Value = PublicAttributeHelper.GetPublicEditValue(attribute, value)
            });
        }
Beispiel #6
0
        public BlockActionResult GetEditAttribute(Guid attributeGuid)
        {
            using (var rockContext = new RockContext())
            {
                var attributeService = new AttributeService(rockContext);
                var attribute        = attributeService.Get(attributeGuid);

                if (attribute == null)
                {
                    return(ActionBadRequest());
                }

                return(ActionOk(new EditAttributeViewModel
                {
                    Attribute = PublicAttributeHelper.GetPublicEditableAttributeViewModel(attribute),
                    EntityTypeQualifierColumn = attribute.EntityTypeQualifierColumn,
                    EntityTypeQualifierValue = attribute.EntityTypeQualifierValue
                }));
            }
        }
Beispiel #7
0
        public static void SetPublicAttributeValue(this IHasAttributes entity, string key, string value, Person currentPerson, bool enforceSecurity = true)
        {
            if (entity == null || entity.Attributes == null || entity.AttributeValues == null)
            {
                return;
            }

            if (!entity.Attributes.ContainsKey(key) || !entity.AttributeValues.ContainsKey(key))
            {
                return;
            }

            var attribute = entity.Attributes[key];

            if (enforceSecurity && !attribute.IsAuthorized(Rock.Security.Authorization.EDIT, currentPerson))
            {
                return;
            }

            var databaseValue = PublicAttributeHelper.GetPrivateValue(attribute, value);

            entity.SetAttributeValue(key, databaseValue);
        }
Beispiel #8
0
        /// <summary>
        /// Gets the attribute value as a model that can be displayed on the
        /// user's device. This handles special block settings that change what
        /// value is available.
        /// </summary>
        /// <param name="rockContext">The rock database context.</param>
        /// <param name="attribute">The attribute whose value will be viewed.</param>
        /// <returns>A <see cref="PublicAttributeValueViewModel"/> that represents the attribute value.</returns>
        private string GetPublicAttributeValue(RockContext rockContext, AttributeCache attribute)
        {
            var entityId = GetEntityId();

            if (GetAttributeValue(AttributeKey.AllowSettingofValues).AsBooleanOrNull() ?? false)
            {
                AttributeValueService attributeValueService = new AttributeValueService(rockContext);
                var attributeValue = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, entityId);

                if (attributeValue != null && !attributeValue.Value.IsNullOrWhiteSpace())
                {
                    return(PublicAttributeHelper.GetPublicValueForView(attribute, attributeValue.Value));
                }
                else
                {
                    return(PublicAttributeHelper.GetPublicValueForView(attribute, attribute.DefaultValue));
                }
            }
            else
            {
                return(PublicAttributeHelper.GetPublicValueForView(attribute, attribute.DefaultValue));
            }
        }
Beispiel #9
0
        public BlockActionResult SaveEditAttributeValue(Guid attributeGuid, string value)
        {
            if (GetAttributeValue(AttributeKey.AllowSettingofValues).AsBooleanOrNull() != true)
            {
                return(ActionBadRequest("Setting values is not enabled."));
            }

            var attribute = AttributeCache.Get(attributeGuid);

            if (attribute == null)
            {
                return(ActionBadRequest());
            }

            using (var rockContext = new RockContext())
            {
                var entityId = GetEntityId();
                var attributeValueService = new AttributeValueService(rockContext);
                var attributeValue        = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, entityId);

                if (attributeValue == null)
                {
                    attributeValue = new AttributeValue
                    {
                        AttributeId = attribute.Id,
                        EntityId    = entityId
                    };
                    attributeValueService.Add(attributeValue);
                }

                attributeValue.Value = PublicAttributeHelper.GetPrivateValue(attribute, value);

                rockContext.SaveChanges();

                return(ActionOk(GetAttributeRow(attribute, rockContext)));
            }
        }