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