| 1 | from trac.util import TracError |
|---|
| 2 | from trac.perm import PermissionError |
|---|
| 3 | |
|---|
| 4 | action_map = {'allow': True, 'deny': False} |
|---|
| 5 | |
|---|
| 6 | def execute(hdf, args, env): |
|---|
| 7 | """Macro API entry point for PagePermission""" |
|---|
| 8 | args = [a.strip() for a in args.split(',')] |
|---|
| 9 | |
|---|
| 10 | if len(args) < 2: |
|---|
| 11 | raise TracError, 'PagePermission macro expects at least 2 arguments' |
|---|
| 12 | |
|---|
| 13 | allow = action_map.get(args[0]) |
|---|
| 14 | |
|---|
| 15 | if allow is None: |
|---|
| 16 | raise TracError, "The first argument to PagePermission must be 'allow' or 'deny'" |
|---|
| 17 | |
|---|
| 18 | username = hdf.getValue('trac.authname', 'anonymous') |
|---|
| 19 | user_in_list = username in args[1:] |
|---|
| 20 | |
|---|
| 21 | if allow != user_in_list: |
|---|
| 22 | raise PermissionError, 'Denied by PagePermission macro' |
|---|
| 23 | |
|---|
| 24 | return '' |
|---|