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@11 | 21 | import re |
paul@10 | 22 | |
paul@1 | 23 | __version__ = "0.1" |
paul@1 | 24 | |
paul@11 | 25 | space_pattern = re.compile("(\s+)") |
paul@11 | 26 | |
paul@3 | 27 | def get_queued_changes_page(request): |
paul@3 | 28 | return getattr(request.cfg, "queued_changes_page", "ApprovalQueue") |
paul@1 | 29 | |
paul@1 | 30 | def get_approved_editors_group(request): |
paul@1 | 31 | return getattr(request.cfg, "approved_editors_group", "ApprovedGroup") |
paul@1 | 32 | |
paul@4 | 33 | def get_page_reviewers_group(request): |
paul@4 | 34 | return getattr(request.cfg, "reviewers_group", "PageReviewersGroup") |
paul@4 | 35 | |
paul@4 | 36 | def get_queued_changes_user(request): |
paul@4 | 37 | return getattr(request.cfg, "queued_changes_user", "ApprovalQueueUser") |
paul@4 | 38 | |
paul@4 | 39 | def get_secret_key(request): |
paul@4 | 40 | return request.cfg.secrets["wikiutil/tickets"] |
paul@4 | 41 | |
paul@4 | 42 | def is_reviewer(request): |
paul@4 | 43 | return request.user.valid and ( |
paul@16 | 44 | has_member(request, get_approved_editors_group(request), request.user.name) or \ |
paul@4 | 45 | request.user.isSuperUser()) |
paul@4 | 46 | |
paul@4 | 47 | def is_approved(request): |
paul@4 | 48 | return request.user.valid and ( |
paul@16 | 49 | has_member(request, get_approved_editors_group(request), request.user.name) or \ |
paul@4 | 50 | request.user.isSuperUser()) |
paul@4 | 51 | |
paul@6 | 52 | def is_queued_changes_user(request): |
paul@6 | 53 | return request.user.valid and request.user.name == get_queued_changes_user(request) |
paul@6 | 54 | |
paul@3 | 55 | def is_queued_changes_page(request, pagename): |
paul@1 | 56 | |
paul@3 | 57 | "Return whether 'pagename' is a queued changes page by testing its name." |
paul@1 | 58 | |
paul@1 | 59 | parts = pagename.split("/") |
paul@3 | 60 | return len(parts) > 1 and parts[-1] == get_queued_changes_page(request) |
paul@1 | 61 | |
paul@1 | 62 | def get_target_page_name(pagename): |
paul@1 | 63 | |
paul@3 | 64 | "Return the target page name for the given queued changes 'pagename'." |
paul@1 | 65 | |
paul@3 | 66 | return "/".join(pagename.split("/")[:-1]) |
paul@1 | 67 | |
paul@4 | 68 | def get_user_for_saving(request): |
paul@4 | 69 | |
paul@4 | 70 | "Return a user that can save pages with ACLs." |
paul@4 | 71 | |
paul@4 | 72 | username = get_queued_changes_user(request) |
paul@4 | 73 | |
paul@4 | 74 | # If the user does not exist, just return the existing user. |
paul@4 | 75 | |
paul@14 | 76 | return get_user(request, username) or request.user |
paul@14 | 77 | |
paul@14 | 78 | def get_user(request, username): |
paul@14 | 79 | |
paul@14 | 80 | "Return the user having the given 'username'." |
paul@14 | 81 | |
paul@14 | 82 | uid = user.getUserId(request, username) |
paul@14 | 83 | |
paul@14 | 84 | # If the user does not exist, just return None. |
paul@14 | 85 | |
paul@4 | 86 | if not uid: |
paul@14 | 87 | return None |
paul@4 | 88 | |
paul@4 | 89 | # Otherwise, return the privileged user. |
paul@4 | 90 | |
paul@4 | 91 | return user.User(request, uid) |
paul@4 | 92 | |
paul@10 | 93 | def get_parent_revision_directive(request, pagename): |
paul@4 | 94 | |
paul@4 | 95 | """ |
paul@10 | 96 | Using the 'request', return a parent page revision directive for the page |
paul@10 | 97 | having the given 'pagename'. |
paul@4 | 98 | """ |
paul@4 | 99 | |
paul@10 | 100 | page = Page(request, pagename) |
paul@10 | 101 | return "#parent-revision %s" % page.current_rev() |
paul@10 | 102 | |
paul@10 | 103 | def get_access_control_directive(request): |
paul@10 | 104 | |
paul@10 | 105 | """ |
paul@10 | 106 | Using the 'request', return an ACL directive for use in a page body in order |
paul@10 | 107 | to prevent anyone other than reviewers from seeing it in the queue. |
paul@10 | 108 | """ |
paul@10 | 109 | |
paul@10 | 110 | return "#acl %s:read,write,delete,revert,admin All:" % ( |
paul@10 | 111 | get_page_reviewers_group(request)) |
paul@4 | 112 | |
paul@14 | 113 | def get_user_directive(request): |
paul@14 | 114 | |
paul@14 | 115 | """ |
paul@14 | 116 | Using the 'request', return a user directive for use in a page body in order |
paul@14 | 117 | to record who saved the changes originally. |
paul@14 | 118 | """ |
paul@14 | 119 | |
paul@14 | 120 | if request.user.valid: |
paul@14 | 121 | return "#unapproved-user %s" % request.user.name |
paul@14 | 122 | else: |
paul@14 | 123 | return "" |
paul@14 | 124 | |
paul@10 | 125 | def add_directives(body, directives): |
paul@10 | 126 | |
paul@10 | 127 | "Add to the page 'body' the given 'directives'." |
paul@10 | 128 | |
paul@19 | 129 | return "\n".join([directive for directive in directives if directive] + [body]) |
paul@4 | 130 | |
paul@10 | 131 | def remove_directives(body, names): |
paul@10 | 132 | |
paul@10 | 133 | """ |
paul@11 | 134 | Return a new page body, copying the page 'body' provided but removing the |
paul@11 | 135 | first of each directive having one of the given 'names', along with a |
paul@11 | 136 | dictionary mapping directive names to values. |
paul@10 | 137 | """ |
paul@4 | 138 | |
paul@9 | 139 | new_body = [] |
paul@9 | 140 | header = 1 |
paul@11 | 141 | found = {} |
paul@9 | 142 | |
paul@9 | 143 | for line in body.split("\n"): |
paul@9 | 144 | if header: |
paul@9 | 145 | |
paul@9 | 146 | # Detect the end of the header. |
paul@4 | 147 | |
paul@9 | 148 | if not line.startswith("#"): |
paul@9 | 149 | header = 0 |
paul@11 | 150 | |
paul@11 | 151 | # Process the comment or directive. |
paul@11 | 152 | |
paul@10 | 153 | else: |
paul@11 | 154 | parts = space_pattern.split(line[1:]) |
paul@10 | 155 | |
paul@10 | 156 | # Identify any directive. |
paul@10 | 157 | |
paul@10 | 158 | directive = parts[0] |
paul@10 | 159 | |
paul@13 | 160 | # Obtain the value of the first instance of any directive, |
paul@13 | 161 | # stripping any initial space. |
paul@13 | 162 | |
paul@11 | 163 | if directive in names and not found.has_key(directive): |
paul@13 | 164 | found[directive] = "".join(parts[2:]) |
paul@10 | 165 | continue |
paul@9 | 166 | |
paul@9 | 167 | new_body.append(line) |
paul@9 | 168 | |
paul@11 | 169 | return "\n".join(new_body), found |
paul@4 | 170 | |
paul@1 | 171 | # Utility classes and associated functions. |
paul@1 | 172 | # NOTE: These are a subset of EventAggregatorSupport. |
paul@1 | 173 | |
paul@1 | 174 | class Form: |
paul@1 | 175 | |
paul@1 | 176 | """ |
paul@1 | 177 | A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x |
paul@1 | 178 | environment. |
paul@1 | 179 | """ |
paul@1 | 180 | |
paul@1 | 181 | def __init__(self, form): |
paul@1 | 182 | self.form = form |
paul@1 | 183 | |
paul@1 | 184 | def get(self, name, default=None): |
paul@1 | 185 | values = self.form.getlist(name) |
paul@1 | 186 | if not values: |
paul@1 | 187 | return default |
paul@1 | 188 | else: |
paul@1 | 189 | return values |
paul@1 | 190 | |
paul@1 | 191 | def __getitem__(self, name): |
paul@1 | 192 | return self.form.getlist(name) |
paul@1 | 193 | |
paul@1 | 194 | class ActionSupport: |
paul@1 | 195 | |
paul@1 | 196 | """ |
paul@1 | 197 | Work around disruptive MoinMoin changes in 1.9, and also provide useful |
paul@1 | 198 | convenience methods. |
paul@1 | 199 | """ |
paul@1 | 200 | |
paul@1 | 201 | def get_form(self): |
paul@1 | 202 | return get_form(self.request) |
paul@1 | 203 | |
paul@1 | 204 | def get_form(request): |
paul@1 | 205 | |
paul@1 | 206 | "Work around disruptive MoinMoin changes in 1.9." |
paul@1 | 207 | |
paul@1 | 208 | if hasattr(request, "values"): |
paul@1 | 209 | return Form(request.values) |
paul@1 | 210 | else: |
paul@1 | 211 | return request.form |
paul@1 | 212 | |
paul@7 | 213 | def escattr(s): |
paul@7 | 214 | return escape(s, 1) |
paul@7 | 215 | |
paul@16 | 216 | # More Moin 1.9 compatibility functions. |
paul@16 | 217 | |
paul@16 | 218 | def has_member(request, groupname, username): |
paul@16 | 219 | if hasattr(request.dicts, "has_member"): |
paul@16 | 220 | return request.dicts.has_member(groupname, username) |
paul@16 | 221 | else: |
paul@16 | 222 | return username in request.dicts.get(groupname, []) |
paul@16 | 223 | |
paul@1 | 224 | # vim: tabstop=4 expandtab shiftwidth=4 |