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@25 | 14 | @copyright: 2011, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@25 | 15 | 2013 by Jakub Jedelsky <jedelsky@master.cz> |
paul@21 | 16 | 2003-2007 MoinMoin:ThomasWaldmann, |
paul@21 | 17 | 2003 by Gustavo Niemeyer |
paul@1 | 18 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@1 | 19 | """ |
paul@1 | 20 | |
paul@4 | 21 | from MoinMoin import user |
paul@10 | 22 | from MoinMoin.Page import Page |
paul@21 | 23 | from MoinMoin.PageEditor import PageEditor |
paul@7 | 24 | from MoinMoin.wikiutil import escape |
paul@11 | 25 | import re |
paul@10 | 26 | |
paul@28 | 27 | __version__ = "0.2" |
paul@1 | 28 | |
paul@11 | 29 | space_pattern = re.compile("(\s+)") |
paul@21 | 30 | group_member_pattern = re.compile(ur'^ \* +(?:\[\[)?(?P<member>.+?)(?:\]\])? *$', re.MULTILINE | re.UNICODE) |
paul@11 | 31 | |
paul@28 | 32 | def have_user_specific_queue(request): |
paul@28 | 33 | return getattr(request.cfg, "queued_changes_per_user", False) |
paul@28 | 34 | |
paul@28 | 35 | def get_user_specific_queue(request): |
paul@28 | 36 | return have_user_specific_queue(request) and \ |
paul@28 | 37 | request.user.valid and ("%s/" % request.user.name) or \ |
paul@28 | 38 | "" |
paul@28 | 39 | |
paul@3 | 40 | def get_queued_changes_page(request): |
paul@3 | 41 | return getattr(request.cfg, "queued_changes_page", "ApprovalQueue") |
paul@1 | 42 | |
paul@1 | 43 | def get_approved_editors_group(request): |
paul@1 | 44 | return getattr(request.cfg, "approved_editors_group", "ApprovedGroup") |
paul@1 | 45 | |
paul@4 | 46 | def get_page_reviewers_group(request): |
paul@4 | 47 | return getattr(request.cfg, "reviewers_group", "PageReviewersGroup") |
paul@4 | 48 | |
paul@4 | 49 | def get_queued_changes_user(request): |
paul@4 | 50 | return getattr(request.cfg, "queued_changes_user", "ApprovalQueueUser") |
paul@4 | 51 | |
paul@4 | 52 | def is_reviewer(request): |
paul@4 | 53 | return request.user.valid and ( |
paul@25 | 54 | has_member(request, get_page_reviewers_group(request), request.user.name) or \ |
paul@4 | 55 | request.user.isSuperUser()) |
paul@4 | 56 | |
paul@4 | 57 | def is_approved(request): |
paul@4 | 58 | return request.user.valid and ( |
paul@21 | 59 | user_is_approved(request, request.user.name) or \ |
paul@4 | 60 | request.user.isSuperUser()) |
paul@4 | 61 | |
paul@21 | 62 | def user_is_approved(request, username): |
paul@21 | 63 | return has_member(request, get_approved_editors_group(request), username) |
paul@21 | 64 | |
paul@6 | 65 | def is_queued_changes_user(request): |
paul@6 | 66 | return request.user.valid and request.user.name == get_queued_changes_user(request) |
paul@6 | 67 | |
paul@3 | 68 | def is_queued_changes_page(request, pagename): |
paul@1 | 69 | |
paul@3 | 70 | "Return whether 'pagename' is a queued changes page by testing its name." |
paul@1 | 71 | |
paul@1 | 72 | parts = pagename.split("/") |
paul@3 | 73 | return len(parts) > 1 and parts[-1] == get_queued_changes_page(request) |
paul@1 | 74 | |
paul@28 | 75 | def get_target_page_name(page): |
paul@28 | 76 | |
paul@28 | 77 | "Return the target page name for the given queued changes 'page'." |
paul@1 | 78 | |
paul@28 | 79 | directive = "unapproved-user-queue" |
paul@28 | 80 | body, directives = remove_directives(page.get_raw_body(), [directive]) |
paul@28 | 81 | extra_parts = directives.has_key(directive) and 2 or 1 |
paul@28 | 82 | return "/".join(page.page_name.split("/")[:-extra_parts]) |
paul@1 | 83 | |
paul@4 | 84 | def get_user_for_saving(request): |
paul@4 | 85 | |
paul@4 | 86 | "Return a user that can save pages with ACLs." |
paul@4 | 87 | |
paul@4 | 88 | username = get_queued_changes_user(request) |
paul@4 | 89 | |
paul@4 | 90 | # If the user does not exist, just return the existing user. |
paul@4 | 91 | |
paul@14 | 92 | return get_user(request, username) or request.user |
paul@14 | 93 | |
paul@14 | 94 | def get_user(request, username): |
paul@14 | 95 | |
paul@14 | 96 | "Return the user having the given 'username'." |
paul@14 | 97 | |
paul@14 | 98 | uid = user.getUserId(request, username) |
paul@14 | 99 | |
paul@14 | 100 | # If the user does not exist, just return None. |
paul@14 | 101 | |
paul@4 | 102 | if not uid: |
paul@14 | 103 | return None |
paul@4 | 104 | |
paul@4 | 105 | # Otherwise, return the privileged user. |
paul@4 | 106 | |
paul@4 | 107 | return user.User(request, uid) |
paul@4 | 108 | |
paul@10 | 109 | def get_parent_revision_directive(request, pagename): |
paul@4 | 110 | |
paul@4 | 111 | """ |
paul@10 | 112 | Using the 'request', return a parent page revision directive for the page |
paul@10 | 113 | having the given 'pagename'. |
paul@4 | 114 | """ |
paul@4 | 115 | |
paul@10 | 116 | page = Page(request, pagename) |
paul@10 | 117 | return "#parent-revision %s" % page.current_rev() |
paul@10 | 118 | |
paul@10 | 119 | def get_access_control_directive(request): |
paul@10 | 120 | |
paul@10 | 121 | """ |
paul@10 | 122 | Using the 'request', return an ACL directive for use in a page body in order |
paul@10 | 123 | to prevent anyone other than reviewers from seeing it in the queue. |
paul@10 | 124 | """ |
paul@10 | 125 | |
paul@10 | 126 | return "#acl %s:read,write,delete,revert,admin All:" % ( |
paul@10 | 127 | get_page_reviewers_group(request)) |
paul@4 | 128 | |
paul@14 | 129 | def get_user_directive(request): |
paul@14 | 130 | |
paul@14 | 131 | """ |
paul@14 | 132 | Using the 'request', return a user directive for use in a page body in order |
paul@14 | 133 | to record who saved the changes originally. |
paul@14 | 134 | """ |
paul@14 | 135 | |
paul@14 | 136 | if request.user.valid: |
paul@14 | 137 | return "#unapproved-user %s" % request.user.name |
paul@14 | 138 | else: |
paul@14 | 139 | return "" |
paul@14 | 140 | |
paul@28 | 141 | def get_user_queue_directive(request): |
paul@28 | 142 | |
paul@28 | 143 | """ |
paul@28 | 144 | Using the 'request', return a user directive for use in a page body in order |
paul@28 | 145 | to record who saved the changes originally. |
paul@28 | 146 | """ |
paul@28 | 147 | |
paul@28 | 148 | if request.user.valid and have_user_specific_queue(request): |
paul@28 | 149 | return "#unapproved-user-queue" |
paul@28 | 150 | else: |
paul@28 | 151 | return "" |
paul@28 | 152 | |
paul@10 | 153 | def add_directives(body, directives): |
paul@10 | 154 | |
paul@10 | 155 | "Add to the page 'body' the given 'directives'." |
paul@10 | 156 | |
paul@19 | 157 | return "\n".join([directive for directive in directives if directive] + [body]) |
paul@4 | 158 | |
paul@10 | 159 | def remove_directives(body, names): |
paul@10 | 160 | |
paul@10 | 161 | """ |
paul@11 | 162 | Return a new page body, copying the page 'body' provided but removing the |
paul@11 | 163 | first of each directive having one of the given 'names', along with a |
paul@11 | 164 | dictionary mapping directive names to values. |
paul@10 | 165 | """ |
paul@4 | 166 | |
paul@9 | 167 | new_body = [] |
paul@9 | 168 | header = 1 |
paul@11 | 169 | found = {} |
paul@9 | 170 | |
paul@9 | 171 | for line in body.split("\n"): |
paul@9 | 172 | if header: |
paul@9 | 173 | |
paul@9 | 174 | # Detect the end of the header. |
paul@4 | 175 | |
paul@9 | 176 | if not line.startswith("#"): |
paul@9 | 177 | header = 0 |
paul@11 | 178 | |
paul@11 | 179 | # Process the comment or directive. |
paul@11 | 180 | |
paul@10 | 181 | else: |
paul@11 | 182 | parts = space_pattern.split(line[1:]) |
paul@10 | 183 | |
paul@10 | 184 | # Identify any directive. |
paul@10 | 185 | |
paul@10 | 186 | directive = parts[0] |
paul@10 | 187 | |
paul@13 | 188 | # Obtain the value of the first instance of any directive, |
paul@13 | 189 | # stripping any initial space. |
paul@13 | 190 | |
paul@11 | 191 | if directive in names and not found.has_key(directive): |
paul@13 | 192 | found[directive] = "".join(parts[2:]) |
paul@10 | 193 | continue |
paul@9 | 194 | |
paul@9 | 195 | new_body.append(line) |
paul@9 | 196 | |
paul@11 | 197 | return "\n".join(new_body), found |
paul@4 | 198 | |
paul@21 | 199 | def add_to_group_page(request, username, groupname): |
paul@21 | 200 | |
paul@21 | 201 | """ |
paul@21 | 202 | Using the 'request', add 'username' to 'groupname', changing the group page. |
paul@21 | 203 | This is not the same as adding a member to the group, but it will have the |
paul@21 | 204 | same effect when the group is rescanned. |
paul@21 | 205 | """ |
paul@21 | 206 | |
paul@21 | 207 | _ = request.getText |
paul@21 | 208 | |
paul@21 | 209 | page = PageEditor(request, groupname) |
paul@21 | 210 | body = page.get_raw_body() |
paul@21 | 211 | match = None |
paul@21 | 212 | |
paul@21 | 213 | # Find the last matching span. |
paul@21 | 214 | |
paul@21 | 215 | for match in group_member_pattern.finditer(body): |
paul@21 | 216 | start, end = match.span() |
paul@21 | 217 | |
paul@21 | 218 | # Add a group member to the body. |
paul@21 | 219 | |
paul@21 | 220 | entry = ("\n * %s" % username) |
paul@21 | 221 | |
paul@21 | 222 | if match: |
paul@21 | 223 | body = body[:end] + entry + body[end:] |
paul@21 | 224 | else: |
paul@21 | 225 | body += entry |
paul@21 | 226 | |
paul@21 | 227 | page.saveText(body, 0, comment=_("Added %s to the approved editors group.") % username) |
paul@21 | 228 | |
paul@1 | 229 | # Utility classes and associated functions. |
paul@25 | 230 | # NOTE: These are now present in MoinSupport which should be used in future. |
paul@1 | 231 | |
paul@1 | 232 | class Form: |
paul@1 | 233 | |
paul@1 | 234 | """ |
paul@1 | 235 | A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x |
paul@1 | 236 | environment. |
paul@1 | 237 | """ |
paul@1 | 238 | |
paul@1 | 239 | def __init__(self, form): |
paul@1 | 240 | self.form = form |
paul@1 | 241 | |
paul@1 | 242 | def get(self, name, default=None): |
paul@1 | 243 | values = self.form.getlist(name) |
paul@1 | 244 | if not values: |
paul@1 | 245 | return default |
paul@1 | 246 | else: |
paul@1 | 247 | return values |
paul@1 | 248 | |
paul@1 | 249 | def __getitem__(self, name): |
paul@1 | 250 | return self.form.getlist(name) |
paul@1 | 251 | |
paul@1 | 252 | class ActionSupport: |
paul@1 | 253 | |
paul@1 | 254 | """ |
paul@1 | 255 | Work around disruptive MoinMoin changes in 1.9, and also provide useful |
paul@1 | 256 | convenience methods. |
paul@1 | 257 | """ |
paul@1 | 258 | |
paul@1 | 259 | def get_form(self): |
paul@1 | 260 | return get_form(self.request) |
paul@1 | 261 | |
paul@1 | 262 | def get_form(request): |
paul@1 | 263 | |
paul@1 | 264 | "Work around disruptive MoinMoin changes in 1.9." |
paul@1 | 265 | |
paul@1 | 266 | if hasattr(request, "values"): |
paul@1 | 267 | return Form(request.values) |
paul@1 | 268 | else: |
paul@1 | 269 | return request.form |
paul@1 | 270 | |
paul@7 | 271 | def escattr(s): |
paul@7 | 272 | return escape(s, 1) |
paul@7 | 273 | |
paul@16 | 274 | # More Moin 1.9 compatibility functions. |
paul@16 | 275 | |
paul@16 | 276 | def has_member(request, groupname, username): |
paul@16 | 277 | if hasattr(request.dicts, "has_member"): |
paul@16 | 278 | return request.dicts.has_member(groupname, username) |
paul@16 | 279 | else: |
paul@25 | 280 | return username in request.groups.get(groupname, []) |
paul@16 | 281 | |
paul@1 | 282 | # vim: tabstop=4 expandtab shiftwidth=4 |