1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Moritz Bunkus
12 # Email: mbunkus@linet-services.de
13 # Web: www.linet-services.de
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
30 # group administration module
31 # add/edit/delete user groups
33 #======================================================================
35 use List::MoreUtils qw(uniq);
40 $main::lxdebug->enter_sub();
42 my $form = $main::form;
44 my @groups = sort { lc $a->{name} cmp lc $b->{name} } values %{ $main::auth->read_groups() };
47 print $form->parse_html_template("admin/edit_groups", { 'GROUPS' => \@groups,
48 'num_groups' => scalar @groups });
50 $main::lxdebug->leave_sub();
54 $main::lxdebug->enter_sub();
56 my $form = $main::form;
57 my $locale = $main::locale;
59 delete $form->{group_id};
60 $form->{message} = $locale->text("The group has been added.");
64 $main::lxdebug->leave_sub();
68 $main::lxdebug->enter_sub();
70 my $form = $main::form;
71 my $locale = $main::locale;
73 $form->isblank('name', $locale->text('The group name is missing.'));
75 my $groups = $main::auth->read_groups();
77 foreach my $group (values %{$groups}) {
78 if (($form->{group_id} != $group->{id})
79 && ($form->{name} eq $group->{name})) {
80 $form->show_generic_error($locale->text("A group with that name does already exist."));
86 if ($form->{group_id} && $groups->{$form->{group_id}}) {
87 $group = $groups->{$form->{group_id}};
93 $group->{name} = $form->{name};
94 $group->{description} = $form->{description};
95 $group->{rights} = {};
97 map { $group->{rights}->{$_} = $form->{"${_}_granted"} ? 1 : 0 } SL::Auth::all_rights();
99 my $is_new = !$form->{group_id};
101 $main::auth->save_group($group);
103 $form->{message} ||= $locale->text('The group has been saved.');
112 $main::lxdebug->leave_sub();
116 $main::lxdebug->enter_sub();
118 my $form = $main::form;
119 my $locale = $main::locale;
121 my $groups = $main::auth->read_groups();
123 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
124 $form->show_generic_error($locale->text("No group has been selected, or the group does not exist anymore."));
127 my $group = $groups->{$form->{group_id}};
129 my %all_users = $main::auth->read_all_users();
130 my %users_by_id = map { $_->{id} => $_ } values %all_users;
132 my @members = uniq sort { lc $a->{login} cmp lc $b->{login} } @users_by_id{ @{ $group->{members} } };
134 my %grouped = map { $_ => 1 } uniq @{ $group->{members} };
135 my @non_members = sort { lc $a->{login} cmp lc $b->{login} } grep { !$grouped{$_->{id}} } values %all_users;
138 { "right" => $_->[0],
139 "description" => $_->[1],
140 "is_section" => '--' eq substr($_->[0], 0, 2),
141 "granted" => defined $group->{rights}->{$_->[0]} ? $group->{rights}->{$_->[0]} : 0,
143 } SL::Auth::all_rights_full();
146 print $form->parse_html_template("admin/edit_group", { "USERS_IN_GROUP" => \@members,
147 "USERS_NOT_IN_GROUP" => \@non_members,
148 "RIGHTS" => \@rights,
149 "name" => $group->{name},
150 "description" => $group->{description} });
152 $main::lxdebug->leave_sub();
156 $main::lxdebug->enter_sub();
158 my $form = $main::form;
159 my $locale = $main::locale;
161 my $groups = $main::auth->read_groups();
163 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
164 $form->show_generic_error($locale->text("No group has been selected, or the group does not exist anymore."));
167 if ($form->{confirmed}) {
168 $main::auth->delete_group($form->{"group_id"});
170 $form->{message} = $locale->text("The group has been deleted.");
176 print $form->parse_html_template("admin/delete_group_confirm", $groups->{$form->{group_id}});
179 $main::lxdebug->leave_sub();
183 $main::lxdebug->enter_sub();
185 my $form = $main::form;
186 my $locale = $main::locale;
188 $form->isblank('user_id_not_in_group', $locale->text('No user has been selected.'));
190 my $groups = $main::auth->read_groups();
192 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
193 $form->show_generic_error($locale->text('No group has been selected, or the group does not exist anymore.'));
196 my $group = $groups->{$form->{group_id}};
197 $group->{members} = [ uniq @{ $group->{members} }, $form->{user_id_not_in_group} ];
199 $main::auth->save_group($group);
201 $form->{message} = $locale->text('The user has been added to this group.');
204 $main::lxdebug->leave_sub();
207 sub remove_from_group {
208 $main::lxdebug->enter_sub();
210 my $form = $main::form;
211 my $locale = $main::locale;
213 $form->isblank('user_id_in_group', $locale->text('No user has been selected.'));
215 my $groups = $main::auth->read_groups();
217 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
218 $form->show_generic_error($locale->text('No group has been selected, or the group does not exist anymore.'));
221 my $group = $groups->{$form->{group_id}};
222 $group->{members} = [ uniq grep { $_ ne $form->{user_id_in_group} } @{ $group->{members} } ];
224 $main::auth->save_group($group);
226 $form->{message} = $locale->text('The user has been removed from this group.');
229 $main::lxdebug->leave_sub();
232 sub edit_group_membership {
233 $main::lxdebug->enter_sub();
235 my $form = $main::form;
236 my $locale = $main::locale;
238 my %users = $main::auth->read_all_users();
239 my $groups = $main::auth->read_groups();
240 $groups = [ sort { lc $a->{name} cmp lc $b->{name} } values %{ $groups } ];
242 my @headings = map { { 'title' => $_ } } map { $_->{name} } @{ $groups };
244 foreach my $group (@{ $groups }) {
245 $group->{members_h} = { map { $_ => 1 } @{ $group->{members} } };
250 foreach my $user (sort { lc $a->{login} cmp lc $b->{login} } values %users) {
253 'login' => $user->{login},
254 'name' => $user->{name},
255 'repeat_headings' => (scalar(@rows) % 20) == 0,
259 foreach my $group (@{ $groups }) {
260 push @{ $row->{GROUPS} }, {
261 'id' => $group->{id},
262 'is_member' => $group->{members_h}->{$user->{id}},
269 $form->{title} = $locale->text('Edit group membership');
271 print $form->parse_html_template('admin/edit_group_membership', { 'HEADINGS' => \@headings, 'USERS' => \@rows });
273 $main::lxdebug->leave_sub();
276 sub save_group_membership {
277 $main::lxdebug->enter_sub();
279 my $form = $main::form;
280 my $locale = $main::locale;
282 my %users = $main::auth->read_all_users();
283 my $groups = $main::auth->read_groups();
285 foreach my $group (values %{ $groups }) {
286 $group->{members} = [ ];
288 foreach my $user (values %users) {
289 push @{ $group->{members} }, $user->{id} if ($form->{"u_$user->{id}_g_$group->{id}"});
292 $main::auth->save_group($group);
295 $form->{message} = $locale->text('The group memberships have been saved.');
299 $main::lxdebug->leave_sub();