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@32 | 21 | from MoinSupport import Form, ActionSupport, get_form, escattr, groupHasMember |
paul@11 | 22 | import re |
paul@10 | 23 | |
paul@28 | 24 | __version__ = "0.2" |
paul@1 | 25 | |
paul@11 | 26 | space_pattern = re.compile("(\s+)") |
paul@21 | 27 | group_member_pattern = re.compile(ur'^ \* +(?:\[\[)?(?P<member>.+?)(?:\]\])? *$', re.MULTILINE | re.UNICODE) |
paul@11 | 28 | |
paul@28 | 29 | def have_user_specific_queue(request): |
paul@28 | 30 | return getattr(request.cfg, "queued_changes_per_user", False) |
paul@28 | 31 | |
paul@28 | 32 | def get_user_specific_queue(request): |
paul@28 | 33 | return have_user_specific_queue(request) and \ |
paul@28 | 34 | request.user.valid and ("%s/" % request.user.name) or \ |
paul@28 | 35 | "" |
paul@28 | 36 | |
paul@3 | 37 | def get_queued_changes_page(request): |
paul@3 | 38 | return getattr(request.cfg, "queued_changes_page", "ApprovalQueue") |
paul@1 | 39 | |
paul@1 | 40 | def get_approved_editors_group(request): |
paul@1 | 41 | return getattr(request.cfg, "approved_editors_group", "ApprovedGroup") |
paul@1 | 42 | |
paul@4 | 43 | def get_page_reviewers_group(request): |
paul@4 | 44 | return getattr(request.cfg, "reviewers_group", "PageReviewersGroup") |
paul@4 | 45 | |
paul@4 | 46 | def is_reviewer(request): |
paul@4 | 47 | return request.user.valid and ( |
paul@32 | 48 | groupHasMember(request, get_page_reviewers_group(request), request.user.name) or \ |
paul@4 | 49 | request.user.isSuperUser()) |
paul@4 | 50 | |
paul@4 | 51 | def is_approved(request): |
paul@4 | 52 | return request.user.valid and ( |
paul@21 | 53 | user_is_approved(request, request.user.name) or \ |
paul@4 | 54 | request.user.isSuperUser()) |
paul@4 | 55 | |
paul@21 | 56 | def user_is_approved(request, username): |
paul@32 | 57 | return groupHasMember(request, get_approved_editors_group(request), username) |
paul@21 | 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@28 | 66 | def get_target_page_name(page): |
paul@28 | 67 | |
paul@28 | 68 | "Return the target page name for the given queued changes 'page'." |
paul@1 | 69 | |
paul@28 | 70 | directive = "unapproved-user-queue" |
paul@28 | 71 | body, directives = remove_directives(page.get_raw_body(), [directive]) |
paul@28 | 72 | extra_parts = directives.has_key(directive) and 2 or 1 |
paul@28 | 73 | return "/".join(page.page_name.split("/")[:-extra_parts]) |
paul@1 | 74 | |
paul@14 | 75 | def get_user(request, username): |
paul@14 | 76 | |
paul@14 | 77 | "Return the user having the given 'username'." |
paul@14 | 78 | |
paul@14 | 79 | uid = user.getUserId(request, username) |
paul@14 | 80 | |
paul@14 | 81 | # If the user does not exist, just return None. |
paul@14 | 82 | |
paul@4 | 83 | if not uid: |
paul@14 | 84 | return None |
paul@4 | 85 | |
paul@4 | 86 | # Otherwise, return the privileged user. |
paul@4 | 87 | |
paul@4 | 88 | return user.User(request, uid) |
paul@4 | 89 | |
paul@10 | 90 | def get_parent_revision_directive(request, pagename): |
paul@4 | 91 | |
paul@4 | 92 | """ |
paul@10 | 93 | Using the 'request', return a parent page revision directive for the page |
paul@10 | 94 | having the given 'pagename'. |
paul@4 | 95 | """ |
paul@4 | 96 | |
paul@10 | 97 | page = Page(request, pagename) |
paul@10 | 98 | return "#parent-revision %s" % page.current_rev() |
paul@10 | 99 | |
paul@10 | 100 | def get_access_control_directive(request): |
paul@10 | 101 | |
paul@10 | 102 | """ |
paul@10 | 103 | Using the 'request', return an ACL directive for use in a page body in order |
paul@10 | 104 | to prevent anyone other than reviewers from seeing it in the queue. |
paul@10 | 105 | """ |
paul@10 | 106 | |
paul@10 | 107 | return "#acl %s:read,write,delete,revert,admin All:" % ( |
paul@10 | 108 | get_page_reviewers_group(request)) |
paul@4 | 109 | |
paul@14 | 110 | def get_user_directive(request): |
paul@14 | 111 | |
paul@14 | 112 | """ |
paul@14 | 113 | Using the 'request', return a user directive for use in a page body in order |
paul@14 | 114 | to record who saved the changes originally. |
paul@14 | 115 | """ |
paul@14 | 116 | |
paul@14 | 117 | if request.user.valid: |
paul@14 | 118 | return "#unapproved-user %s" % request.user.name |
paul@14 | 119 | else: |
paul@14 | 120 | return "" |
paul@14 | 121 | |
paul@28 | 122 | def get_user_queue_directive(request): |
paul@28 | 123 | |
paul@28 | 124 | """ |
paul@28 | 125 | Using the 'request', return a user directive for use in a page body in order |
paul@28 | 126 | to record who saved the changes originally. |
paul@28 | 127 | """ |
paul@28 | 128 | |
paul@28 | 129 | if request.user.valid and have_user_specific_queue(request): |
paul@28 | 130 | return "#unapproved-user-queue" |
paul@28 | 131 | else: |
paul@28 | 132 | return "" |
paul@28 | 133 | |
paul@10 | 134 | def add_directives(body, directives): |
paul@10 | 135 | |
paul@10 | 136 | "Add to the page 'body' the given 'directives'." |
paul@10 | 137 | |
paul@19 | 138 | return "\n".join([directive for directive in directives if directive] + [body]) |
paul@4 | 139 | |
paul@10 | 140 | def remove_directives(body, names): |
paul@10 | 141 | |
paul@10 | 142 | """ |
paul@11 | 143 | Return a new page body, copying the page 'body' provided but removing the |
paul@11 | 144 | first of each directive having one of the given 'names', along with a |
paul@11 | 145 | dictionary mapping directive names to values. |
paul@10 | 146 | """ |
paul@4 | 147 | |
paul@9 | 148 | new_body = [] |
paul@9 | 149 | header = 1 |
paul@11 | 150 | found = {} |
paul@9 | 151 | |
paul@9 | 152 | for line in body.split("\n"): |
paul@9 | 153 | if header: |
paul@9 | 154 | |
paul@9 | 155 | # Detect the end of the header. |
paul@4 | 156 | |
paul@9 | 157 | if not line.startswith("#"): |
paul@9 | 158 | header = 0 |
paul@11 | 159 | |
paul@11 | 160 | # Process the comment or directive. |
paul@11 | 161 | |
paul@10 | 162 | else: |
paul@11 | 163 | parts = space_pattern.split(line[1:]) |
paul@10 | 164 | |
paul@10 | 165 | # Identify any directive. |
paul@10 | 166 | |
paul@10 | 167 | directive = parts[0] |
paul@10 | 168 | |
paul@13 | 169 | # Obtain the value of the first instance of any directive, |
paul@13 | 170 | # stripping any initial space. |
paul@13 | 171 | |
paul@11 | 172 | if directive in names and not found.has_key(directive): |
paul@13 | 173 | found[directive] = "".join(parts[2:]) |
paul@10 | 174 | continue |
paul@9 | 175 | |
paul@9 | 176 | new_body.append(line) |
paul@9 | 177 | |
paul@11 | 178 | return "\n".join(new_body), found |
paul@4 | 179 | |
paul@21 | 180 | def add_to_group_page(request, username, groupname): |
paul@21 | 181 | |
paul@21 | 182 | """ |
paul@21 | 183 | Using the 'request', add 'username' to 'groupname', changing the group page. |
paul@21 | 184 | This is not the same as adding a member to the group, but it will have the |
paul@21 | 185 | same effect when the group is rescanned. |
paul@21 | 186 | """ |
paul@21 | 187 | |
paul@21 | 188 | _ = request.getText |
paul@21 | 189 | |
paul@21 | 190 | page = PageEditor(request, groupname) |
paul@21 | 191 | body = page.get_raw_body() |
paul@21 | 192 | match = None |
paul@21 | 193 | |
paul@21 | 194 | # Find the last matching span. |
paul@21 | 195 | |
paul@21 | 196 | for match in group_member_pattern.finditer(body): |
paul@21 | 197 | start, end = match.span() |
paul@21 | 198 | |
paul@21 | 199 | # Add a group member to the body. |
paul@21 | 200 | |
paul@21 | 201 | entry = ("\n * %s" % username) |
paul@21 | 202 | |
paul@21 | 203 | if match: |
paul@21 | 204 | body = body[:end] + entry + body[end:] |
paul@21 | 205 | else: |
paul@21 | 206 | body += entry |
paul@21 | 207 | |
paul@21 | 208 | page.saveText(body, 0, comment=_("Added %s to the approved editors group.") % username) |
paul@21 | 209 | |
paul@1 | 210 | # vim: tabstop=4 expandtab shiftwidth=4 |