ymlfront vs MMD
[ikiwiki] / doc / plugins / contrib / groupfile.mdwn
1 [[!template id=plugin name=groupfile core=0 author="[[Jogo]]"]]
2
3 This plugin add a `group(groupname)` function to [[ikiwiki/PageSpec]], which is true
4 only if the actual user is member of the group named `groupname`.
5
6 Groups membership are read from a file. The syntax of this file is very close to
7 usual `/etc/passwd` Unix file : the group's name, followed by a colon, followed by
8 a coma separated list of user's names. For exemple :
9
10     dev:toto,foo
11     i18n:zorba
12
13 -----
14
15     #!/usr/bin/perl
16     # GroupFile plugin.
17     # by Joseph Boudou <jogo at matabio dot net>
18     
19     package IkiWiki::Plugin::groupfile;
20     
21     use warnings;
22     use strict;
23     use IkiWiki 3.00;
24     
25     sub import {
26         hook(type => 'getsetup', id => 'groups', call => \&get_setup);
27     }
28     
29     sub get_setup () {
30         return (
31             plugin => {
32                 safe    => 0,
33                 rebuild => 0,
34             },
35             group_file => {
36                 type        => 'string',
37                 example     => '/etc/ikiwiki/group',
38                 description => 'group file location',
39                 safe        => 0,
40                 rebuild     => 0,
41             },
42         );
43     }
44     
45     my $users_of = 0;
46     
47     sub get_groups () {
48         if (not $users_of) {
49     
50             if (not defined $config{group_file}) {
51                 return 'group_file option not set';
52             }
53     
54             open my $file, '<', $config{group_file}
55                 or return 'Unable to open group_file';
56     
57             $users_of = {};
58             READ:
59             while (<$file>) {
60                 next READ if (/^\s*$/);
61     
62                 if (/^(\w+):([\w,]+)/) {
63                     %{ $users_of->{$1} } = map { $_ => 1 } split /,/, $2;
64                 }
65                 else {
66                     $users_of = "Error at group_file:$.";
67                     last READ;
68                 }
69             }
70     
71             close $file;
72         }
73     
74         return $users_of;
75     }
76     
77     package IkiWiki::PageSpec;
78     
79     sub match_group ($$;@) {
80         shift;
81         my $group  = shift;
82         my %params = @_;
83     
84         if (not exists $params{user}) {
85             return IkiWiki::ErrorReason->new('no user specified');
86         }
87         if (not defined $params{user}) {
88             return IkiWiki::FailReason->new('not logged in');
89         }
90     
91         my $users_of = IkiWiki::Plugin::groupfile::get_groups();
92         if (not ref $users_of) {
93             return IkiWiki::ErrorReason->new($users_of);
94         }
95     
96         if (exists $users_of->{$group}{ $params{user} }) {
97             return IkiWiki::SuccessReason->new("user is member of $group");
98         }
99         else {
100             return IkiWiki::FailReason->new(
101                 "user $params{user} isn't member of $group");
102         }
103     }
104     
105     1