Python script to modify the permision on photos
Hello, I have a very basic script to write and I facing the some difficulties to understand the Zope Api. My problem is the following : I have tones of photo in several PhotoAlbums that do not have the correct permision. I want to allow only a specific role to view those photos. I found out how to do it in interactive mode, using (instance:8080/manage), but with this method is way too long so I have decided to write a script that will recursively change the permision on all the photo in a branch of my web site tree. So far I already play around in "spe" and get the following: ++++++++++++++++++++++module++++++++++++++++++++++++++ #! /usr/bin/env python import os, sys sys.path.append("/usr/lib/zope/lib/python") import Zope Zope.configure("/home/yml/myZopeInstances/sitePerso/etc/zope.conf") app = Zope.app() #app.sitePerso.Albums.Photo_Album_la.get("Photo_Album.Apt") ------------------In Shell------------------------------------------------------------- appart = app.sitePerso.Albums.Photo_Album_la.get("Photo_Album.Apt") albumsChantier = appart.get("Photo_Album.chantier") chant3 = albumsChantier.get("Photo_Album.chan3")
chant3.objectItems()
[('Chantier 052.jpg', <Photo at /sitePerso/Albums/Photo_Album_la/Photo_Album.Apt/Photo_Album.chantier/Photo_Album.chan3/Chantier 052.jpg>), ('Chantier 053.jpg', <Photo at /sitePerso/Albums/Photo_Album_la/Photo_Album.Apt/Photo_Album.chantier/Photo_Album.chan3/Chantier 053.jpg>), ... cut.... ]
photo = chant3.get('Chantier 053.jpg') photo.manage_permission("View", "MyRole",1) I have several questions concerning this code: How can I check, by script, that I have done a modification on the permission? Please I am stuck And I do not know where to search I have googled the whole evening without any success. Thank you for your help.
kbond wrote:
So far I already play around in "spe" and get the following: ++++++++++++++++++++++module++++++++++++++++++++++++++ #! /usr/bin/env python
import os, sys sys.path.append("/usr/lib/zope/lib/python") import Zope Zope.configure("/home/yml/myZopeInstances/sitePerso/etc/zope.conf") app = Zope.app()
You could do the work in a Script (Python) too, then you don't have to do this dance, or worry about managing transactions, conflict errors, and the rest for yourself...
photo = chant3.get('Chantier 053.jpg') photo.manage_permission("View", "MyRole",1)
I have several questions concerning this code: How can I check, by script, that I have done a modification on the permission?
That's pretty involved. The only way I could find to do it was with this function: def checkSettings(object,permissionname,acquire=0,roles=[]): # check the roles and acquire settings for a permission on an # object are as expected happy=0 for pstuff in object.ac_inherited_permissions(1): name,value = pstuff[:2] if name==permissionname: p = Permission(name,value,object) groles=p.getRoles(default=[]) acquired=isinstance(groles,types.ListType) expected={} for role in roles: expected[role]=1 got={} for role in groles: got[role]=1 self.assertEqual((acquire,expected),(acquired,got)) happy=1 if not happy: raise ValueError,"'%s' not found in permissions: %s" % ( permissionname,all_names ) So, to test your edit above, you would do: checkSettings(photo,'View',1,['MyRole'])
Please I am stuck And I do not know where to search I have googled the whole evening without any success.
Now, if you're wondering why your changes aren't "sticking", see what I said about Script (Python)'s. your need to do a: get_transaction().commit() ..after you've made your changes, but the n you gotta worry abotu conflict errors.. cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
Chris Withers wrote:
kbond wrote:
So far I already play around in "spe" and get the following: ++++++++++++++++++++++module++++++++++++++++++++++++++ #! /usr/bin/env python
import os, sys sys.path.append("/usr/lib/zope/lib/python") import Zope Zope.configure("/home/yml/myZopeInstances/sitePerso/etc/zope.conf") app = Zope.app()
You could do the work in a Script (Python) too, then you don't have to do this dance, or worry about managing transactions, conflict errors, and the rest for yourself...
I am not sure to understand the previous statement could you please be more explicit?
photo = chant3.get('Chantier 053.jpg') photo.manage_permission("View", "MyRole",1)
I have several questions concerning this code: How can I check, by script, that I have done a modification on the permission?
That's pretty involved. The only way I could find to do it was with this function:
def checkSettings(object,permissionname,acquire=0,roles=[]): # check the roles and acquire settings for a permission on an # object are as expected happy=0 for pstuff in object.ac_inherited_permissions(1): name,value = pstuff[:2] if name==permissionname: p = Permission(name,value,object) groles=p.getRoles(default=[]) acquired=isinstance(groles,types.ListType) expected={} for role in roles: expected[role]=1 got={} for role in groles: got[role]=1 self.assertEqual((acquire,expected),(acquired,got)) happy=1 if not happy: raise ValueError,"'%s' not found in permissions: %s" % ( permissionname,all_names )
So, to test your edit above, you would do: checkSettings(photo,'View',1,['MyRole'])
Please I am stuck And I do not know where to search I have googled the whole evening without any success.
Now, if you're wondering why your changes aren't "sticking", see what I said about Script (Python)'s.
your need to do a:
get_transaction().commit()
..after you've made your changes, but the n you gotta worry abotu conflict errors..
cheers,
Chris
Thank you for your help. I now need to digest what you sent to me that look nice but for some unexpected reason I am not yet able to get it up and running. I guess this is a matter playing a bit with your function in order to taylor it to meet my expectation. There is still a couple of thing that are unclear to me: -How can I list all the role available on an object? -How can I get the permissions list granted on an object? - Which module should I import in my script in order to run your function? - Is there somewhere a nice tutorial that will help me to be familiar with the ZOPE API? As I guess you noticed I am a newbie in the zope world so I am sorry asking stupid things Cheers,
kbond wrote:
So far I already play around in "spe" and get the following: ++++++++++++++++++++++module++++++++++++++++++++++++++ #! /usr/bin/env python
import os, sys sys.path.append("/usr/lib/zope/lib/python") import Zope Zope.configure("/home/yml/myZopeInstances/sitePerso/etc/zope.conf") app = Zope.app()
You could do the work in a Script (Python) too, then you don't have to do this dance, or worry about managing transactions, conflict errors, and the rest for yourself...
I am not sure to understand the previous statement could you please be more explicit?
Your script is something you store on the filesystem and run from a shell prompt. That means you have to do all the ZODB setup work and management yourself. If you wrote a Script(Python) through the ZMI, it could do the same work but all the setup and ZODB management would be done for you by Zope's publisher.
-How can I list all the role available on an object?
from AccessControl import getSecurityManager getSecurityManager().getUser().getRolesInContext(object)
-How can I get the permissions list granted on an object?
Bwahahahaha ;-) from AccessControl.Permission import Permission from types import ListType result = [] for pstuff in object.ac_inherited_permissions(1): name,value = pstuff[:2] p = Permission(name,value,oject) groles = p.getRoles(default=[]) acquired = isinstance(groles,ListType) result.append({'permission':name,'roles':groles,'acquire':acquired)) Which will leave result somthing like:
print result [{'permission':'View','roles':['MyRole'],'acquire':1}]
- Which module should I import in my script in order to run your function?
for the original function: from AccessControl.Permission import Permission import types
- Is there somewhere a nice tutorial that will help me to be familiar with the ZOPE API?
Bwahahahhahahahaha - no. Sorry :-(
As I guess you noticed I am a newbie in the zope world so I am sorry asking stupid things
Don't worry, your questions point out some interesting gaps in current Zope docs and interfaces... Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
kbond wrote at 2004-5-26 00:06 -0400:
... How can I check, by script, that I have done a modification on the permission?
That sounds like a funny question. You should know that you modified something. Why do you want to check for it? When you want to check whether the action had the desired effect: there is also a method to read the current permission assignment. Use it...
Please I am stuck And I do not know where to search I have googled the whole evening without any success.
Look at the source: "AccessControl.Role.RoleManager". Check what methods are available. Read their (so called) docstrings (the string immediately following the definition). -- Dieter
participants (3)
-
Chris Withers -
Dieter Maurer -
kbond