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