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