paul@1 | 1 | # -*- coding: iso-8859-1 -*- |
paul@1 | 2 | """ |
paul@1 | 3 | MoinMoin - ApproveChanges library |
paul@1 | 4 | |
paul@4 | 5 | This library relies on the existence of a user (by default |
paul@4 | 6 | "ApprovalQueueUser") who has sufficient privileges to write pages with ACLs |
paul@4 | 7 | to an approval queue (ACL permissions "write,admin"). |
paul@4 | 8 | |
paul@4 | 9 | If users other than the superuser are to be able to edit pages freely, they |
paul@4 | 10 | must be present in a group (by default "ApprovedGroup"), and if they are to |
paul@4 | 11 | be allowed to review changes, they must be present in a different group (by |
paul@4 | 12 | default "PageReviewersGroup"). |
paul@4 | 13 | |
paul@1 | 14 | @copyright: 2011 by Paul Boddie <paul@boddie.org.uk> |
paul@1 | 15 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@1 | 16 | """ |
paul@1 | 17 | |
paul@4 | 18 | from MoinMoin import user |
paul@10 | 19 | from MoinMoin.Page import Page |
paul@7 | 20 | from MoinMoin.wikiutil import escape |
paul@1 | 21 | |
paul@10 | 22 | try: |
paul@10 | 23 | set |
paul@10 | 24 | except NameError: |
paul@10 | 25 | from sets import Set as set |
paul@10 | 26 | |
paul@1 | 27 | __version__ = "0.1" |
paul@1 | 28 | |
paul@3 | 29 | def get_queued_changes_page(request): |
paul@3 | 30 | return getattr(request.cfg, "queued_changes_page", "ApprovalQueue") |
paul@1 | 31 | |
paul@1 | 32 | def get_approved_editors_group(request): |
paul@1 | 33 | return getattr(request.cfg, "approved_editors_group", "ApprovedGroup") |
paul@1 | 34 | |
paul@4 | 35 | def get_page_reviewers_group(request): |
paul@4 | 36 | return getattr(request.cfg, "reviewers_group", "PageReviewersGroup") |
paul@4 | 37 | |
paul@4 | 38 | def get_queued_changes_user(request): |
paul@4 | 39 | return getattr(request.cfg, "queued_changes_user", "ApprovalQueueUser") |
paul@4 | 40 | |
paul@4 | 41 | def get_secret_key(request): |
paul@4 | 42 | return request.cfg.secrets["wikiutil/tickets"] |
paul@4 | 43 | |
paul@4 | 44 | def is_reviewer(request): |
paul@4 | 45 | return request.user.valid and ( |
paul@4 | 46 | request.dicts.has_member(get_approved_editors_group(request), request.user.name) or \ |
paul@4 | 47 | request.user.isSuperUser()) |
paul@4 | 48 | |
paul@4 | 49 | def is_approved(request): |
paul@4 | 50 | return request.user.valid and ( |
paul@4 | 51 | request.dicts.has_member(get_approved_editors_group(request), request.user.name) or \ |
paul@4 | 52 | request.user.isSuperUser()) |
paul@4 | 53 | |
paul@6 | 54 | def is_queued_changes_user(request): |
paul@6 | 55 | return request.user.valid and request.user.name == get_queued_changes_user(request) |
paul@6 | 56 | |
paul@3 | 57 | def is_queued_changes_page(request, pagename): |
paul@1 | 58 | |
paul@3 | 59 | "Return whether 'pagename' is a queued changes page by testing its name." |
paul@1 | 60 | |
paul@1 | 61 | parts = pagename.split("/") |
paul@3 | 62 | return len(parts) > 1 and parts[-1] == get_queued_changes_page(request) |
paul@1 | 63 | |
paul@1 | 64 | def get_target_page_name(pagename): |
paul@1 | 65 | |
paul@3 | 66 | "Return the target page name for the given queued changes 'pagename'." |
paul@1 | 67 | |
paul@3 | 68 | return "/".join(pagename.split("/")[:-1]) |
paul@1 | 69 | |
paul@4 | 70 | def get_user_for_saving(request): |
paul@4 | 71 | |
paul@4 | 72 | "Return a user that can save pages with ACLs." |
paul@4 | 73 | |
paul@4 | 74 | username = get_queued_changes_user(request) |
paul@4 | 75 | uid = user.getUserId(request, username) |
paul@4 | 76 | |
paul@4 | 77 | # If the user does not exist, just return the existing user. |
paul@4 | 78 | |
paul@4 | 79 | if not uid: |
paul@4 | 80 | return request.user |
paul@4 | 81 | |
paul@4 | 82 | # Otherwise, return the privileged user. |
paul@4 | 83 | |
paul@4 | 84 | return user.User(request, uid) |
paul@4 | 85 | |
paul@10 | 86 | def get_parent_revision_directive(request, pagename): |
paul@4 | 87 | |
paul@4 | 88 | """ |
paul@10 | 89 | Using the 'request', return a parent page revision directive for the page |
paul@10 | 90 | having the given 'pagename'. |
paul@4 | 91 | """ |
paul@4 | 92 | |
paul@10 | 93 | page = Page(request, pagename) |
paul@10 | 94 | return "#parent-revision %s" % page.current_rev() |
paul@10 | 95 | |
paul@10 | 96 | def get_access_control_directive(request): |
paul@10 | 97 | |
paul@10 | 98 | """ |
paul@10 | 99 | Using the 'request', return an ACL directive for use in a page body in order |
paul@10 | 100 | to prevent anyone other than reviewers from seeing it in the queue. |
paul@10 | 101 | """ |
paul@10 | 102 | |
paul@10 | 103 | return "#acl %s:read,write,delete,revert,admin All:" % ( |
paul@10 | 104 | get_page_reviewers_group(request)) |
paul@4 | 105 | |
paul@10 | 106 | def add_directives(body, directives): |
paul@10 | 107 | |
paul@10 | 108 | "Add to the page 'body' the given 'directives'." |
paul@10 | 109 | |
paul@10 | 110 | return "\n".join(directives + [body]) |
paul@4 | 111 | |
paul@10 | 112 | def remove_directives(body, names): |
paul@10 | 113 | |
paul@10 | 114 | """ |
paul@10 | 115 | Remove from the page 'body' the first of each directive using the given |
paul@10 | 116 | 'names'. |
paul@10 | 117 | """ |
paul@4 | 118 | |
paul@9 | 119 | new_body = [] |
paul@9 | 120 | header = 1 |
paul@10 | 121 | found = set() |
paul@9 | 122 | |
paul@9 | 123 | for line in body.split("\n"): |
paul@9 | 124 | if header: |
paul@9 | 125 | |
paul@9 | 126 | # Detect the end of the header. |
paul@4 | 127 | |
paul@9 | 128 | if not line.startswith("#"): |
paul@9 | 129 | header = 0 |
paul@10 | 130 | else: |
paul@10 | 131 | parts = line[1:].split() |
paul@10 | 132 | |
paul@10 | 133 | # Identify any directive. |
paul@10 | 134 | |
paul@10 | 135 | directive = parts[0] |
paul@10 | 136 | |
paul@10 | 137 | if directive in names and directive not in found: |
paul@10 | 138 | found.add(directive) |
paul@10 | 139 | continue |
paul@9 | 140 | |
paul@9 | 141 | new_body.append(line) |
paul@9 | 142 | |
paul@9 | 143 | return "\n".join(new_body) |
paul@4 | 144 | |
paul@1 | 145 | # Utility classes and associated functions. |
paul@1 | 146 | # NOTE: These are a subset of EventAggregatorSupport. |
paul@1 | 147 | |
paul@1 | 148 | class Form: |
paul@1 | 149 | |
paul@1 | 150 | """ |
paul@1 | 151 | A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x |
paul@1 | 152 | environment. |
paul@1 | 153 | """ |
paul@1 | 154 | |
paul@1 | 155 | def __init__(self, form): |
paul@1 | 156 | self.form = form |
paul@1 | 157 | |
paul@1 | 158 | def get(self, name, default=None): |
paul@1 | 159 | values = self.form.getlist(name) |
paul@1 | 160 | if not values: |
paul@1 | 161 | return default |
paul@1 | 162 | else: |
paul@1 | 163 | return values |
paul@1 | 164 | |
paul@1 | 165 | def __getitem__(self, name): |
paul@1 | 166 | return self.form.getlist(name) |
paul@1 | 167 | |
paul@1 | 168 | class ActionSupport: |
paul@1 | 169 | |
paul@1 | 170 | """ |
paul@1 | 171 | Work around disruptive MoinMoin changes in 1.9, and also provide useful |
paul@1 | 172 | convenience methods. |
paul@1 | 173 | """ |
paul@1 | 174 | |
paul@1 | 175 | def get_form(self): |
paul@1 | 176 | return get_form(self.request) |
paul@1 | 177 | |
paul@1 | 178 | def get_form(request): |
paul@1 | 179 | |
paul@1 | 180 | "Work around disruptive MoinMoin changes in 1.9." |
paul@1 | 181 | |
paul@1 | 182 | if hasattr(request, "values"): |
paul@1 | 183 | return Form(request.values) |
paul@1 | 184 | else: |
paul@1 | 185 | return request.form |
paul@1 | 186 | |
paul@7 | 187 | def escattr(s): |
paul@7 | 188 | return escape(s, 1) |
paul@7 | 189 | |
paul@1 | 190 | # vim: tabstop=4 expandtab shiftwidth=4 |