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