0

I have a list of Photo items that I'm trying to get from my Glass Mapper model using the following declaration:

SitecoreField("Photos")]
public virtual IEnumerable<Photo> Photos { get; set; }

This set of photos is always empty when I'm not logged into Sitecore because the photos have Sitecore security on them. When I am logged into Sitecore (for example, in edit or preview mode while signed into the admin account), I can grab the photos just fine. Normally, to disable such security I would use the security disabler:

using (new Sitecore.SecurityModel.SecurityDisabler()){...}

I'm unsure of how to combine the security disabler with Glass Mapper to grab sitecore items that have security enabled. Any ideas on how I can disable this security from my Glass Mapper model? Thanks for the help!

3

The underlying service for GlassMapper is Sitecore. Just wrap it with a SecurityDisabler like normal.

You can find an example in the tutorials for GM. http://glass.lu/Mapper/Sc/Tutorials/Tutorial11

    void CommentSubmit_Click(object sender, EventArgs e)
    {
        var contextService = new SitecoreContext();
        var masterService = new SitecoreService("master");

        var page = contextService.GetCurrentItem<CommentPage>();
        //we could also use the model property instead of the line above:
        // var page = this.Model;

        var comment = new Comment();

        //This value will be used for the name of the item
        comment.Name = DateTime.Now.ToString("yy-MM-ddThh-mm-ss");
        comment.Content = CommentContent.Text;
        comment.FullName = CommentName.Text;
        comment.Email = CommentEmail.Text;

        using (new SecurityDisabler())
        {
            masterService.Create(page, comment);
        }

        CommentThankYou.Visible = true;
        comment.Content = string.Empty;
        comment.FullName = string.Empty;
        comment.Email = string.Empty;
    }
  • Yeah, this is pretty much what I ended up going with. I wish there was something cleaner like a Glass attribute you could throw on there but there doesn't appear to be one. – c-bro Mar 10 '17 at 21:31
  • I agree. But it is nice to know Glass is just a wrapper to Sitecore and does not rewrite the Sitecore API. Makes transitioning to glass easier. – Chris Auer Mar 10 '17 at 21:51
2

I would recommend using user switcher instead of security disabler.

Sitecore.Security.Accounts.User user = Sitecore.Security.Accounts.User.FromName(@"(sitecore user with permission)", true);
        using (new Sitecore.Security.Accounts.UserSwitcher(user))
        {}
  • Can you elaborate on why to do this to make the answer more valuable? – Gatogordo Mar 11 '17 at 10:52
  • Hi Gert, Sitecore has recommend to use user switcher over security disabler on best practise because it allows switching to a particular user instead of disabling security layer restrictions completely, which enforces need-based access. – Arpit Mar 11 '17 at 12:59
0

After looking at it a little bit more this is what I went with. It's ugly but it gets the job done. I'm still very much open to a cleaner solution.

    [SitecoreField("Photos")]
    public virtual IEnumerable<Photo> Photos {
        get {
            List<Photo> photos = new List<Photo>();
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                var contextService = new SitecoreContext();
                Item page = contextService.GetItem<Item>(this.Id);
                MultilistField list = page.Fields["Photos"];
                foreach (Item child in list.GetItems())
                {
                    Photo currPhoto = contextService.Cast<Photo>(child);
                    photos.Add(currPhoto);
                }
            }
            return photos;
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.