this.GetSecurityRoot(); allways NULL

2 posts, 0 answers
  1. lcmassena
    lcmassena avatar
    4 posts
    Member since:
    Feb 2011

    Posted 21 Feb 2011 Link to this post

    Hi All.
    I am creating a new News Provider but allways the method this.GetSecurityRoot(); is null.
    Where I am making a mistake?

    The above code is a test only. Please do not consider any logic error and hard code.




    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using Telerik.OpenAccess;
    using Telerik.Sitefinity.Data;
    using Telerik.Sitefinity.Data.Linq;
    using Telerik.Sitefinity.Localization;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Modules.GenericContent.Data;
    using Telerik.Sitefinity.Security;
    using Telerik.Sitefinity.Security.Model;

    namespace XP.Portal.CMS.Providers.News.Data
    {
        /// <summary>
        /// Open access implementation of the products data provider
        /// </summary>
        [ContentProviderDecorator(typeof(OpenAccessContentDecorator))]
        public class OpenAccessProvider : ReutersDataProvider, IOpenAccessDataProvider
        {
            #region Fields

            private static Assembly[] peristentAssemblies;

            #endregion

            #region Constructors

            /// <summary>
            /// Initializes static members of the <see cref="ReutersProvider"/> class.
            /// </summary>
            static OpenAccessProvider()
            {            
                peristentAssemblies = new Assembly[] { typeof(Telerik.Sitefinity.News.Model.NewsItem).Assembly };            
            }

            protected override void Initialize(string providerName, System.Collections.Specialized.NameValueCollection config, Type managerType)
            {
                base.Initialize(providerName, config, managerType);
            }
                 
            #endregion

            #region IOpenAccessDataProvider

            /// <summary>
            /// Gets or sets the database instance for this provider.
            /// </summary>
            /// <value>The database.</value>
            public Database Database
            {
                get;
                set;
            }

            /// <summary>
            /// Gets a value indicating whether to use implicit transactions.
            /// The recommended value for this property is true.
            /// </summary>
            /// <value>
            /// <c>true</c> if [use implicit transactions]; otherwise, <c>false</c>.
            /// </value>
            public bool UseImplicitTransactions
            {
                get { return true; }
            }

            /// <summary>
            /// The list of all assemblies with persistent classes inside.
            /// Only this list of assemblies will be used, it must be complete!
            /// </summary>
            /// <returns>The persistent assemblies.</returns>
            /// <value>The persistent assemblies.</value>
            public Assembly[] GetPersistentAssemblies()
            {
                return peristentAssemblies;
            }

            #endregion

            #region ProductItem methods

            /// <summary>
            /// Create a product item with random id
            /// </summary>
            /// <returns>Newly created product item in transaction</returns>
            public override Telerik.Sitefinity.News.Model.NewsItem CreateNewsItem()
            {
                throw new NotImplementedException();
            }

            /// <summary>
            /// Create a product item with specific primary key
            /// </summary>
            /// <param name="id">Primary key</param>
            /// <returns>Newly created product item in transaction</returns>
            public override Telerik.Sitefinity.News.Model.NewsItem CreateNewsItem(Guid id)
            {
                var product = new Telerik.Sitefinity.News.Model.NewsItem();
                product.Id = id;
                product.ApplicationName = this.ApplicationName;
                product.Owner = SecurityManager.GetCurrentUserId();
                var dateValue = DateTime.UtcNow;
                product.DateCreated = dateValue;
                product.PublicationDate = dateValue;
                ((IDataItem)product).Provider = this;

                // news permissions inherit form the security root
                var securityRoot = this.GetSecurityRoot();
                if (securityRoot != null)
                {
                    this.providerDecorator.CreatePermissionInheritanceAssociation(securityRoot, product);
                }
                else
                {
                    var msg = Res.Get<SecurityResources>().NoSecurityRoot;
                    msg = string.Format(msg, typeof(Telerik.Sitefinity.News.Model.NewsItem).AssemblyQualifiedName);
                    throw new InvalidOperationException(msg);
                }

                // items with empty guid are used in the UI to get a "blank" data item
                // -> i.e. to fill a data item with default values
                // if this is the case, we leave the item out of the transaction
                if (id != Guid.Empty)
                {
                    this.GetScope().Add(product);
                }

                return product;
            }

            /// <summary>
            /// Get a prodct by primary key
            /// </summary>
            /// <param name="id">Primary key</param>
            /// <returns>Product item in transaction</returns>
            /// <exception cref="T:Telerik.Sitefinity.SitefinityExceptions.ItemNotFoundException">When there is no item with the given primary key</exception>
            public override Telerik.Sitefinity.News.Model.NewsItem GetNewsItem(Guid id)
            {
                throw new NotImplementedException();
            }

            /// <summary>
            /// Get a query of all products in this provider
            /// </summary>
            /// <returns>Query of all products</returns>
            public override IQueryable<Telerik.Sitefinity.News.Model.NewsItem> GetNewsItems()
            {
                List<Telerik.Sitefinity.News.Model.NewsItem> vList = new List<Telerik.Sitefinity.News.Model.NewsItem>();

                for (int i = 0; i < 10; i++)
                {
                    Telerik.Sitefinity.News.Model.NewsItem Item = this.CreateNewsItem(Guid.NewGuid());

                    Item.ApplicationName = this.ApplicationName;
                    Item.Owner = Telerik.Sitefinity.Security.SecurityManager.GetCurrentUserId();
                    var dateValue = DateTime.UtcNow;
                    Item.DateCreated = dateValue;
                    Item.PublicationDate = dateValue;
                    Item.DateCreated = dateValue.AddDays(-10).AddSeconds(120);
                    Item.ExpirationDate = dateValue.AddDays(10).AddSeconds(120);
                    Item.PublicationDate = dateValue.AddSeconds(120);

                    // news permissions inherit form the security root
                    var securityRoot = this.GetSecurityRoot();
                    if (securityRoot != null)
                    {
                        this.providerDecorator.CreatePermissionInheritanceAssociation(securityRoot, Item);
                    }
                    else
                    {
                        var msg = Res.Get<SecurityResources>().NoSecurityRoot;
                        msg = string.Format(msg, typeof(Telerik.Sitefinity.News.Model.NewsItem).AssemblyQualifiedName);
                        throw new InvalidOperationException(msg);
                    }

                    if (Item.Id != Guid.Empty)
                    {
                        this.GetScope().Add(Item);
                    }


                    //Item.Title = "Title " + i.ToString();
                    //Item.Content = "Content from item " + i.ToString();

                    vList.Add(Item);
                }

                return vList.AsQueryable();

                var appName = "/Reuters";
                var query =
                    SitefinityQuery
                    .Get<Telerik.Sitefinity.News.Model.NewsItem>(this, MethodBase.GetCurrentMethod())
                    .Where(b => b.ApplicationName == appName);

                return query;
            }

            /// <summary>
            /// Delete a product
            /// </summary>
            /// <param name="product">Product to delete</param>
            public override void Delete(Telerik.Sitefinity.News.Model.NewsItem product)
            {
                throw new NotImplementedException();
            }

            #endregion
        }
    }

  2. Alon Rotem
    Admin
    Alon Rotem avatar
    2 posts

    Posted 24 Feb 2011 Link to this post

    Hello lcmassena,

    There may be 2 reasons for GetSecurityRoot to return null.
    The first: if your data provider does not implement the ISecuredObject interface.
    The second common reason is that it has never been initialized and created in the first place.
    Note that GetSecurityRoot has an overload with signature:

    public SecurityRoot GetSecurityRoot(bool create)
    ...
    Calling this with argument set to True, should create the security root, if it does not exist.

    If the above does answer not help to solve your issue, please send additional information about your implementation, including the code of your base class ReutersDataProvider.
    Thanks!

    Kind regards,

    Alon Rotem
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. DevCraft banner
Back to Top