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