File: Groups.pm

package info (click to toggle)
faqomatic 2.721-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,984 kB
  • ctags: 548
  • sloc: perl: 13,356; sh: 69; makefile: 47
file content (262 lines) | stat: -rw-r--r-- 7,869 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
##############################################################################
# The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved.      #
#                                                                            #
# This program is free software; you can redistribute it and/or              #
# modify it under the terms of the GNU General Public License                #
# as published by the Free Software Foundation; either version 2             #
# of the License, or (at your option) any later version.                     #
#                                                                            #
# This program is distributed in the hope that it will be useful,            #
# but WITHOUT ANY WARRANTY; without even the implied warranty of             #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
# GNU General Public License for more details.                               #
#                                                                            #
# You should have received a copy of the GNU General Public License          #
# along with this program; if not, write to the Free Software                #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.#
#                                                                            #
# Jon Howell can be contacted at:                                            #
# 6211 Sudikoff Lab, Dartmouth College                                       #
# Hanover, NH  03755-3510                                                    #
# jonh@cs.dartmouth.edu                                                      #
#                                                                            #
# An electronic copy of the GPL is available at:                             #
# http://www.gnu.org/copyleft/gpl.html                                       #
#                                                                            #
##############################################################################

use strict;

###
### FAQ::OMatic::Groups manages group membership, so you can control postings
### more finely than {moderator-only or anyone-with-an-email-address}.
###

package FAQ::OMatic::Groups;

use FAQ::OMatic;
use FAQ::OMatic::I18N;

sub readGroups {
	# groups are cached per CGI invocation so we don't have to read
	# the groups file from the filesystem multiple times.
	# We store the cache in the s/getLocal() mechanism so that it
	# doesn't persist across invocations on a mod_perl child.
	my $groupCache = FAQ::OMatic::getLocal('groupCache');
	return $groupCache if (defined $groupCache);

	if (not open GROUPS, "$FAQ::OMatic::Config::metaDir/groups") {
		$groupCache = {};
	} else {
		while (defined($_=<GROUPS>)) {
			chomp;
			my ($groupName, $member) = split('\s', $_, 2);
			$groupCache->{$groupName}{$member} = 1;
		}
		close GROUPS;
	}

	# Make sure the one special group ('Administrators') always appears,
	# even if it has no members. By deleting, we avoid disturbing any
	# loaded hash, but ensures Perl creates a hash for this group.
	delete $groupCache->{'Administrators'}{''};

	FAQ::OMatic::setLocal('groupCache', $groupCache);
	return $groupCache;
}

sub writeGroups {
	my $groups = shift;
	my $groupCache = readGroups();
	$groupCache = $groups if (defined $groups);	# allow caller to overwrite

	if (not open GROUPS, ">$FAQ::OMatic::Config::metaDir/groups") {
		FAQ::OMatic::gripe('abort',
			"Can't write to $FAQ::OMatic::Config::metaDir/groups: $!.");
	}
	my ($groupName, $member);
	foreach $groupName (sort keys %{$groupCache}) {
		foreach $member (sort keys %{$groupCache->{$groupName}}) {
			print GROUPS "$groupName $member\n";
		}
	}
	close GROUPS;
}

sub getGroupNameList {
	my $groupCache = readGroups();
	return sort keys %{$groupCache};
}

sub groupCodeToName {
	my $code = shift;
	$code =~ s/^6 //;
	return $code;	# boy, that was easy.
}

sub groupNameToCode {
	my $code = shift;
	return "6 ".$code;
}

sub getGroupCodeList {
	readGroups();
	return map {groupNameToCode($_)} getGroupNameList();
}

sub checkMembership {
	my $code = shift;
	my $id = shift;

	my $groupCache = readGroups();
	return 1 if ($id eq $FAQ::OMatic::Config::adminAuth);

	readGroups();

	# By checking for the existence of the group first, we avoid
	# "creating" that group in the in-core cache as a side effect of
	# looking in its hash for $id.
	return 0 if (not $groupCache->{groupCodeToName($code)});

	# check for a direct user match:
	return 1 if ($groupCache->{groupCodeToName($code)}{$id});

	# check if any domains match a suffix of user's id:
	my @members = keys %{$groupCache->{groupCodeToName($code)}};
	my @domains = grep {not FAQ::OMatic::validEmail($_)} @members;
	my $domain;
	foreach $domain (@domains) {
		return 1 if ($id =~ m/$domain$/);
	}

	return 0;
}

sub displayHTML {
	my $group = shift;
	my $html = '';

	my $groupCache = readGroups();

	if (not $group) {
		$html.=gettext("Select a group to edit:")."<dl>\n";
		my ($groupName,$member);
		foreach $groupName (getGroupNameList()) {
			$html.="<dt>"
				.FAQ::OMatic::makeAref('editGroups', {'group'=>$groupName})
				."$groupName</a>\n";
			if ($groupName eq 'Administrators') {
				$html.="<dd><i>"
					.gettext("(Members of this group are allowed to access these group definition pages.)")
					."</i>\n";
			}
			my $limit=4;
			foreach $member
				(sort {sortEmail($a,$b)} keys %{$groupCache->{$groupName}}) {

				$html.="<dd>$member\n";
				if (--$limit <= 0) {
					$html.="<dd>...\n";
					last;
				}
			}
		}
		$html.="</dl>\n";
		$html.=FAQ::OMatic::makeAref('editGroups', {'group'=>''}, 'GET')
			."<input type=text size=30 name=\"group\">\n"
			."<input type=submit name=\"_junk\" value=\""
			.gettext("Add Group")
			."\">\n"
			."</form>\n";
	} else {
		validGroupName($group);
		$html.="<p>".FAQ::OMatic::button(
			FAQ::OMatic::makeAref('editGroups', {'group'=>''}),
			gettext("Up To List Of Groups"));

		$html.="<table>\n"
			."<tr><td></td><td><b>$group</b></td></tr>\n";
		my $member;
		foreach $member
			(sort {sortEmail($a,$b)} keys %{$groupCache->{$group}}) {

			$html.="<tr><td align=right>\n"
				.FAQ::OMatic::button(
					FAQ::OMatic::makeAref('submitGroup',
						{'_action'=>'remove', '_member'=>$member}),
					gettext("Remove Member"))
				."</td><td>"
				."$member\n"
				."</td></tr>\n";
		}
		$html.="<form><tr><td align=right valign=bottom>"
				.FAQ::OMatic::makeAref('submitGroup',
					{'_action'=>'add'}, 'GET')
				."<input type=submit name=\"_junk\" value=\""
				.gettext("Add Member")
				."\">\n"
				."</td><td valign=bottom>\n"
				."<input type=text size=50 name=\"_member\">\n"
				."</td></tr></form>\n";
		$html.="</table>\n";
	}

	$html.="<p>".FAQ::OMatic::button(
			FAQ::OMatic::makeAref('faq', {'group'=>''}),
			gettext("Go to the Faq-O-Matic"));
	$html.=" ".FAQ::OMatic::button(
			FAQ::OMatic::makeAref('install', {'group'=>''}),
			gettext("Go To Install/Configuration Page"));
	$html.="\n";

	return $html;
}

sub addMember {
	my $group = shift;
	my $member = shift;

	my $groupCache = readGroups();
	$groupCache->{$group}{$member} = 1;
	writeGroups();
}

sub removeMember {
	my $group = shift;
	my $member = shift;

	my $groupCache = readGroups();
	delete $groupCache->{$group}{$member};
	writeGroups();
}

sub validGroupName {
	my $group = shift;

	if (not $group =~ m/^[\w.-]+$/) {
		FAQ::OMatic::gripe('error',
			"Group names may only contain alphanumerics, "
			."periods, and hyphens.");
	}
}

sub sortEmail {
	my $a = shift;
	my $b = shift;
	my ($auser,$adomain,$buser,$bdomain);

	if ($a =~ m'@') {
		($auser,$adomain) = split('@', $a);
	} else {
		($auser,$adomain) = ('', $a);
	}
	if ($b =~ m'@') {
		($buser,$bdomain) = split('@', $b);
	} else {
		($buser,$bdomain) = ('', $b);
	}
	
	return ($adomain cmp $bdomain) || ($auser cmp $buser);
}

1;