Changeset 2818

Show
Ignore:
Timestamp:
11/23/07 21:47:33 (9 months ago)
Author:
osimons
Message:

AddCommentMacro: Adding form_token and more readable permissions in code.

References #1614

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • addcommentmacro/0.11/addcomment/macro.py

    r2797 r2818  
    4848        page_url = req.href.wiki(resource.id) 
    4949        wikipreview = req.args.get("preview", "") 
     50         
     51        # Can this user add a comment to this page? 
    5052        appendonly = ('appendonly' in args) 
    51         # Can this user add a comment to this page? 
    52         cancomment = not page.readonly 
    53         # Is this an "append-only" comment or are we an administrator? 
    54         if 'WIKI_ADMIN' in req.perm(resource) or appendonly: 
     53        cancomment = False 
     54        if page.readonly: 
     55            if 'WIKI_ADMIN' in req.perm(resource): 
     56                cancomment = True 
     57        elif 'WIKI_MODIFY' in req.perm(resource): 
    5558            cancomment = True 
    56         if not cancomment: 
     59        elif appendonly and 'WIKI_VIEW' in req.perm(resource): 
     60            cancomment = True 
     61        else: 
    5762            raise TracError('Error: Insufficient privileges to AddComment') 
    58         disabled = False 
    5963         
    6064        # Get the data from the POST 
     
    7175         
    7276        the_preview = the_message = the_form = tag() 
    73          
    74         if wikipreview or not ('WIKI_MODIFY' in req.perm(resource) or appendonly): 
    75             disabled = True 
    76          
     77 
    7778        # If we are submitting or previewing, inject comment as it should look 
    7879        if cancomment and comment and (preview or submit): 
     
    8586                                class_="wikipage", id="preview") 
    8687         
     88        # Check the form_token 
     89        form_ok = True 
     90        if submit and req.args.get('__FORM_TOKEN','') != req.form_token: 
     91            form_ok = False 
     92            the_message = tag.div(tag.strong("ERROR: "), 
     93                "AddComment received incorrect form token. " 
     94                "Do you have cookies enabled?", 
     95                class_="system-message") 
     96         
    8797        # When submitting, inject comment before macro 
    88         if comment and submit
     98        if comment and submit and cancomment and form_ok
    8999            submitted = False 
    90100            newtext = "" 
     
    118128                                        name="addcomment", 
    119129                                        cols=80, rows=5, 
    120                                         disabled=(disabled and "disabled" or None)), 
     130                                    disabled=(not cancomment and "disabled" or None)), 
    121131                            class_="field" 
    122132                        ), 
     
    127137                                    size=30, value=authname) 
    128138                        ) or None), 
     139                        tag.input(type="hidden", name="__FORM_TOKEN", 
     140                                        value=req.form_token), 
    129141                        tag.div( 
    130142                            tag.input(value="Add comment", type="submit", 
    131143                                    name="submitaddcomment", size=30, 
    132                                     disabled=(disabled and "disabled" or None)), 
     144                                    disabled=(not cancomment and "disabled" or None)), 
    133145                            tag.input(value="Preview comment", type="submit", 
    134146                                    name="previewaddcomment", 
    135                                     disabled=(disabled and "disabled" or None)), 
     147                                    disabled=(not cancomment and "disabled" or None)), 
    136148                            tag.input(value="Cancel", type="submit", 
    137149                                    name="canceladdcomment", 
    138                                     disabled=(disabled and "disabled" or None)), 
     150                                    disabled=(not cancomment and "disabled" or None)), 
    139151                            class_="buttons" 
    140152                        ), 
     
    145157         
    146158        add_script(req, 'common/js/wikitoolbar.js') 
    147  
     159         
    148160        return tag.div(the_preview, the_message, the_form, id="commenting") 
    149161