1 ;;; planner-authz.el --- restrict portions of published pages
3 ;; Copyright (C) 2004, 2005 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
13 ;; Compatibility: Emacs21
15 ;; This file is not part of GNU Emacs.
17 ;; This is free software; you can redistribute it and/or modify it under
18 ;; the terms of the GNU General Public License as published by the Free
19 ;; Software Foundation; either version 2, or (at your option) any later
22 ;; This is distributed in the hope that it will be useful, but WITHOUT
23 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; 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.
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.
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 sectionalize-markup-tagname to map
63 ;; your diary section to a tag called "diary", for example:
65 ;; (add-to-list 'sectionalize-markup-tagname '("* Schedule" . "diary"))
67 ;; Then make sure the diary entries you want restricted contain a
68 ;; corresponding plan page name in parentheses, for example:
70 ;; 10:00 10:30 Meeting with boss (WorkStuff)
74 ;; Add the following to your .emacs file to cause
75 ;; M-x muse-project-publish to automatically use planner-authz
78 ;; (require 'planner-authz)
82 ;; All user-serviceable options can be customized with
83 ;; M-x customize-group RET planner-authz RET.
87 ;; The following customization options let you set default access
88 ;; lists for pages that don't have explicit settings:
90 ;; planner-authz-project-default
92 ;; Default access list for project pages (not day pages). If a
93 ;; given project page doesn't contain a #authz tag, it will receive
94 ;; the access list defined here. If this variable is nil, all users
95 ;; will be allowed to view the page. No corresponding variable is
96 ;; provided for day pages because it doesn't seem like you'd ever
97 ;; want to control access based on what day it was. (But I will
98 ;; accept patches. :) Notes and tasks referencing pages without
99 ;; #authz tags will also be restricted to the users listed here.
101 ;; planner-authz-day-note-default
103 ;; Default access list for notes on day pages not associated with
104 ;; any project. There is way to set a default for notes on project
105 ;; pages for the reason above; they would only be associated with
106 ;; date pages anyway.
108 ;; planner-authz-day-task-default
110 ;; Same as above but for tasks.
114 ;; - Make more specific tags override less specific ones, rather than
115 ;; more restrictive overriding less restrictive
119 (require 'planner-publish
)
121 ;; Customization options
123 (defgroup planner-authz nil
124 "A planner.el extension for restricting portions of your
125 published pages to specified users."
127 :prefix
"planner-authz")
129 (defcustom planner-authz-after-publish-hook
130 '(planner-authz-generate-mason-component)
131 "Functions called after all pages have been published."
132 :group
'planner-authz
135 (defcustom planner-authz-appt-alt nil
136 "If non-nil, show `planner-appt' appointments to users not
137 authorized to see them, but replace the text of the appointment with
138 the contents of this variable. If nil, don't show any part of an
139 appointment to an unauthorized user.
141 For example, if this variable is set to \"Private appointment\" and
142 some hypothetical user is not authorized for the SecretStuff page, an
143 appointment that was entered as
145 #A1 _ @10:00 12:00 Secret meeting (SecretStuff)
147 would appear to our unauthorized user as
149 #A1 _ @10:00 12:00 Private appointment"
150 :group
'planner-authz
151 :type
'(choice (string :tag
"Replacement text")
152 (const :tag
"Disable" nil
)))
154 (defcustom planner-authz-appt-regexp
155 (if (require 'planner-appt nil t
)
156 (concat "\\(?:[@!][ \t]*\\)?\\(?:" planner-appt-time-regexp
157 "\\| \\)\\(?:[ \t|]+\\(?:" planner-appt-time-regexp
158 "\\| \\)\\)?[ \t|]+"))
159 "Regexp that matches a `planner-appt' start and end time specification."
160 :group
'planner-authz
163 (defcustom planner-authz-day-note-default nil
164 "Default list of users for restricting non-project notes on day pages."
165 :group
'planner-authz
166 :type
'(repeat string
))
168 (defcustom planner-authz-day-task-default nil
169 "Default list of users for restricting non-project tasks on day pages."
170 :group
'planner-authz
171 :type
'(repeat string
))
173 (defcustom planner-authz-link-regexp
174 (concat "(\\(" muse-explicit-link-regexp
175 (if (boundp 'muse-wiki-wikiword-regexp
)
176 (concat "\\|" muse-wiki-wikiword-regexp
))
177 "\\|" muse-implicit-link-regexp
"\\))$")
178 "Regexp that matches the plan page link at the end of a line in a
179 task or diary entry."
180 :group
'planner-authz
183 (defcustom planner-authz-mason-component-contents
186 my $r_user = $r ? $r->connection->user
187 : $ENV{REMOTE_USER} or return 0;
188 foreach (@_) { return 1 if $r_user eq $_ }
197 % if (authz @users) {
204 <%args>@users</%args>
206 unless (authz @users) {
213 "Mason code to be stored in a component.
214 The component's name is determined from
215 `planner-authz-mason-component-name'."
216 :group
'planner-authz
219 (defcustom planner-authz-mason-component-name
"authz.mas"
220 "Name of Mason component that restricts content."
221 :group
'planner-authz
224 (defcustom planner-authz-project-default nil
225 "Default list of users for restricting project pages if #authz is nil."
226 :group
'planner-authz
227 :type
'(repeat string
))
229 (defcustom planner-authz-sections-regexp
"^\\([*]\\)+\\s-+\\(.+\\)"
230 "Regexp that matches headings for sections authorization markup."
231 :group
'planner-authz
234 (defcustom planner-authz-sections-rule-list nil
235 "List of sections and their access rule.
237 Each rule is a sublist of the form:
239 (SECTION-NAME PREDICTION USER-LIST)
241 For sections matching SECTION-NAME, if the PREDICTION is t or a
242 function return t, that section will be accessable for users in
245 The following example will make the \"Timeclock\" section and
246 \"Accomplishments\" section on day pages only accessable by user1 and
247 user2, while on plan pages obey the \"parent\" rule.
249 ((\"Timeclock\" planner-authz-day-p
250 (\"user1\" \"user2\"))
251 (\"Accomplishments\" planner-authz-day-p
252 (\"user1\" \"user2\")))"
253 :group
'planner-authz
254 :type
'(repeat (regexp (choice boolean function
))
257 (defcustom planner-authz-markup-functions
258 '((table . planner-authz-mason-markup-table
))
259 "An alist of style types to custom functions for that kind of text."
260 :group
'planner-authz
261 :type
'(alist :key-type symbol
:value-type function
))
263 (defcustom planner-authz-markup-tags
264 '(("authz" t t planner-authz-tag
)
265 ("diary" t t planner-authz-diary-tag
)
266 ("note" t t planner-authz-note-tag
)
267 ("task" t t planner-authz-task-tag
))
268 "A list of tag specifications for authorization markup."
269 :group
'planner-authz
270 :type
'(repeat (list (string :tag
"Markup tag")
271 (boolean :tag
"Expect closing tag" :value t
)
272 (boolean :tag
"Parse attributes" :value nil
)
275 (defcustom planner-authz-mason-markup-strings
276 '((planner-authz-begin .
"<&| authz.mas:content, 'users', [qw(%s)] &>")
277 (planner-authz-begin-alt
278 .
"<&| authz.mas:content, 'users', [qw(%s)], 'alt', '%s' &>")
279 (planner-authz-end .
"</&>")
280 (planner-authz-page .
"<& authz.mas:page, 'users', [qw(%s)] &>"))
281 "Strings used for additing authorization controls.
283 If a markup rule is not found here, `planner-html-markup-strings' is
285 :type
'(alist :key-type symbol
:value-type string
)
286 :group
'planner-authz
)
288 ;; Non-customizable variables
290 (defvar planner-authz-pages nil
291 "Alist of planner pages and users authorized to view them.
292 The list of users is separated by spaces. This variable is
293 internal to planner-authz; do not set it manually.")
294 (defvar planner-authz-pages-to-republish nil
295 "Queue of planner pages to republish when finished with current round.
296 Used to markup planner day pages that wouldn't ordinarily get
297 republished because they haven't explicitly changed. This
298 variable is internal to planner-authz; do not set it manually.")
302 (defun planner-authz-after-markup ()
303 "Remove the page currently being marked up from the queue of pages
304 to republish and enforce default access controls for project pages."
305 (let ((page (planner-page-name)))
307 (delete page planner-authz-pages-to-republish
)
308 (let ((users (planner-authz-users)))
310 (goto-char (point-min))
311 (planner-insert-markup (muse-markup-text 'planner-authz-page users
))
314 (defun planner-authz-after-project-publish (project)
315 "Republish pages that reference restricted pages and call the
316 generate Mason code."
317 (when (string= planner-project
(car project
))
319 (while (setq file
(pop planner-authz-pages-to-republish
))
320 (muse-project-publish-file file planner-project t
)))
321 (run-hook-with-args 'planner-authz-after-publish-hook project
)))
323 (defun planner-authz-before-markup ()
324 "Process #authz directives when publishing only a single page. Mark
325 planner page sections according to
326 `planner-authz-sections-rule-list'."
327 (planner-authz-markup-all-sections))
329 (defun planner-authz-day-p (&optional page
)
330 "Return non-nil if the current page or PAGE is a day page."
332 (string-match planner-date-regexp
(or page
(planner-page-name)))))
334 (defun planner-authz-default (page)
335 "Return the default space-separated string of users that would apply
336 to PAGE. Nil is always returned for day pages."
337 (and planner-authz-project-default
338 (not (planner-authz-day-p page
)) ; not on day pages
339 (mapconcat 'identity planner-authz-project-default
" ")))
341 (defun planner-authz-file-alist (users)
342 "Generate a list of planner files that USERS have access to."
343 (let ((pages (planner-file-alist))
348 (insert-file-contents-literally (cdar pages
))
349 (when (re-search-forward "^#authz\\s-+\\(.+\\)\n+" nil t
)
350 (let ((users-iter users
)
351 (authz (split-string (match-string 1))))
352 (while (and users-iter
(not not-found-p
))
353 (unless (member (car users-iter
) authz
)
354 (setq not-found-p t
))
355 (setq users-iter
(cdr users-iter
)))))
357 (setq result
(append (list (car pages
)) result
))))
358 (setq pages
(cdr pages
))))
361 (defun planner-authz-generate-mason-component (project)
362 "Generate the Mason component restricting content.
363 The component's name is taken from
364 `planner-authz-mason-component-name' and initialized with the
365 contents of `planner-authz-mason-component-contents'. The
366 component restricts access to users specified by <authz> and
369 (insert planner-authz-mason-component-contents
)
370 (let ((backup-inhibited t
)
371 (styles (cddr project
)))
373 (let ((path (muse-style-element :path
(car styles
))))
375 (string-match "mason" (muse-style-element :base
(car styles
)))
377 (concat (file-name-directory path
)
378 planner-authz-mason-component-name
))))
379 (setq styles
(cdr styles
))))))
381 (defun planner-authz-markup-section-predict (rule)
382 "Check if the prediction is satisfied."
383 (let ((predict (elt rule
1)))
384 (if (functionp predict
)
388 (defun planner-authz-markup-section ()
389 "Restrict section according to `planner-authz-sections-rule-list'."
390 (let ((begin (planner-line-beginning-position))
391 (rule-list planner-authz-sections-rule-list
)
397 (re-search-forward planner-authz-sections-regexp nil t
)
398 (setq section-level
(length (match-string 1)))
399 (setq section-name
(match-string 2)))
400 (let ((rule (catch 'done
402 (if (string-match (caar rule-list
) section-name
)
403 (throw 'done
(car rule-list
))
404 (setq rule-list
(cdr rule-list
))))
407 (planner-authz-markup-section-predict rule
))
410 (muse-publish-surround-text
411 (format "<authz users=\"%s\">\n"
412 (mapconcat 'identity
(elt rule
2) " "))
417 (re-search-forward planner-authz-sections-regexp nil t
)
418 (while (and (not found
)
419 (re-search-forward planner-authz-sections-regexp
421 (if (<= (length (match-string 1))
425 (goto-char (planner-line-beginning-position))
426 (goto-char (point-max))))))))))))
428 (defun planner-authz-markup-all-sections ()
429 "Run `planner-authz-markup-section' on the entire buffer."
430 (goto-char (point-min))
431 (while (re-search-forward planner-authz-sections-regexp nil t
)
432 (planner-authz-markup-section)))
434 (defun planner-authz-mason-markup-table ()
435 "Protect \"<&|\" Mason constructs from Muse table markup."
436 (let* ((beg (planner-line-beginning-position))
437 (style (muse-style-element :base
(muse-style)))
439 (muse-style-element :base style
)))
441 (muse-find-markup-element
442 :functions
'table
(muse-style-element :base base
)))))
443 (when (functionp func
)
447 (while (search-forward "<&|" (line-end-position) t
)
448 (replace-match "<&:" t t
))))
452 (while (search-forward "<&:" end t
)
453 (replace-match "<&|" t t
))))))
455 (defun planner-authz-index-as-string (&optional as-list exclude-private
)
456 "Generate an index of all Muse pages with authorization controls.
457 In the published index, only those links to pages which the remote
458 user is authorized to access will be shown.
459 If AS-LIST is non-nil, insert a dash and spaces before each item.
460 If EXCLUDE-PRIVATE is non-nil, exclude files that have private permissions.
461 If EXCLUDE-CURRENT is non-nil, exclude the current file from the output."
463 (insert (planner-index-as-string as-list exclude-private
))
464 (goto-char (point-min))
465 (while (and (re-search-forward
467 (concat "^[" muse-regexp-blank
"]+\\(-["
468 muse-regexp-blank
"]*\\)")
469 (concat "^\\([" muse-regexp-blank
"]*\\)"))
471 (save-match-data (looking-at muse-explicit-link-regexp
)))
473 (let ((func (muse-markup-function 'list
)))
475 (save-excursion (funcall func
))))
476 (re-search-forward "<li" nil t
)
477 (goto-char (match-beginning 0)))
478 (let* ((match (planner-link-base
479 (buffer-substring (point) (line-end-position))))
480 (users (if match
(planner-authz-users match
))))
482 (planner-insert-markup (muse-markup-text
483 'planner-authz-begin users
))
485 (re-search-forward "</li>" nil t
)
487 (planner-insert-markup (muse-markup-text 'planner-authz-end
)))))
488 (buffer-substring (point-min) (point-max))))
490 (defun planner-authz-republish-page-maybe (linked-page)
491 "Remember LINKED-PAGE to be republished later.
492 The page will be republished if and only if the current page is
494 (if (planner-authz-users)
495 (add-to-list 'planner-authz-pages-to-republish
496 (planner-page-file linked-page
))))
498 (defun planner-authz-tag (beg end attrs
)
499 "Publish <authz> tags. The region from BEG to END is protected.
500 ATTRS should be an alist of tag attributes including \"users\" and
501 optionally \"alt\" for alternative text to be displayed to
504 (let ((alt (or (cdr (assoc "alt" attrs
)) ""))
505 (users (or (cdr (assoc "users" attrs
)) "")))
507 (planner-insert-markup
508 (if (zerop (length alt
))
509 (muse-markup-text 'planner-authz-begin users
)
510 (muse-markup-text 'planner-authz-begin-alt users alt
)))
512 (planner-insert-markup (muse-markup-text 'planner-authz-end
)))))
514 (defun planner-authz-diary-tag (beg end attrs
)
515 "Restrict entries in a diary section."
518 (narrow-to-region beg end
)
519 (planner-publish-section-tag beg end attrs
)
521 (while (and (zerop (forward-line))
522 (= (point) (planner-line-beginning-position)))
523 (unless (looking-at "^\\(?:[ \t]*\\|No entries\\|</div>\\)$")
524 (let ((line-begin (point))
525 (line-end (line-end-position)))
526 (re-search-forward planner-authz-link-regexp line-end t
)
527 (let* ((link (match-string 1))
528 (linked-page (if link
(planner-link-base link
)))
531 (planner-authz-users linked-page
)
532 (and planner-authz-day-task-default
533 (mapconcat 'identity planner-authz-day-task-default
536 (if (and planner-authz-appt-alt planner-authz-appt-regexp
538 (goto-char line-begin
)
540 planner-authz-appt-regexp line-end t
)))
542 (search-forward " - " (+ 2 (point)) t
)
543 (planner-insert-markup
544 (muse-markup-text 'planner-authz-begin-alt linked-users
545 planner-authz-appt-alt
)))
546 (planner-insert-markup
547 (muse-markup-text 'planner-authz-begin linked-users
)))
549 (planner-insert-markup
550 (muse-markup-text 'planner-authz-end
))))))))))
552 (defun planner-authz-note-tag (beg end attrs
)
553 "Restrict notes linked to a restricted page. If this page is
554 restricted and the note is linked to another page, remember to
555 republish that page later and restrict the note as it appears there.
556 Call `planner-publish-note-tag' as a side effect."
559 (narrow-to-region beg end
)
560 (planner-publish-note-tag beg end attrs
)
561 (let* ((link (cdr (assoc "link" attrs
)))
562 (linked-page (if link
(planner-link-base link
)))
565 (planner-authz-users linked-page
)
566 (and planner-authz-day-note-default
567 (planner-authz-day-p)
569 planner-authz-day-note-default
" ")))))
571 ;; If this note is linked to another page, republish that page
572 ;; later to restrict the note as it appears there, providing that
573 ;; page has an authz restriction
576 (planner-authz-republish-page-maybe linked-page
))
578 ;; If the linked page has an authz restriction, restrict this note
581 (goto-char (point-min))
582 (planner-insert-markup
583 (muse-markup-text 'planner-authz-begin linked-users
))
585 (goto-char (point-max))
586 (planner-insert-markup (muse-markup-text 'planner-authz-end
))
589 (defun planner-authz-task-tag (beg end attrs
)
590 "Restrict tasks linked to restricted pages. If this page is
591 restricted and the task is linked to another page, remember to
592 republish that page later and restrict the task as it appears there.
593 Call `planner-publish-task-tag' as a side effect."
596 (narrow-to-region beg end
)
597 (planner-publish-task-tag beg end attrs
)
598 (let* ((link (cdr (assoc "link" attrs
)))
599 (linked-page (if link
(planner-link-base link
)))
602 (planner-authz-users linked-page
)
603 (and planner-authz-day-task-default
604 (planner-authz-day-p)
606 planner-authz-day-task-default
" ")))))
608 ;; If this task is linked to another page, republish that page
609 ;; later to restrict the task as it appears there, providing that
610 ;; page has an authz restriction
613 (planner-authz-republish-page-maybe linked-page
))
615 ;; If the linked page has an authz restriction, restrict this task
618 (goto-char (point-min))
619 (planner-insert-markup
620 (muse-markup-text 'planner-authz-begin linked-users
))
621 (goto-char (point-max))
622 (planner-insert-markup (muse-markup-text 'planner-authz-end
)))))))
624 (defun planner-authz-users (&optional page
)
625 "Return a list of acceptable users for PAGE.
626 The list of users is returned as space-separated string, based on
627 a #authz directive appearing in the page. If PAGE contains no
628 #authz directive and is a project page (it doesn't match
629 `planner-date-regexp'), return `planner-authz-project-default' as
630 a space-separated string.
632 If PAGE is nil, return a list of users associated with the
634 (unless page
(setq page
(planner-page-name)))
635 (let ((match (cdr (assoc page planner-authz-pages
))))
637 (let ((file (cdr (assoc page
(planner-file-alist)))))
641 (insert-file-contents-literally file
)
642 (if (re-search-forward "^#authz\\s-+\\(.+\\)\n+"
645 (planner-authz-default page
))))
646 (push `(,page .
,match
) planner-authz-pages
))
649 (add-hook 'muse-after-project-publish-hook
650 'planner-authz-after-project-publish
)
652 (let ((styles (list "html" "xhtml")))
654 (let ((style (concat "planner-authz-mason-" (car styles
))))
655 (unless (assoc style muse-publishing-styles
)
657 style
(concat "planner-" (car styles
))
658 :before
'planner-authz-before-markup
659 :after
'planner-authz-after-markup
660 :functions
'planner-authz-markup-functions
661 :strings
'planner-authz-mason-markup-strings
662 :tags
(append planner-authz-markup-tags
663 planner-publish-markup-tags
))))
664 (setq styles
(cdr styles
))))
666 (provide 'planner-authz
)
668 ;;; planner-authz.el ends here