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