1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Follow-Up Questions on Generic Content Module Sample

Follow-Up Questions on Generic Content Module Sample

2 posts, 0 answered
  1. Adam
    Adam avatar
    33 posts
    Registered:
    23 Feb 2009
    19 Feb 2010
    Link to this post
    Hi Everyone,

    Hats off to Ivan for his blog posting on the Sample Generic Content Module (http://www.sitefinity.com/blogs/ivan/posts/09-03-11/sample_generic_content_based_module_products_module.aspx). Now we can leverage the code in this sample to build the majority of my custom modules.

    I have a couple of follow-up questions to the post:

    1) Could you provide a code sample of a module using fileupload/radupload/imageselector in the edit/insert view(s)? A perfect example would be the image management control in the edit view of the image library module. However, a simple file upload sample would suffice, worst case.

    2) Could you provide a code sample where two generic content types have relationships and are managed in separate views. For example, I'd like to see a module where Products and Orders could be managed within the same module.

    3) How do we implement embedded templates for the public views in the Products module?

    4) Finally, does this post on applying permissions to Gen. Content Modules apply to any custom module? http://www.sitefinity.com/blogs/ivan/posts/09-03-22/products_module_implementing_permissions.aspx. If not, please provide a more generic code sample for applying permissions to custom modules not based on Gen. Content.


    Thanks!
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    24 Feb 2010
    Link to this post
    Hi Adam,

    1) Could you provide a code sample of a module using fileupload/radupload/imageselector in the edit/insert view(s)? A perfect example would be the image management control in the edit view of the image library module. However, a simple file upload sample would suffice, worst case.

    You can add ButtonSelector control to your view template. The control expose upload and select functionality

    <li class="selector">
                       <asp:Label ID="Label2" AssociatedControlID="SelectItem" runat="server"><asp:Literal ID="Literal14" runat="server" Text="<%$Resources:SelectItem%>"></asp:Literal> <em id="SelectItemLable" runat="server"></em></asp:Label>
                       <asp:TextBox ID="SelectItem" Text="" runat="server" MaxLength="250"></asp:TextBox>
                       <sfLib:ButtonSelector WindowNavigateUrl="~/Sitefinity/UserControls/Dialogs/ImageEditorDialog.aspx" ItemTemplatePath="~/Sitefinity/Admin/ControlTemplates/News/Dialogs/ButtonSelector.ascx" AssociatedControls="SelectItem" runat="server" cssclass="pickerWrapper" ButtonText="<%$Resources:Select%>" />
                       <p class="example">
                           <asp:Literal ID="Literal15" runat="server" Text="<%$Resources:SelectItemNote %>"></asp:Literal>
                       </p>
                   </li>


    2) Could you provide a code sample where two generic content types have relationships and are managed in separate views. For example, I'd like to see a module where Products and Orders could be managed within the same module.

    Generally you can create one module with two view - Products and Orders. Each of these views will have IContent objects. The relation between objects could be based on the Items ID using selectors that pass the ID. Then you can create a separate view where you merge the list of items.
    If you use separate module for Products and Orders you can work with IContent ID and provider name. If both views are in the same module you can use only the ID of an item. You can also use Dictionary<guid, guid> to make some relation, then retrieve and manupulate the data.

    3) How do we implement embedded templates for the public views in the Products module?


    Crate ascx files which will be used as templates
    set the files as embedded resourced through VisualStudio
    use WebSysTemplate class to make templates embedded

    sample

    [WebSysTemplate(CustomView.CustomViewItemListTemplateName , "Desc", "/provider", true, "2009-02-17")]
           public override string ItemListTemplatePath
           {
               get
               {
                   return base.ItemListTemplatePath;
               }
               set
               {
                   base.ItemListTemplatePath = value;
               }
           }
     
           [WebSysTemplate(CustomView.CustomViewSingleItemTemplateName , "Desc", "/provider", true, "2009-02-17")]
           public override string SingleItemTemplatePath
           {
               get
               {
                   return base.SingleItemTemplatePath;
               }
               set
               {
                   base.SingleItemTemplatePath = value;
               }
           }
     
     
    #region Constants
     
           private const string CustomViewItemListTemplateName =
               "Telerik.Samples.Resources.ControlTemplates.Frontend.Modes.ListPageMaster.ascx";
     
           private const string CustomViewSingleItemTemplateName =
               "Telerik.Samples.Resources.ControlTemplates.Frontend.Modes.ListPageDetails.ascx";
     
           #endregion

    internal class WebSysTemplate : EmbeddedTemplateAttribute
    {
        public WebSysTemplate(string resourceName, string description
            , string defaultExtPaht, bool isFrontEnd, string lastModified)
            : base(resourceName, description, defaultExtPaht, isFrontEnd, lastModified)
        {
        }
     
        public override string Description
        {
            get
            {
                return "Somedesc";
            }
        }
     
        public override object TypeId
        {
            get
            {
                return typeof(EmbeddedTemplateAttribute);
            }
        }
    }

    another option is shown below

    public override string ItemListTemplateName
     {
         get
         {
             return CustomView.CustomViewCustomItemListTemplateName;
         }
     }
     
     public override string SingleItemTemplateName
     {
         get
         {
             return CustomView.CustomViewCustomSingleItemTemplateName;
         }
     }
     
     public override Type AssemblyInfo
     {
         get
         {
             return typeof(ContentView);
         }
         set
         {
             base.AssemblyInfo = value;
         }
     }
     
     
     
     #region Constants
     
     private const string CustomViewCustomItemListTemplateName = "CustomViewCustomControl.Resources.ListPageMaster.ascx";
     
     private const string CustomViewCustomSingleItemTemplateName = "CustomViewCustomControl.Resources.ListPageDetails.ascx";
     
     #endregion


    4) Finally, does this post on applying permissions to Gen. Content Modules apply to any custom module? http://www.sitefinity.com/blogs/ivan/posts/09-03-22/products_module_implementing_permissions.aspx. If not, please provide a more generic code sample for applying permissions to custom modules not based on Gen. Content.
    You can take a loo at Programming Security: TOC

    sample implementation

    1. Create GlobalPermission and GlobalPermissions classes

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Security.Permissions;
    using Telerik.Security;
     
    /// <summary>
    /// Summary description for GlobalPermission
    /// </summary>
    public class GlobalPermission : ApplicationPermission
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="GlobalPermission"/> class for the
        ///     specified provider name.
        /// </summary>
        /// <param name="providerName">The name of the provider for which the permissions will be checked.</param>
        public GlobalPermission(string providerName)
            : this(new GlobalPermissions(providerName))
        {
        }
     
        /// <summary>
        ///     Initializes a new instance of the <see cref="GlobalPermission"/> class with
        ///     the specified secured object.
        /// </summary>
        /// <param name="secObj"><see cref="GlobalPermissions"/> secured object.</param>
        public GlobalPermission(GlobalPermissions secObj)
            : base(secObj, 0)
        {
        }
     
        /// <summary>
        ///     Initializes a new instance of the <see cref="GlobalPermission"/> class for the
        ///     specified provider name and rights.
        /// </summary>
        /// <param name="providerName">The name of the provider for which the permissions will be checked.</param>
        /// <param name="requestedRights">The requested righths to check permissions for.</param>
        public GlobalPermission(string providerName, int requestedRights)
            : this(new GlobalPermissions(providerName), requestedRights)
        {
        }
     
        /// <summary>
        ///     Initializes a new instance of the <see cref="GlobalPermission"/> class for the
        ///     specified secured object and rights.
        /// </summary>
        /// <param name="secObj"><see cref="GlobalPermissions"/> secured object.</param>
        /// <param name="requestedRights">The requested righths to check permissions for.</param>
        public GlobalPermission(GlobalPermissions secObj, int requestedRights)
            : base(secObj, requestedRights)
        {
        }
     
        /// <summary>
        /// Checks if the current user has granted permissions.
        /// </summary>
        /// <returns>true if has permissions; otherwise false.</returns>
        public override bool CheckDemand()
        {
            if (SecurityManager.IsCurrentUserUnrestricted())
                return true;
     
            return base.CheckDemand();
        }
     
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Security.Permissions;
     
    /// <summary>
    /// Summary description for GlobalPermissions
    /// </summary>
    public class GlobalPermissions : SecuredBase
    {
        /// <summary>
        /// Initializes a new instance of <see cref="GlobalPermissions"/> class for a
        /// specified provider name.
        /// </summary>
        /// <param name="providerName">the name of the provider</param>
        public GlobalPermissions(string providerName)
        {
            // THIS WILL WORK IN CASE YOU HAVE ONE PROVIDER
            this.rootId = new Guid("E48BA2BE-B022-4949-BBE7-E70C7A14462E");
            this.securityManager = new Telerik.Security.SecurityManager();
            this.rights = TelerikRights.Instance;
        }
     
        /// <summary>Read-only property. Gets the ID of the secured object.</summary>
        /// <value><see cref="Guid"/> object.</value>
        public override Guid ID
        {
            get
            {
                return this.rootId;
            }
        }
     
        private Guid rootId;
     
    }

    In the base provider get the name of the security provider

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Configuration.Provider;
    using System.Collections.Specialized;
     
    /// <summary>
    /// Summary description for BaseProvider
    /// </summary>
    public abstract class BaseProvider : ProviderBase
    {
     
        #region Properties
     
        /// <summary>
        /// Gets the name of the security provider
        /// </summary>
        public virtual string SecurityProviderName
        {
            get
            {
                return this.securityProviderName;
            }
        }
     
        #endregion
     
        #region Initialization
     
         
        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
            this.securityProviderName = config["SecurityProviderName"];
        }
     
        #endregion
     
        #region Private fields
     
        private string securityProviderName;
     
        #endregion
     
     
    }

    Get the permission if your custom module manager

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Security.Permissions;
    using Telerik.Cms.Engine.Security;
    using System.Configuration;
    using Telerik;
     
    /// <summary>
    /// Summary description for TelerikManager
    /// </summary>
    public class TelerikManager : ManagerBase<BaseProvider>
     {
           public TelerikManager()
           {
           }
            
            #region Constructors
     
           public TelerikManager(string providerName)
                : base(providerName)
            {
     
            }
     
            #endregion
     
            #region Base overrides
            /// <summary>
            /// Overriden. Gets the default provider for module
            /// </summary>
            public override string DefaultProvider
            {
                get
                {
                  
                    if (!string.IsNullOrEmpty(ConfigHelper.Handler.DefaultProvider))
                        return ConfigHelper.Handler.DefaultProvider;
                    return String.Empty;
                }
            }
     
            /// <summary>
            /// Overriden. Gets the module name
            /// </summary>
            public override string ModuleName
            {
                get
                {
     
                    return "CustomTelerikModule";
                }
            }
     
             ///<summary>
             ///Gets the permissions.
             ///</summary>
             ///<value>The permissions.</value>
            public GlobalPermissions Permissions
            {
                get
                {
                    return (GlobalPermissions)TelerikManager.SecurityRoots[base.Provider.Name];
                }
            }
     
             ///<summary>
             ///Gets the security roots.
             ///</summary>
             ///<value>The security roots.</value>
            public static Dictionary<String, ISecured> SecurityRoots
            {
                get
                {
                    if (securityRoots == null)
                    {
                        securityRoots = new Dictionary<String, ISecured>(Providers.Count);
                        foreach (string name in Providers.Keys)
                            securityRoots.Add(name, new GlobalPermissions(name));
                    }
                    return securityRoots;
                }
            }
     
            /// <summary>
            /// Overriden. Gets the collection of all providers for this module
            /// </summary>
            protected override ProviderSettingsCollection ProvidersSettings
            {
                get
                {
                    return ConfigHelper.Handler.Providers;
                }
            }
     
            /// <summary>
            /// Gets the providers.
            /// </summary>
            /// <value>The providers.</value>
            public static IDictionary<String, BaseProvider> Providers
            {
                get
                {
                    if (providers == null)
                        providers = new TelerikManager().GetProviders();
                    return providers;
                }
            }
     
     
            #endregion
     
            #region Security Methods
     
            /// <summary>
            /// Gets the permission.
            /// </summary>
            /// <returns></returns>
            public GlobalPermission GetPermission()
            {
                return new GlobalPermission(this.Permissions);
            }
     
            /// <summary>
            /// Gets the permission.
            /// </summary>
            /// <param name="requestRights">The request rights.</param>
            /// <returns></returns>
            public GlobalPermission GetPermission(int requestRights)
            {
                return new GlobalPermission(this.Permissions, requestRights);
            }
     
     
            #endregion
     
            #region Private fields
     
            string providerName;
            private static IDictionary<String, BaseProvider> providers;
            private static Dictionary<String, ISecured> securityRoots;
     
            #endregion
    }

    You can also implement your own rights by creating a custom class that inherits from Rights. Then you need to use this custom class in GlobalPermissions class as the samples show.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Security.Permissions;
     
    /// <summary>
    /// Summary description for TelerikRights
    /// </summary>
    public class TelerikRights : Rights
    {
     
        //adding custom permissions
     
        [LocalRight("View")]
        public static int View
        {
            get
            {
                return 1;
            }
        }
     
     
        [LocalRight("Modify")]
        public static int Modify
        {
            get
            {
                return 2;
            }
        }
     
        [LocalRight("ManagePermissions")]
        public static int ManagePermissions
        {
            get
            {
                return 4;
            }
        }
     
        [LocalRight("SomethingElse")]
        public static int SomethingElse
        {
            get
            {
                return 8;
     
            }
        }
     
     
        public static TelerikRights Instance
        {
            get
            {
                return instance;
            }
        }
     
     
        private static TelerikRights instance = new TelerikRights();
     
    }

    You can observe Sample pluggable module: Contacts pluggable module and Sample intra site module: Contacts intra site module.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
2 posts, 0 answered