用户填写(ploneformgen)表单后,我想使用自定义脚本适配器来调用python脚本来更改用户的本地角色,以便他们不再能够看到表单。换句话说,我想阻止用户填写(或查看)表单两次。
我认为执行此操作的一种方法是调用位于表单文件夹中的脚本permission_changer.py。我在该脚本中的代码是:
container.manage_delLocalRoles((‘bob',))
container.reindexObjectSecurity()
其中'bob'只是一个示例用户,只有全局角色FormFiller(我在ZMI的Security选项卡下创建)和本地角色“Reader”用于表单文件夹。
当我填写表格(具有“私人”状态)作为系统管理员时,脚本被成功调用并且bob失去了他的“读者”本地角色(这是他必须开始的全部),并且他再也看不到形式了。但是,当bob填写表单时,会显示“您没有足够的权限查看此页面。”错误,并且不会删除bob的本地角色。我无法理解为什么 - 我尝试了很多不同的东西:
我通过单击ZMI中脚本的“代理”选项卡更改了permission_changer.py的代理。我将其更改为“管理员”,“系统管理员”和“所有者”,但这并没有解决问题(也没有任何组合)。
我尝试通过在表单文件夹中创建文件permission_changer.py.metdadata来更改代理,包括:
[default]
proxy = Manager
但这也不起作用。
奇怪的是,当我将bob的全局角色更改为Manager,系统管理员,甚至是Viewer或Editor时,问题就会消失,脚本运行得很好(我也可以更改脚本,以便添加和删除任意其他本地角色)。 (这些选项对我来说不是解决方案,因为鲍勃因为他的全球角色,仍然可以看到表格。)
此外,我尝试在安全选项卡下为每个可能的权限授予角色FormFiller角色,但是没有用。
所以,我猜这个问题与代理设置有关,但我无法弄清楚我做错了什么。我经常搜索,我找不到任何人讨论类似的问题。
非常感谢任何帮助!
答案 0 :(得分:0)
处理这种丑陋丑陋的方法可能是访问数据保护程序字段的下载方法并解析其输出以查找要检查的数据。 例如,如果username是添加到表单中的第二个pfg字段,则可以阻止用户进一步填充的自定义脚本适配器
alreadyInDB = False
savedData = ploneformgen.savefield.getSavedFormInputForEdit()
username = request.AUTHENTICATED_USER.getId()
usersInDB = [x.split(',')[1] for x in savedData.split('\r\n') if len(x)>0]
if username in usersInDB:
alreadyInDB = True
if alreadyInDB:
return {'username': 'No way man!'}
答案 1 :(得分:0)
我弄清楚发生了什么,但我不确定如何准确描述它。基本上,我发现通过将脚本称为自定义成功操作(表单>编辑>覆盖),我没有遇到问题。所以我认为通过将脚本称为自定义脚本适配器,我试图在他们仍然使用表单时更改用户的权限,这是不可能的,即使使用管理器代理角色。
我希望有所帮助。如果有人对问题有更准确的描述,那将不胜感激。
答案 2 :(得分:0)
要授予和撤消提交表单的权限,您可以:
# Remove current user of group and redirect to [FORM_PARENT_URL]/landing_page_id'.
# If user is not in group, fail silently and continue.
# Fail if landing_page_id does not exist in form-folder, or one of its parents.
#
# Assumes a page with the ID as declared in `landing_page_id` lives in the
# form-folder's parent (or one of its grand-parents, first found wins),
# and holds the state 'public', so users can view it regardless of their
# group-memberships.
#
# Ment to be used after submission of a PloneFormGen-form with private-state and
# a locally assigned Reader-role for the group, so only group-members can view and
# submit the form.
from Products.CMFCore.utils import getToolByName
group_id = 'Submitters' # change as needed
landing_page_id = 'submitted' # change as needed
portal_groups = getToolByName(ploneformgen, 'portal_groups')
user_id = ploneformgen.memberId()
parent_url = '/'.join(ploneformgen.absolute_url().split('/')[:-1])
redirect_to_url = parent_url + '/' + landing_page_id
# Revoke current user's group-membership:
portal_groups.removePrincipalFromGroup(user_id, group_id)
# Let user land in userland:
request.response.redirect(redirect_to_url)
经过Plone-4.3.11和Products.PloneFormGen-1.7.25的测试