Manual: Heading fixes.
[planner-el.git] / planner-authz.el
blob160dbe02973aab1b7ad926e34a451acd5f809e00
1 ;;; planner-authz.el --- restrict portions of published pages
3 ;; Copyright (C) 2004, 2005, 2006 Andrew J. Korty <ajk@iu.edu>
4 ;; Parts copyright (C) 2004, 2005 Free Software Foundation, Inc.
6 ;; Emacs Lisp Archive Entry
7 ;; Filename: planner-authz.el
8 ;; Keywords: hypermedia
9 ;; Author: Andrew J. Korty <ajk@iu.edu>
10 ;; Maintainer: Andrew J. Korty <ajk@iu.edu>
11 ;; Description: Control access to portions of published planner pages
12 ;; URL:
13 ;; Compatibility: Emacs21
15 ;; This file is part of Planner. It is not part of GNU Emacs.
17 ;; Planner is free software; you can redistribute it and/or modify it
18 ;; under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; any later version.
22 ;; Planner is distributed in the hope that it will be useful, but
23 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 ;; General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with Planner; see the file COPYING. If not, write to the
29 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 ;; Boston, MA 02110-1301, USA.
32 ;;; Commentary:
34 ;; This library lets you publish your planner pages while controlling
35 ;; access to certain portions of them to users you specify. When you
36 ;; load this library, you gain access to two additional markup
37 ;; directives to use in your planner pages. The <authz> tag lets you
38 ;; restrict access to arbitrary content as follows:
40 ;; Here is a sentence everyone should see. This sentence also
41 ;; contains no sensitive data whatsoever. <authz users="ajk">This
42 ;; sentence, however, talks about my predilection for that French
43 ;; vanilla instant coffee that comes in the little tin, and I'm
44 ;; embarrassed for anyone else to know about that.</authz> And
45 ;; here's some more perfectly innocuous content.
47 ;; You can use <authz> tags to mark up entire paragraphs, tasks,
48 ;; notes, and anything else. The tags are replaced with Mason code by
49 ;; default, but you could add support for some other templating system
50 ;; by configuring planner-authz-mason-markup-strings and
51 ;; planner-authz-after-publish-hook.
53 ;; The #authz directive restricts access to an entire page. It will
54 ;; generate a 403 error when someone not listed tries to access it.
55 ;; Any notes or tasks on a #authz-protected page are also wrapped in
56 ;; authorization controls on linked pages.
58 ;; * Diary Markup
60 ;; If your pages have a section with diary entries maintained by
61 ;; planner-appt.el (or by any other means), you can control access to
62 ;; these entries. First, customize `planner-section-tagnames' to map
63 ;; your diary section ("* Schedule", in this example) to a tag called
64 ;; "diary-section", for example:
66 ;; (add-to-list 'planner-section-tagnames '("Schedule" . "diary-section"))
68 ;; If the name of your diary section is "* Diary", you will not need
69 ;; to customize `planner-section-tagnames' by default.
71 ;; Then make sure the diary entries you want restricted contain a
72 ;; corresponding plan page name in parentheses, for example:
74 ;; 10:00 10:30 Meeting with boss (WorkStuff)
76 ;; * Startup
78 ;; Add the following to your .emacs file to cause
79 ;; M-x muse-project-publish to automatically use planner-authz
80 ;; features.
82 ;; (require 'planner-authz)
84 ;; * Customization
86 ;; All user-serviceable options can be customized with
87 ;; M-x customize-group RET planner-authz RET.
89 ;; * Defaults
91 ;; The following customization options let you set default access
92 ;; lists for pages that don't have explicit settings:
94 ;; planner-authz-project-default
96 ;; Default access list for project pages (not day pages). If a
97 ;; given project page doesn't contain a #authz tag, it will receive
98 ;; the access list defined here. If this variable is nil, all users
99 ;; will be allowed to view the page. No corresponding variable is
100 ;; provided for day pages because it doesn't seem like you'd ever
101 ;; want to control access based on what day it was. (But I will
102 ;; accept patches. :) Notes and tasks referencing pages without
103 ;; #authz tags will also be restricted to the users listed here.
105 ;; planner-authz-day-note-default
107 ;; Default access list for notes on day pages not associated with
108 ;; any project. There is way to set a default for notes on project
109 ;; pages for the reason above; they would only be associated with
110 ;; date pages anyway.
112 ;; planner-authz-day-task-default
114 ;; Same as above but for tasks.
116 ;;; Todo
118 ;; - Make more specific tags override less specific ones, rather than
119 ;; more restrictive overriding less restrictive
121 ;;; Code
123 (require 'planner-publish)
125 ;; Customization options
127 (defgroup planner-authz nil
128 "A planner.el extension for restricting portions of your
129 published pages to specified users."
130 :group 'planner
131 :prefix "planner-authz")
133 (defcustom planner-authz-after-publish-hook
134 '(planner-authz-generate-mason-component)
135 "Functions called after all pages have been published."
136 :group 'planner-authz
137 :type 'hook)
139 (defcustom planner-authz-appt-alt nil
140 "If non-nil, show `planner-appt' appointments to users not
141 authorized to see them, but replace the text of the appointment with
142 the contents of this variable. If nil, don't show any part of an
143 appointment to an unauthorized user.
145 For example, if this variable is set to \"Private appointment\" and
146 some hypothetical user is not authorized for the SecretStuff page, an
147 appointment that was entered as
149 #A1 _ @10:00 12:00 Secret meeting (SecretStuff)
151 would appear to our unauthorized user as
153 #A1 _ @10:00 12:00 Private appointment"
154 :group 'planner-authz
155 :type '(choice (string :tag "Replacement text")
156 (const :tag "Disable" nil)))
158 (defcustom planner-authz-appt-regexp
159 (if (require 'planner-appt nil t)
160 (concat "\\(?:[@!][ \t]*\\)?\\(?:" planner-appt-time-regexp
161 "\\|&nbsp;\\)\\(?:[ \t|]+\\(?:" planner-appt-time-regexp
162 "\\|&nbsp;\\)\\)?[ \t|]+"))
163 "Regexp that matches a `planner-appt' start and end time specification."
164 :group 'planner-authz
165 :type 'string)
167 (defcustom planner-authz-day-note-default nil
168 "Default list of users for restricting non-project notes on day pages."
169 :group 'planner-authz
170 :type '(repeat string))
172 (defcustom planner-authz-day-task-default nil
173 "Default list of users for restricting non-project tasks on day pages."
174 :group 'planner-authz
175 :type '(repeat string))
177 (defcustom planner-authz-link-regexp
178 (concat "(\\(" muse-explicit-link-regexp
179 (if (boundp 'muse-wiki-wikiword-regexp)
180 (concat "\\|" muse-wiki-wikiword-regexp))
181 "\\|" muse-implicit-link-regexp "\\))$")
182 "Regexp that matches the plan page link at the end of a line in a
183 task or diary entry."
184 :group 'planner-authz
185 :type '(string))
187 (defcustom planner-authz-mason-component-contents
188 "<%once>
189 sub authz {
190 my $r_user = $r ? $r->connection->user
191 : $ENV{REMOTE_USER} or return 0;
192 foreach (@_) { return 1 if $r_user eq $_ }
193 return 0;
195 </%once>
196 <%method content>
197 <%args>
198 $alt => undef
199 @users
200 </%args>
201 % if (authz @users) {
202 <% $m->content %>\\
203 % } elsif ($alt) {
204 <% $alt %>\\
206 </%method>
207 <%method page>
208 <%args>@users</%args>
209 <%perl>
210 unless (authz @users) {
211 $m->clear_buffer;
212 $m->abort(404);
214 </%perl>
215 </%method>
217 "Mason code to be stored in a component.
218 The component's name is determined from
219 `planner-authz-mason-component-name'."
220 :group 'planner-authz
221 :type 'string)
223 (defcustom planner-authz-mason-component-name "authz.mas"
224 "Name of Mason component that restricts content."
225 :group 'planner-authz
226 :type 'string)
228 (defcustom planner-authz-multi-func 'planner-authz-multi-union
229 "*Function used to combine access lists for multiple planner pages.
231 When `planner-multi' is in effect and a task or note is linked to
232 multiple plan pages, `planner-authz' uses this function to decide how
233 to build the access list for the task or note from the access lists of
234 the linked pages.
236 It is passed a list of sublists, each sublist being the access list (a
237 list of usernames) for one of the linked pages. It should return a
238 combined single list of usernames.
240 Two such functions are provided: `planner-authz-multi-intersection'
241 returns only those user names that are common to all the access lists
242 for all the linked pages, and `planner-authz-multi-union' returns a
243 list of all the unique user names in any of those access lists."
244 :group 'planner-authz
245 :type '(radio (function-item :tag "Intersection"
246 planner-authz-multi-intersection)
247 (function-item :tag "Union" planner-authz-multi-union)
248 (function :tag "Other")))
251 (defcustom planner-authz-project-default nil
252 "Default list of users for restricting project pages if #authz is nil."
253 :group 'planner-authz
254 :type '(repeat string))
256 (defcustom planner-authz-sections-regexp "^\\([*]\\)+\\s-+\\(.+\\)"
257 "Regexp that matches headings for sections authorization markup."
258 :group 'planner-authz
259 :type '(string))
261 (defcustom planner-authz-sections-rule-list nil
262 "List of sections and their access rule.
264 Each rule is a sublist of the form:
266 (SECTION-NAME PREDICTION USER-LIST)
268 For sections matching SECTION-NAME, if the PREDICTION is t or a
269 function return t, that section will be accessable for users in
270 USER-LIST only.
272 The following example will make the \"Timeclock\" section and
273 \"Accomplishments\" section on day pages only accessable by user1 and
274 user2, while on plan pages obey the \"parent\" rule.
276 ((\"Timeclock\" planner-authz-day-p
277 (\"user1\" \"user2\"))
278 (\"Accomplishments\" planner-authz-day-p
279 (\"user1\" \"user2\")))"
280 :group 'planner-authz
281 :type '(repeat (regexp (choice boolean function))
282 (repeat string)))
284 (defcustom planner-authz-markup-regexps
285 '((2300 "\\(<li>\\)\\(<&| [^<]*>\\)\\(.*\\)\\(</&>\\)\\(</li>\\)" 3
286 planner-authz-fix-list-item))
287 "List of markup rules for publishing PLANNER with `planner-authz' restrictions.
288 For more on the structure of this list, see `muse-publish-markup-regexps'."
289 :group 'planner-authz
290 :type '(repeat (choice
291 (list :tag "Markup rule"
292 integer
293 (choice regexp symbol)
294 integer
295 (choice string function symbol))
296 function)))
298 (defcustom planner-authz-markup-functions
299 '((table . planner-authz-mason-markup-table))
300 "An alist of style types to custom functions for that kind of text."
301 :group 'planner-authz
302 :type '(alist :key-type symbol :value-type function))
304 (defcustom planner-authz-markup-tags
305 '(("authz" t t planner-authz-tag)
306 ("diary-section" t t planner-authz-diary-section-tag)
307 ("note" t t planner-authz-note-tag)
308 ("task" t t planner-authz-task-tag))
309 "A list of tag specifications for authorization markup."
310 :group 'planner-authz
311 :type '(repeat (list (string :tag "Markup tag")
312 (boolean :tag "Expect closing tag" :value t)
313 (boolean :tag "Parse attributes" :value nil)
314 function)))
316 (defcustom planner-authz-mason-markup-strings
317 '((planner-authz-begin . "<&| authz.mas:content, 'users', [qw(%s)] &>")
318 (planner-authz-begin-alt
319 . "<&| authz.mas:content, 'users', [qw(%s)], 'alt', '%s' &>")
320 (planner-authz-end . "</&>")
321 (planner-authz-page . "<& authz.mas:page, 'users', [qw(%s)] &>"))
322 "Strings used for additing authorization controls.
324 If a markup rule is not found here, `planner-html-markup-strings' is
325 searched."
326 :type '(alist :key-type symbol :value-type string)
327 :group 'planner-authz)
329 ;; Non-customizable variables
331 (defvar planner-authz-pages nil
332 "Alist of planner pages and users authorized to view them.
333 The list of users is separated by spaces. This variable is
334 internal to planner-authz; do not set it manually.")
335 (defvar planner-authz-publishing-alist nil
336 "Alist used by `planner-authz' to track published pages and their dependencies.
337 This alist stores pages that have been published during the current
338 publishing process, as (PAGENAME . t), and pages whose tasks and notes
339 depend on those pages for access control, as (PAGENAME . nil). At the
340 end of publishing, `planner-authz' uses this alist to determine which
341 dependencies need to be republished, even if they themselves haven't
342 changed.")
343 (defvar planner-authz-disable-dependency-publishing nil
344 "If non-nil, `planner-authz' will not republish unchanged pages whose tasks or notes depend on the page currently being published.
345 Normally, linked pages are republished in case the access list for the
346 current page has changed. This variable is set to t while
347 `planner-authz' is republishing dependent pages to avoid indefinite
348 recursion.")
350 ;;; Functions
352 (defun planner-authz-after-markup ()
353 "Remove the page currently being marked up from the queue of pages
354 to republish and enforce default access controls for project pages."
355 (let ((page (planner-page-name)))
356 (when page
358 (let ((cell (assoc page planner-authz-publishing-alist)))
359 (if cell
361 ;; if already t, the list is stale; whack it
362 (if (cdr cell)
363 (setq planner-authz-publishing-alist '(page . t)))
365 (push '(page . t) planner-authz-publishing-alist)))
367 (let ((users (planner-authz-users)))
368 (when users
369 (goto-char (point-min))
370 (planner-insert-markup (muse-markup-text 'planner-authz-page users))
371 (insert "\n"))))))
373 (defun planner-authz-after-project-publish (project)
374 "Republish pages that reference restricted pages and call the
375 generate Mason code."
376 (when (string= planner-project (car project))
377 (while planner-authz-publishing-alist
378 (if (not (cdar planner-authz-publishing-alist))
379 (let ((planner-authz-disable-dependency-publishing t))
380 (muse-project-publish-file (caar planner-authz-publishing-alist)
381 planner-project t)))
382 (setq planner-authz-publishing-alist
383 (cdr planner-authz-publishing-alist)))
384 (run-hook-with-args 'planner-authz-after-publish-hook project)))
386 (defun planner-authz-before-markup ()
387 "Process #authz directives when publishing only a single page. Mark
388 planner page sections according to
389 `planner-authz-sections-rule-list'."
390 (planner-authz-markup-all-sections))
392 (defun planner-authz-day-p (&optional page)
393 "Return non-nil if the current page or PAGE is a day page."
394 (save-match-data
395 (string-match planner-date-regexp (or page (planner-page-name)))))
397 (defun planner-authz-default (page)
398 "Return the default space-separated string of users that would apply
399 to PAGE. Nil is always returned for day pages."
400 (and planner-authz-project-default
401 (not (planner-authz-day-p page)) ; not on day pages
402 (mapconcat 'identity planner-authz-project-default " ")))
404 (defun planner-authz-file-alist (users)
405 "Generate a list of planner files that USERS have access to."
406 (let ((pages (planner-file-alist))
407 result)
408 (while pages
409 (let (not-found-p)
410 (with-temp-buffer
411 (insert-file-contents-literally (cdar pages))
412 (when (re-search-forward "^#authz\\s-+\\(.+\\)\n+" nil t)
413 (let ((users-iter users)
414 (authz (split-string (match-string 1))))
415 (while (and users-iter (not not-found-p))
416 (unless (member (car users-iter) authz)
417 (setq not-found-p t))
418 (setq users-iter (cdr users-iter)))))
419 (unless not-found-p
420 (setq result (append (list (car pages)) result))))
421 (setq pages (cdr pages))))
422 result))
424 (defun planner-authz-fix-list-item ()
425 "Rearrange list items restricted by `planner-authz' to avoid empty list items on the published page."
426 (replace-match "\\2\\1\\3\\5\\4")
427 (muse-publish-mark-read-only (match-beginning 0) (match-end 2))
428 (muse-publish-mark-read-only (match-beginning 4) (match-end 0)))
430 (defun planner-authz-generate-mason-component (project)
431 "Generate the Mason component restricting content.
432 The component's name is taken from
433 `planner-authz-mason-component-name' and initialized with the
434 contents of `planner-authz-mason-component-contents'. The
435 component restricts access to users specified by <authz> and
436 #authz tags."
437 (with-temp-buffer
438 (insert planner-authz-mason-component-contents)
439 (let ((backup-inhibited t)
440 (styles (cddr project)))
441 (while styles
442 (let ((path (muse-style-element :path (car styles))))
443 (and path
444 (string-match "mason" (muse-style-element :base (car styles)))
445 (write-file
446 (concat (file-name-directory path)
447 planner-authz-mason-component-name))))
448 (setq styles (cdr styles))))))
450 (defun planner-authz-markup-section-predict (rule)
451 "Check if the prediction is satisfied."
452 (let ((predict (elt rule 1)))
453 (if (functionp predict)
454 (funcall predict)
455 predict)))
457 (defun planner-authz-markup-section ()
458 "Restrict section according to `planner-authz-sections-rule-list'."
459 (let ((begin (planner-line-beginning-position))
460 (rule-list planner-authz-sections-rule-list)
461 section-name
462 section-level
463 next-section-regexp)
464 (goto-char begin)
465 (save-match-data
466 (re-search-forward planner-authz-sections-regexp nil t)
467 (setq section-level (length (match-string 1)))
468 (setq section-name (match-string 2)))
469 (let ((rule (catch 'done
470 (while rule-list
471 (if (string-match (caar rule-list) section-name)
472 (throw 'done (car rule-list))
473 (setq rule-list (cdr rule-list))))
474 nil)))
475 (if (and rule
476 (planner-authz-markup-section-predict rule))
477 (progn
478 (goto-char begin)
479 (muse-publish-surround-text
480 (format "<authz users=\"%s\">\n"
481 (mapconcat 'identity (elt rule 2) " "))
482 "\n</authz>\n"
483 (lambda ()
484 (save-match-data
485 (let ((found nil))
486 (re-search-forward planner-authz-sections-regexp nil t)
487 (while (and (not found)
488 (re-search-forward planner-authz-sections-regexp
489 nil t))
490 (if (<= (length (match-string 1))
491 section-level)
492 (setq found t)))
493 (if found
494 (goto-char (planner-line-beginning-position))
495 (goto-char (point-max))))))))))))
497 (defun planner-authz-markup-all-sections ()
498 "Run `planner-authz-markup-section' on the entire buffer."
499 (goto-char (point-min))
500 (while (re-search-forward planner-authz-sections-regexp nil t)
501 (planner-authz-markup-section)))
503 (defun planner-authz-mason-markup-table ()
504 "Protect \"<&|\" Mason constructs from Muse table markup."
505 (let* ((beg (planner-line-beginning-position))
506 (style (muse-style-element :base (muse-style)))
507 (base (if style
508 (muse-style-element :base style)))
509 (func (if base
510 (muse-find-markup-element
511 :functions 'table (muse-style-element :base base)))))
512 (when (functionp func)
513 (save-excursion
514 (save-match-data
515 (goto-char beg)
516 (while (search-forward "<&|" (line-end-position) t)
517 (replace-match "<&:" t t))))
518 (funcall func)
519 (let ((end (point)))
520 (goto-char beg)
521 (while (search-forward "<&:" end t)
522 (replace-match "<&|" t t))))))
525 (defun planner-authz-index-as-string (&optional as-list exclude-private)
526 "Generate an index of all Muse pages with authorization controls.
527 In the published index, only those links to pages which the remote
528 user is authorized to access will be shown.
529 If AS-LIST is non-nil, insert a dash and spaces before each item.
530 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
531 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
532 (with-temp-buffer
533 (insert (planner-index-as-string as-list exclude-private))
534 (when muse-publishing-p
535 (goto-char (point-min))
536 (while (and (re-search-forward
537 (if as-list
538 (concat "^[" muse-regexp-blank "]+-["
539 muse-regexp-blank "]*")
540 (concat "^[" muse-regexp-blank "]*"))
541 nil t)
542 (looking-at muse-explicit-link-regexp))
543 (let* ((link (buffer-substring (point) (line-end-position)))
544 (page (planner-link-base link))
545 (users (if page (planner-authz-users page))))
546 (if users
547 (progn
548 (insert (format "<authz users=\"%s\">" users))
549 (end-of-line)
550 (insert "</authz>"))
551 (end-of-line)))))
552 (buffer-substring (point-min) (point-max))))
554 (defun planner-authz-republish-dependencies-maybe (linked-pages)
555 "Remember LINKED-PAGES to be republished later.
556 The pages will be republished if and only if the current page is
557 restricted."
558 (and (not planner-authz-disable-dependency-publishing)
559 (planner-authz-users)
560 (while linked-pages
561 (unless (assoc (car linked-pages) planner-authz-publishing-alist)
562 (push '(car linked-pages) planner-authz-publishing-alist))
563 (setq linked-pages (cdr linked-pages)))))
565 (defun planner-authz-tag (beg end attrs)
566 "Publish <authz> tags. The region from BEG to END is protected.
567 ATTRS should be an alist of tag attributes including \"users\" and
568 optionally \"alt\" for alternative text to be displayed to
569 unauthorized users."
570 (save-excursion
571 (let ((alt (or (cdr (assoc "alt" attrs)) ""))
572 (users (or (cdr (assoc "users" attrs)) "")))
573 (goto-char beg)
574 (planner-insert-markup
575 (if (zerop (length alt))
576 (muse-markup-text 'planner-authz-begin users)
577 (muse-markup-text 'planner-authz-begin-alt users alt)))
578 (goto-char end)
579 (planner-insert-markup (muse-markup-text 'planner-authz-end)))))
581 (defun planner-authz-diary-section-tag (beg end attrs)
582 "Restrict entries in a diary section."
583 (save-excursion
584 (save-restriction
585 (narrow-to-region beg end)
586 (planner-publish-nested-section-tag beg end)
587 (goto-char beg)
588 (while (and (zerop (forward-line))
589 (= (point) (planner-line-beginning-position)))
590 (unless (looking-at "^\\(?:[ \t]*\\|No entries\\|</div>\\)$")
591 (let ((line-begin (point))
592 (line-end (line-end-position)))
593 (re-search-forward planner-authz-link-regexp line-end t)
594 (let* ((link (match-string 1))
595 (linked-pages (if link
596 (mapcar 'planner-link-base
597 (if (featurep 'planner-multi)
598 (planner-multi-split link)
599 link))))
600 (linked-users
601 (if linked-pages
602 (planner-authz-multi-users linked-pages)
603 (and planner-authz-day-task-default
604 (mapconcat 'identity planner-authz-day-task-default
605 " ")))))
606 (when linked-users
607 (if (and planner-authz-appt-alt planner-authz-appt-regexp
608 (progn
609 (goto-char line-begin)
610 (re-search-forward
611 planner-authz-appt-regexp line-end t)))
612 (progn
613 (search-forward " - " (+ 2 (point)) t)
614 (planner-insert-markup
615 (muse-markup-text 'planner-authz-begin-alt linked-users
616 planner-authz-appt-alt)))
617 (planner-insert-markup
618 (muse-markup-text 'planner-authz-begin linked-users)))
619 (end-of-line)
620 (planner-insert-markup
621 (muse-markup-text 'planner-authz-end))))))))))
623 (defun planner-authz-note-tag (beg end attrs)
624 "Restrict notes linked to a restricted page. If this page is
625 restricted and the note is linked to another page, remember to
626 republish that page later and restrict the note as it appears there.
627 Call `planner-publish-note-tag' as a side effect."
628 (save-excursion
629 (save-restriction
630 (narrow-to-region beg end)
631 (planner-publish-note-tag beg end attrs)
632 (let* ((categories (cdr (assoc "categories" attrs)))
633 (links (if (or (not categories) (zerop (length categories)))
634 (cdr (assoc "link" attrs))
635 categories))
636 (linked-pages (if (and links (not (zerop (length links))))
637 (mapcar 'planner-link-base
638 (if (featurep 'planner-multi)
639 (planner-multi-split links)
640 links))))
641 (linked-users
642 (if linked-pages
643 (planner-authz-multi-users linked-pages)
644 (and planner-authz-day-note-default
645 (planner-authz-day-p)
646 (mapconcat 'identity
647 planner-authz-day-note-default " ")))))
649 ;; If this note is linked to another page, republish that page
650 ;; later to restrict the note as it appears there, providing that
651 ;; page has an authz restriction
653 (if linked-pages
654 (planner-authz-republish-dependencies-maybe linked-pages))
656 ;; If the linked page has an authz restriction, restrict this note
658 (when linked-users
659 (goto-char (point-min))
660 (planner-insert-markup
661 (muse-markup-text 'planner-authz-begin linked-users))
662 (insert "\n")
663 (goto-char (point-max))
664 (planner-insert-markup (muse-markup-text 'planner-authz-end))
665 (insert "\n"))))))
667 (defun planner-authz-task-tag (beg end attrs)
668 "Restrict tasks linked to restricted pages. If this page is
669 restricted and the task is linked to another page, remember to
670 republish that page later and restrict the task as it appears there.
671 Call `planner-publish-task-tag' as a side effect."
672 (save-excursion
673 (save-restriction
674 (narrow-to-region beg end)
675 (planner-publish-task-tag beg end attrs)
676 (let* ((link (cdr (assoc "link" attrs)))
677 (linked-pages (if link
678 (mapcar 'planner-link-base
679 (if (featurep 'planner-multi)
680 (planner-multi-split link)
681 link))))
682 (linked-users
683 (if linked-pages
684 (planner-authz-multi-users linked-pages)
685 (and planner-authz-day-task-default
686 (planner-authz-day-p)
687 (mapconcat 'identity
688 planner-authz-day-task-default " ")))))
690 ;; If this task is linked to another page, republish that page
691 ;; later to restrict the task as it appears there, providing that
692 ;; page has an authz restriction
694 (if linked-pages
695 (planner-authz-republish-dependencies-maybe linked-pages))
697 ;; If the linked page has an authz restriction, restrict this task
699 (when linked-users
700 (goto-char (point-min))
701 (planner-insert-markup
702 (muse-markup-text 'planner-authz-begin linked-users))
703 (goto-char (point-max))
704 (planner-insert-markup (muse-markup-text 'planner-authz-end)))))))
706 (defun planner-authz-users (&optional page)
707 "Return a list of acceptable users for PAGE.
708 The list of users is returned as space-separated string, based on
709 a #authz directive appearing in the page. If PAGE contains no
710 #authz directive and is a project page (it doesn't match
711 `planner-date-regexp'), return `planner-authz-project-default' as
712 a space-separated string.
714 If PAGE is nil, return a list of users associated with the
715 current page."
716 (unless page (setq page (planner-page-name)))
717 (let ((match (cdr (assoc page planner-authz-pages))))
718 (unless match
719 (let ((file (cdr (assoc page (planner-file-alist)))))
720 (setq match
721 (or (and file
722 (with-temp-buffer
723 (insert-file-contents-literally file)
724 (if (re-search-forward "^#authz\\s-+\\(.+\\)\n+"
725 nil t)
726 (match-string 1))))
727 (planner-authz-default page))))
728 (push `(,page . ,match) planner-authz-pages))
729 match))
731 (defun planner-authz-multi-intersection (list)
732 "Merge a list of `planner-authz' access lists, returning a list of only those user names that are common to all the passed access lists."
733 (let ((count (length list))
734 alist intersection sublist)
736 ;; in alist, associate each name with its frequency of appearance
737 (while list
738 (setq sublist (car list))
739 (while sublist
740 (let ((entry (assoc (car sublist) alist)))
741 (if entry
742 (setcdr entry (1+ (cdr entry)))
743 (setq alist (cons `(,(car sublist) . 1) alist))))
744 (setq sublist (cdr sublist)))
745 (setq list (cdr list)))
747 ;; those names with `count' frequencies were in every sublist
748 (while alist
749 (if (= (cdar alist) count)
750 (setq intersection (cons (caar alist) intersection)))
751 (setq alist (cdr alist)))
752 intersection))
754 (defun planner-authz-multi-union (list)
755 "Merge a list of `planner-authz' access lists, returning a list of all the unique user names in any of those access lists."
756 (let (sublist union)
757 (while list
758 (setq sublist (car list))
759 (while sublist
760 (add-to-list 'union (car sublist))
761 (setq sublist (cdr sublist)))
762 (setq list (cdr list)))
763 union))
765 (defun planner-authz-multi-users (pages)
766 "Return a merged access list for PAGES.
767 The list of users is returned as space-separated string, based on a
768 #authz directive appearing in the PAGES. If one of PAGES contains no
769 #authz directive and is a project page (it doesn't match
770 `planner-date-regexp'), it will contribute
771 `planner-authz-project-default' to the merge."
772 (let ((users
773 (funcall planner-authz-multi-func
774 (mapcar (lambda (page)
775 (if (not (planner-authz-day-p page))
776 (let ((users (planner-authz-users page)))
777 (if users
778 (split-string users)))))
779 pages))))
780 (if users
781 (mapconcat 'identity users " "))))
783 (add-hook 'muse-after-project-publish-hook
784 'planner-authz-after-project-publish)
786 (let ((styles (list "html" "xhtml")))
787 (while styles
788 (let ((style (concat "planner-authz-mason-" (car styles))))
789 (unless (assoc style muse-publishing-styles)
790 (muse-derive-style
791 style (concat "planner-" (car styles))
792 :before 'planner-authz-before-markup
793 :after 'planner-authz-after-markup
794 :functions 'planner-authz-markup-functions
795 :regexps 'planner-authz-markup-regexps
796 :strings 'planner-authz-mason-markup-strings
797 :tags (append planner-authz-markup-tags
798 planner-publish-markup-tags))))
799 (setq styles (cdr styles))))
801 (provide 'planner-authz)
803 ;;; planner-authz.el ends here