vendor/CMF/1.6.3/CMFCore

view UndoTool.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ Basic undo tool.
15 $Id$
16 """
18 from AccessControl import ClassSecurityInfo
19 from Globals import DTMLFile
20 from Globals import InitializeClass
21 from OFS.SimpleItem import SimpleItem
23 from ActionInformation import ActionInformation
24 from ActionProviderBase import ActionProviderBase
25 from exceptions import AccessControl_Unauthorized
26 from Expression import Expression
27 from interfaces.portal_undo import portal_undo as IUndoTool
28 from permissions import ListUndoableChanges
29 from permissions import ManagePortal
30 from utils import _checkPermission
31 from utils import _dtmldir
32 from utils import _getAuthenticatedUser
33 from utils import UniqueObject
36 class UndoTool (UniqueObject, SimpleItem, ActionProviderBase):
37 """ This tool is used to undo changes.
38 """
40 __implements__ = (IUndoTool, ActionProviderBase.__implements__)
42 id = 'portal_undo'
43 meta_type = 'CMF Undo Tool'
44 _actions = (ActionInformation(id='undo'
45 , title='Undo'
46 , action=Expression(
47 text='string:${portal_url}/undo_form')
48 , condition=Expression(
49 text='member')
50 , permissions=(ListUndoableChanges,)
51 , category='global'
52 , visible=1
53 )
54 ,
55 )
57 security = ClassSecurityInfo()
59 manage_options = ( ActionProviderBase.manage_options +
60 SimpleItem.manage_options +
61 ({ 'label' : 'Overview', 'action' : 'manage_overview' }
62 ,
63 ))
64 #
65 # ZMI methods
66 #
67 security.declareProtected(ManagePortal, 'manage_overview')
68 manage_overview = DTMLFile( 'explainUndoTool', _dtmldir )
70 #
71 # 'portal_undo' interface methods
72 #
73 security.declareProtected(ListUndoableChanges, 'listUndoableTransactionsFor')
74 def listUndoableTransactionsFor(self, object,
75 first_transaction=None,
76 last_transaction=None,
77 PrincipiaUndoBatchSize=None):
78 '''Lists all transaction IDs the user is allowed to undo.
79 '''
80 # arg list for undoable_transactions() changed in Zope 2.2.
81 portal = self.aq_inner.aq_parent
82 transactions = portal.undoable_transactions(
83 first_transaction=first_transaction,
84 last_transaction=last_transaction,
85 PrincipiaUndoBatchSize=PrincipiaUndoBatchSize)
86 for t in transactions:
87 # Ensure transaction ids don't have embedded LF.
88 t['id'] = t['id'].replace('\n', '')
89 if not _checkPermission(ManagePortal, portal):
90 # Filter out transactions done by other members of the portal.
91 user_id = _getAuthenticatedUser(self).getId()
92 transactions = filter(
93 lambda record, user_id=user_id:
94 record['user_name'].split()[-1] == user_id,
95 transactions
96 )
97 return transactions
99 security.declarePublic('undo')
100 def undo(self, object, transaction_info):
101 """
102 Undo the list of transactions passed in 'transaction_info',
103 first verifying that the current user is allowed to undo them.
104 """
105 # Belt and suspenders: make sure that the user is actually
106 # allowed to undo the transation(s) in transaction_info.
108 xids = {} # set of allowed transaction IDs
110 allowed = self.listUndoableTransactionsFor( object )
112 for xid in map( lambda x: x['id'], allowed ):
113 xids[xid] = 1
115 if type( transaction_info ) == type( '' ):
116 transaction_info = [ transaction_info ]
118 for tinfo in transaction_info:
119 if not xids.get( tinfo, None ):
120 raise AccessControl_Unauthorized
122 object.manage_undo_transactions(transaction_info)
124 InitializeClass(UndoTool)