Reputation: 57
If you are running code using the security disabler, is it possible to re-enable security while still in the Using SecurityDisabler?
My problem is I need to test a user's permission but if you are in the SecurityDisabler and use the UserSwitcher to test permissions. It always says they have permissions. So I was hoping to re-enable security to test.
One more piece is that the code is executed when a item enters a workbox and Sitecore seems to run all of that code automatically with the Disabler which makes sense but makes it hard to test permissions.
This is on Sitecore 6.4.1.
Upvotes: 2
Views: 3673
Reputation: 27132
You can use SecurityStateSwitcher
inside the using SecurityDisabler scope
, e.g.:
Item item = Sitecore.Context.Database.GetItem("/sitecore/system");
var canAnonymousRead = item.Security.CanWrite(Sitecore.Context.User); // returns false
using (new SecurityDisabler())
{
canAnonymousRead = item.Security.CanWrite(Sitecore.Context.User); // returns true
using (new SecurityStateSwitcher(SecurityState.Enabled))
{
canAnonymousRead = item.Security.CanWrite(Sitecore.Context.User); // returns false again
}
}
SecurityStateSwitcher
is a base class of SecurityDisabler
(see below) so what you see above is a method for "resetting" security checks back to Enabled
mode.
public class SecurityDisabler : SecurityStateSwitcher
{
public SecurityDisabler : base (SecurityState.Disabled) {}
}
Upvotes: 4