Plone: run code as admin

need this first:
from zope.component.hooks import getSite
from AccessControl import ClassSecurityInfo, getSecurityManager
from AccessControl.SecurityManagement import newSecurityManager, setSecurityManager
from AccessControl.User import UnrestrictedUser as BaseUnrestrictedUser
class UnrestrictedUser(BaseUnrestrictedUser):
“””Unrestricted user that still has an id.
“””
def getId(self):
“””Return the ID of the user.
“””
return self.getUserName()

then go to your code area:
at the start of your code that you want to run as Manager add these lines:
sm = getSecurityManager()
role = ‘Manager’
tmp_user = UnrestrictedUser(sm.getUser().getId(), ”, [role], ”)
portal=getSite()
tmp_user = tmp_user.__of__(portal.acl_users)
newSecurityManager(None, tmp_user)

then place your codes here …

after that add this line to put security manager back to what it was:

setSecurityManager(sm)

Advertisements
About

Work is fun!

Tagged with: , , , , ,
Posted in archtype, Plone theme, ploneCMS, python, Tips And Tricks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Disclaimer
All content provided on this "tareqalam.com" blog is for informational purposes only. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner of [tareqalam.wordpress.com] will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.
May 2014
M T W T F S S
« Apr   Jun »
 1234
567891011
12131415161718
19202122232425
262728293031  

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 23 other followers

%d bloggers like this: