Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
Zope
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
Zope
Commits
3b7d7470
Commit
3b7d7470
authored
Apr 27, 2001
by
Evan Simpson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Major changes to path expressions and security.
parent
ca506a2b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
85 additions
and
32 deletions
+85
-32
lib/python/Products/PageTemplates/Expressions.py
lib/python/Products/PageTemplates/Expressions.py
+85
-32
No files found.
lib/python/Products/PageTemplates/Expressions.py
View file @
3b7d7470
...
@@ -89,10 +89,11 @@ Page Template-specific implementation of TALES, with handlers
...
@@ -89,10 +89,11 @@ Page Template-specific implementation of TALES, with handlers
for Python expressions, Python string literals, and paths.
for Python expressions, Python string literals, and paths.
"""
"""
__version__
=
'$Revision: 1.
7
$'
[
11
:
-
2
]
__version__
=
'$Revision: 1.
8
$'
[
11
:
-
2
]
import
re
,
sys
import
re
,
sys
from
TALES
import
Engine
,
CompilerError
,
_valid_name
,
NAME_RE
from
TALES
import
Engine
,
CompilerError
,
_valid_name
,
NAME_RE
,
\
TALESError
,
Undefined
from
string
import
strip
,
split
,
join
,
replace
,
lstrip
from
string
import
strip
,
split
,
join
,
replace
,
lstrip
from
DocumentTemplate.DT_Util
import
TemplateDict
from
DocumentTemplate.DT_Util
import
TemplateDict
from
Acquisition
import
aq_base
from
Acquisition
import
aq_base
...
@@ -132,23 +133,72 @@ def render(ob):
...
@@ -132,23 +133,72 @@ def render(ob):
raise
raise
return
ob
return
ob
path_modifiers
=
{
'if'
:
0
,
'exists'
:
0
,
'nocall'
:
0
}
class
PathExpr
:
class
PathExpr
:
def
__init__
(
self
,
name
,
expr
):
def
__init__
(
self
,
name
,
expr
):
self
.
_s
=
expr
self
.
_s
=
expr
self
.
_name
=
name
self
.
_name
=
name
self
.
_path
=
path
=
split
(
expr
,
'/'
)
self
.
_path
=
path
=
split
(
expr
,
'/'
)
self
.
_base
=
base
=
path
.
pop
(
0
)
front
=
path
.
pop
(
0
)
fronts
=
split
(
replace
(
replace
(
front
,
'('
,
'( '
),
')'
,
' ) '
))
self
.
_base
=
base
=
fronts
.
pop
()
if
not
_valid_name
(
base
):
if
not
_valid_name
(
base
):
raise
CompilerError
,
'Invalid variable name "%s"'
%
base
raise
CompilerError
,
'Invalid variable name "%s"'
%
base
# Parse path modifiers
self
.
modifiers
=
modifiers
=
path_modifiers
.
copy
()
if
fronts
:
if
len
(
fronts
)
<
2
or
(
fronts
.
pop
(
0
)
!=
'('
or
fronts
.
pop
()
!=
')'
):
raise
CompilerError
,
'Invalid path base "%s"'
%
front
for
modifier
in
fronts
:
if
not
modifiers
.
has_key
(
modifier
):
raise
CompilerError
,
(
'Unknown path modifier "%s"'
%
modifier
)
modifiers
[
modifier
]
=
1
# Parse path
self
.
_dynparts
=
dp
=
[]
self
.
_dynparts
=
dp
=
[]
for
i
in
range
(
len
(
path
)):
for
i
in
range
(
len
(
path
)):
e
=
path
[
i
]
e
=
path
[
i
]
if
e
[:
1
]
==
'?'
and
_valid_name
(
e
[
1
:]):
if
e
[:
1
]
==
'?'
and
_valid_name
(
e
[
1
:]):
dp
.
append
((
i
,
e
[
1
:]))
dp
.
append
((
i
,
e
[
1
:]))
dp
.
reverse
()
dp
.
reverse
()
# Choose call method
on
=
modifiers
.
get
callname
=
on
(
'if'
)
+
on
(
'exists'
)
*
2
callname
=
(
'Render'
,
'If'
,
'Exists'
,
'IfExists'
)[
callname
]
if
on
(
'nocall'
)
and
callname
==
'Render'
:
callname
=
''
self
.
_call_name
=
'_eval'
+
callname
def
_evalRender
(
self
,
econtext
):
return
render
(
self
.
_eval
(
econtext
))
def
_evalIf
(
self
,
econtext
):
val
=
self
.
_eval
(
econtext
)
if
not
val
:
return
Undefined
if
self
.
modifiers
.
has_key
(
'nocall'
):
return
val
return
render
(
val
)
def
_evalExists
(
self
,
econtext
):
try
:
self
.
_eval
(
econtext
)
except
(
Undefined
,
'Unauthorized'
):
return
0
return
1
def
__call__
(
self
,
econtext
):
def
_evalIfExists
(
self
,
econtext
):
try
:
val
=
self
.
_eval
(
econtext
)
except
(
Undefined
,
'Unauthorized'
):
return
Undefined
if
self
.
modifiers
.
has_key
(
'nocall'
):
return
val
return
render
(
val
)
def
_eval
(
self
,
econtext
):
base
=
self
.
_base
base
=
self
.
_base
path
=
list
(
self
.
_path
)
# Copy!
path
=
list
(
self
.
_path
)
# Copy!
contexts
=
econtext
.
contexts
contexts
=
econtext
.
contexts
...
@@ -163,24 +213,22 @@ class PathExpr:
...
@@ -163,24 +213,22 @@ class PathExpr:
# of path names.
# of path names.
path
[
i
:
i
+
1
]
=
list
(
val
)
path
[
i
:
i
+
1
]
=
list
(
val
)
try
:
try
:
__traceback_info__
=
base
if
var
.
has_key
(
base
):
if
var
.
has_key
(
base
):
ob
=
var
[
base
]
ob
=
var
[
base
]
else
:
else
:
ob
=
contexts
[
base
]
ob
=
contexts
[
base
]
# Work around lack of security declaration
# Work around lack of security declaration
if
path
and
(
ob
is
contexts
[
'repeat'
]):
if
path
and
(
ob
is
contexts
[
'repeat'
]):
ob
=
ob
[
path
.
pop
(
0
)]
step
=
path
.
pop
(
0
)
ob
=
restrictedTraverse
(
ob
,
path
)
__traceback_info__
=
(
base
,
step
)
except
(
AttributeError
,
KeyError
):
ob
=
ob
[
step
]
if
self
.
_name
==
'exists'
:
return
restrictedTraverse
(
ob
,
path
)
return
0
except
(
AttributeError
,
KeyError
,
TypeError
,
IndexError
),
e
:
raise
raise
Undefined
,
(
e
.
args
,
sys
.
exc_info
()),
sys
.
exc_info
()[
2
]
else
:
if
self
.
_name
==
'exists'
:
def
__call__
(
self
,
econtext
):
return
1
return
getattr
(
self
,
self
.
_call_name
)(
econtext
)
if
self
.
_name
==
'nocall'
:
return
ob
return
render
(
ob
)
def
__str__
(
self
):
def
__str__
(
self
):
return
'%s expression "%s"'
%
(
self
.
_name
,
self
.
_s
)
return
'%s expression "%s"'
%
(
self
.
_name
,
self
.
_s
)
...
@@ -275,12 +323,16 @@ if sys.modules.has_key('Zope'):
...
@@ -275,12 +323,16 @@ if sys.modules.has_key('Zope'):
security
=
getSecurityManager
()
security
=
getSecurityManager
()
security
.
addContext
(
template
)
security
.
addContext
(
template
)
try
:
try
:
__traceback_info__
=
self
.
expr
return
f
()
return
f
()
finally
:
finally
:
security
.
removeContext
(
template
)
security
.
removeContext
(
template
)
def
__str__
(
self
):
def
__str__
(
self
):
return
'Python expression "%s"'
%
self
.
expr
return
'Python expression "%s"'
%
self
.
expr
def
__repr__
(
self
):
return
'<PythonExpr %s>'
%
self
.
expr
else
:
else
:
class
getSecurityManager
:
class
getSecurityManager
:
'''Null security manager'''
'''Null security manager'''
...
@@ -329,31 +381,31 @@ def restrictedTraverse(self, path):
...
@@ -329,31 +381,31 @@ def restrictedTraverse(self, path):
if
not
path
:
return
self
if
not
path
:
return
self
__traceback_info__
=
path
get
=
getattr
get
=
getattr
N
=
None
N
=
None
M
=
[]
#marker
M
=
[]
#marker
REQUEST
=
{
'TraversalRequestNameStack'
:
path
}
REQUEST
=
{
'TraversalRequestNameStack'
:
path
}
path
.
reverse
()
securityManager
=
getSecurityManager
()
pop
=
path
.
pop
securityManager
=
getSecurityManager
(
)
plen
=
len
(
path
)
i
=
0
if
not
path
[
-
1
]:
if
not
path
[
0
]:
# If the path starts with an empty string, go to the root first.
# If the path starts with an empty string, go to the root first.
pop
()
i
=
1
self
=
self
.
getPhysicalRoot
()
self
=
self
.
getPhysicalRoot
()
if
not
securityManager
.
validateValue
(
self
):
if
not
securityManager
.
validateValue
(
self
):
raise
'Unauthorized'
,
name
raise
'Unauthorized'
,
name
object
=
self
object
=
self
while
path
:
while
i
<
plen
:
name
=
pop
()
__traceback_info__
=
(
path
,
i
)
name
=
path
[
i
]
i
=
i
+
1
if
name
[
0
]
==
'_'
:
if
name
[
0
]
==
'_'
:
# Never allowed in a URL.
# Never allowed in a URL.
raise
'NotFound'
,
name
raise
AttributeError
,
name
if
name
==
'..'
:
if
name
==
'..'
:
o
=
getattr
(
object
,
'aq_parent'
,
M
)
o
=
getattr
(
object
,
'aq_parent'
,
M
)
...
@@ -367,11 +419,12 @@ def restrictedTraverse(self, path):
...
@@ -367,11 +419,12 @@ def restrictedTraverse(self, path):
if
t
is
not
N
:
if
t
is
not
N
:
o
=
t
(
REQUEST
,
name
)
o
=
t
(
REQUEST
,
name
)
# Note we pass no container, because we have no
container
=
None
# way of knowing what it is
if
(
hasattr
(
get
(
object
,
'aq_base'
,
object
),
name
)
if
not
securityManager
.
validate
(
object
,
None
,
name
,
o
):
and
get
(
object
,
name
)
is
o
):
container
=
object
if
not
securityManager
.
validate
(
object
,
container
,
name
,
o
):
raise
'Unauthorized'
,
name
raise
'Unauthorized'
,
name
else
:
else
:
o
=
get
(
object
,
name
,
M
)
o
=
get
(
object
,
name
,
M
)
if
o
is
not
M
:
if
o
is
not
M
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment