Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataValueEditor"/> class.
        /// </summary>
        public DataValueEditor(
            ILocalizedTextService localizedTextService,
            IShortStringHelper shortStringHelper,
            IJsonSerializer jsonSerializer,
            IIOHelper ioHelper,
            DataEditorAttribute attribute)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }
            _localizedTextService = localizedTextService;
            _shortStringHelper    = shortStringHelper;
            _jsonSerializer       = jsonSerializer;

            var view = attribute.View;

            if (string.IsNullOrWhiteSpace(view))
            {
                throw new ArgumentException("The attribute does not specify a view.", nameof(attribute));
            }

            if (view.StartsWith("~/"))
            {
                view = ioHelper.ResolveRelativeOrVirtualUrl(view);
            }

            View      = view;
            ValueType = attribute.ValueType;
            HideLabel = attribute.HideLabel;
        }
        /// <summary>
        /// Discovers fields from configuration properties marked with the field attribute.
        /// </summary>
        private static List <ConfigurationField> DiscoverFields(IIOHelper ioHelper)
        {
            var fields     = new List <ConfigurationField>();
            var properties = TypeHelper.CachedDiscoverableProperties(typeof(TConfiguration));

            foreach (var property in properties)
            {
                var attribute = property.GetCustomAttribute <ConfigurationFieldAttribute>(false);
                if (attribute == null)
                {
                    continue;
                }

                ConfigurationField field;

                var attributeView = ioHelper.ResolveRelativeOrVirtualUrl(attribute.View);
                // if the field does not have its own type, use the base type
                if (attribute.Type == null)
                {
                    field = new ConfigurationField
                    {
                        // if the key is empty then use the property name
                        Key          = string.IsNullOrWhiteSpace(attribute.Key) ? property.Name : attribute.Key,
                        Name         = attribute.Name,
                        PropertyName = property.Name,
                        PropertyType = property.PropertyType,
                        Description  = attribute.Description,
                        HideLabel    = attribute.HideLabel,
                        View         = attributeView
                    };

                    fields.Add(field);
                    continue;
                }

                // if the field has its own type, instantiate it
                try
                {
                    field = (ConfigurationField)Activator.CreateInstance(attribute.Type);
                }
                catch (Exception ex)
                {
                    throw new Exception($"Failed to create an instance of type \"{attribute.Type}\" for property \"{property.Name}\" of configuration \"{typeof(TConfiguration).Name}\" (see inner exception).", ex);
                }

                // then add it, and overwrite values if they are assigned in the attribute
                fields.Add(field);

                field.PropertyName = property.Name;
                field.PropertyType = property.PropertyType;

                if (!string.IsNullOrWhiteSpace(attribute.Key))
                {
                    field.Key = attribute.Key;
                }

                // if the key is still empty then use the property name
                if (string.IsNullOrWhiteSpace(field.Key))
                {
                    field.Key = property.Name;
                }

                if (!string.IsNullOrWhiteSpace(attribute.Name))
                {
                    field.Name = attribute.Name;
                }

                if (!string.IsNullOrWhiteSpace(attribute.View))
                {
                    field.View = attributeView;
                }

                if (!string.IsNullOrWhiteSpace(attribute.Description))
                {
                    field.Description = attribute.Description;
                }

                if (attribute.HideLabelSettable.HasValue)
                {
                    field.HideLabel = attribute.HideLabel;
                }
            }

            return(fields);
        }
Esempio n. 3
0
    /// <inheritdoc />
    public ContentApp?GetContentAppFor(object o, IEnumerable <IReadOnlyUserGroup> userGroups)
    {
        string?partA, partB;

        switch (o)
        {
        case IContent content:
            partA = "content";
            partB = content.ContentType.Alias;
            break;

        case IMedia media:
            partA = "media";
            partB = media.ContentType.Alias;
            break;

        case IMember member:
            partA = "member";
            partB = member.ContentType.Alias;
            break;

        case IContentType contentType:
            partA = "contentType";
            partB = contentType?.Alias;
            break;

        case IDictionaryItem _:
            partA = "dictionary";
            partB = "*";     // Not really a different type for dictionary items
            break;

        default:
            return(null);
        }

        ShowRule[] rules          = _showRules ??= ShowRule.Parse(_definition.Show).ToArray();
        var        userGroupsList = userGroups.ToList();

        var okRole  = false;
        var hasRole = false;
        var okType  = false;
        var hasType = false;

        foreach (ShowRule rule in rules)
        {
            if (rule.PartA?.InvariantEquals("role") ?? false)
            {
                // if roles have been ok-ed already, skip the rule
                if (okRole)
                {
                    continue;
                }

                // remember we have role rules
                hasRole = true;

                foreach (IReadOnlyUserGroup group in userGroupsList)
                {
                    // if the entry does not apply, skip
                    if (!rule.Matches("role", group.Alias))
                    {
                        continue;
                    }

                    // if the entry applies,
                    // if it's an exclude entry, exit, do not display the content app
                    if (!rule.Show)
                    {
                        return(null);
                    }

                    // else ok to display, remember roles are ok, break from userGroupsList
                    okRole = rule.Show;
                    break;
                }
            }

            // it is a type rule
            else
            {
                // if type has been ok-ed already, skip the rule
                if (okType)
                {
                    continue;
                }

                // remember we have type rules
                hasType = true;

                // if the entry does not apply, skip it
                if (!rule.Matches(partA, partB))
                {
                    continue;
                }

                // if the entry applies,
                // if it's an exclude entry, exit, do not display the content app
                if (!rule.Show)
                {
                    return(null);
                }

                // else ok to display, remember type rules are ok
                okType = true;
            }
        }

        // if roles rules are specified but not ok,
        // or if type roles are specified but not ok,
        // cannot display the content app
        if ((hasRole && !okRole) || (hasType && !okType))
        {
            return(null);
        }

        // else
        // content app can be displayed
        return(_app ??= new ContentApp
        {
            Alias = _definition.Alias,
            Name = _definition.Name,
            Icon = _definition.Icon,
            View = _ioHelper.ResolveRelativeOrVirtualUrl(_definition.View),
            Weight = _definition.Weight,
        });
    }