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@1 | 15 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@1 | 16 | """ |
paul@1 | 17 | |
paul@4 | 18 | from MoinMoin import user |
paul@7 | 19 | from MoinMoin.wikiutil import escape |
paul@1 | 20 | import re |
paul@4 | 21 | |
paul@4 | 22 | acl_pattern = re.compile(ur"^#acl .*$", re.UNICODE | re.MULTILINE) |
paul@1 | 23 | |
paul@1 | 24 | __version__ = "0.1" |
paul@1 | 25 | |
paul@3 | 26 | def get_queued_changes_page(request): |
paul@3 | 27 | return getattr(request.cfg, "queued_changes_page", "ApprovalQueue") |
paul@1 | 28 | |
paul@1 | 29 | def get_approved_editors_group(request): |
paul@1 | 30 | return getattr(request.cfg, "approved_editors_group", "ApprovedGroup") |
paul@1 | 31 | |
paul@4 | 32 | def get_page_reviewers_group(request): |
paul@4 | 33 | return getattr(request.cfg, "reviewers_group", "PageReviewersGroup") |
paul@4 | 34 | |
paul@4 | 35 | def get_queued_changes_user(request): |
paul@4 | 36 | return getattr(request.cfg, "queued_changes_user", "ApprovalQueueUser") |
paul@4 | 37 | |
paul@4 | 38 | def get_secret_key(request): |
paul@4 | 39 | return request.cfg.secrets["wikiutil/tickets"] |
paul@4 | 40 | |
paul@4 | 41 | def is_reviewer(request): |
paul@4 | 42 | return request.user.valid and ( |
paul@4 | 43 | request.dicts.has_member(get_approved_editors_group(request), request.user.name) or \ |
paul@4 | 44 | request.user.isSuperUser()) |
paul@4 | 45 | |
paul@4 | 46 | def is_approved(request): |
paul@4 | 47 | return request.user.valid and ( |
paul@4 | 48 | request.dicts.has_member(get_approved_editors_group(request), request.user.name) or \ |
paul@4 | 49 | request.user.isSuperUser()) |
paul@4 | 50 | |
paul@6 | 51 | def is_queued_changes_user(request): |
paul@6 | 52 | return request.user.valid and request.user.name == get_queued_changes_user(request) |
paul@6 | 53 | |
paul@3 | 54 | def is_queued_changes_page(request, pagename): |
paul@1 | 55 | |
paul@3 | 56 | "Return whether 'pagename' is a queued changes page by testing its name." |
paul@1 | 57 | |
paul@1 | 58 | parts = pagename.split("/") |
paul@3 | 59 | return len(parts) > 1 and parts[-1] == get_queued_changes_page(request) |
paul@1 | 60 | |
paul@1 | 61 | def get_target_page_name(pagename): |
paul@1 | 62 | |
paul@3 | 63 | "Return the target page name for the given queued changes 'pagename'." |
paul@1 | 64 | |
paul@3 | 65 | return "/".join(pagename.split("/")[:-1]) |
paul@1 | 66 | |
paul@4 | 67 | def get_user_for_saving(request): |
paul@4 | 68 | |
paul@4 | 69 | "Return a user that can save pages with ACLs." |
paul@4 | 70 | |
paul@4 | 71 | username = get_queued_changes_user(request) |
paul@4 | 72 | uid = user.getUserId(request, username) |
paul@4 | 73 | |
paul@4 | 74 | # If the user does not exist, just return the existing user. |
paul@4 | 75 | |
paul@4 | 76 | if not uid: |
paul@4 | 77 | return request.user |
paul@4 | 78 | |
paul@4 | 79 | # Otherwise, return the privileged user. |
paul@4 | 80 | |
paul@4 | 81 | return user.User(request, uid) |
paul@4 | 82 | |
paul@4 | 83 | def add_access_control(request, body): |
paul@4 | 84 | |
paul@4 | 85 | """ |
paul@4 | 86 | Using the 'request', add an ACL to the page 'body' in order to prevent |
paul@4 | 87 | anyone other than reviewers from seeing it in the queue. |
paul@4 | 88 | """ |
paul@4 | 89 | |
paul@4 | 90 | # Find existing ACLs. |
paul@4 | 91 | |
paul@4 | 92 | match = acl_pattern.search(body) |
paul@4 | 93 | if match: |
paul@4 | 94 | start, end = match.span() |
paul@4 | 95 | |
paul@4 | 96 | # Comment out existing ACLs. |
paul@4 | 97 | |
paul@4 | 98 | parts = [] |
paul@4 | 99 | parts.append(body[:start]) |
paul@4 | 100 | parts.append("#") |
paul@4 | 101 | parts.append(body[start:]) |
paul@4 | 102 | else: |
paul@4 | 103 | parts = [body] |
paul@4 | 104 | |
paul@4 | 105 | # Add the ACL. |
paul@4 | 106 | |
paul@6 | 107 | parts.insert(0, "#acl %s:read,write,delete,revert,admin All:\n" % |
paul@6 | 108 | get_page_reviewers_group(request)) |
paul@4 | 109 | return "".join(parts) |
paul@4 | 110 | |
paul@4 | 111 | def remove_access_control(request, body): |
paul@4 | 112 | |
paul@4 | 113 | "Using the 'request', remove any added ACL to the page 'body'." |
paul@4 | 114 | |
paul@4 | 115 | lines = body.split("\n") |
paul@4 | 116 | |
paul@4 | 117 | try: |
paul@4 | 118 | directive = lines[0].split()[0] |
paul@4 | 119 | if directive == "#acl": |
paul@4 | 120 | return "\n".join(lines[1:]) |
paul@4 | 121 | except ValueError: |
paul@4 | 122 | pass |
paul@4 | 123 | |
paul@4 | 124 | return body |
paul@4 | 125 | |
paul@1 | 126 | # Utility classes and associated functions. |
paul@1 | 127 | # NOTE: These are a subset of EventAggregatorSupport. |
paul@1 | 128 | |
paul@1 | 129 | class Form: |
paul@1 | 130 | |
paul@1 | 131 | """ |
paul@1 | 132 | A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x |
paul@1 | 133 | environment. |
paul@1 | 134 | """ |
paul@1 | 135 | |
paul@1 | 136 | def __init__(self, form): |
paul@1 | 137 | self.form = form |
paul@1 | 138 | |
paul@1 | 139 | def get(self, name, default=None): |
paul@1 | 140 | values = self.form.getlist(name) |
paul@1 | 141 | if not values: |
paul@1 | 142 | return default |
paul@1 | 143 | else: |
paul@1 | 144 | return values |
paul@1 | 145 | |
paul@1 | 146 | def __getitem__(self, name): |
paul@1 | 147 | return self.form.getlist(name) |
paul@1 | 148 | |
paul@1 | 149 | class ActionSupport: |
paul@1 | 150 | |
paul@1 | 151 | """ |
paul@1 | 152 | Work around disruptive MoinMoin changes in 1.9, and also provide useful |
paul@1 | 153 | convenience methods. |
paul@1 | 154 | """ |
paul@1 | 155 | |
paul@1 | 156 | def get_form(self): |
paul@1 | 157 | return get_form(self.request) |
paul@1 | 158 | |
paul@1 | 159 | def get_form(request): |
paul@1 | 160 | |
paul@1 | 161 | "Work around disruptive MoinMoin changes in 1.9." |
paul@1 | 162 | |
paul@1 | 163 | if hasattr(request, "values"): |
paul@1 | 164 | return Form(request.values) |
paul@1 | 165 | else: |
paul@1 | 166 | return request.form |
paul@1 | 167 | |
paul@7 | 168 | def escattr(s): |
paul@7 | 169 | return escape(s, 1) |
paul@7 | 170 | |
paul@1 | 171 | # vim: tabstop=4 expandtab shiftwidth=4 |