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();
76 my %users = map { ( $_->{id} => 1 ) } values %{ { $::auth->read_all_users() } };
78 foreach my $group (values %{$groups}) {
79 if (($form->{group_id} != $group->{id})
80 && ($form->{name} eq $group->{name})) {
81 $form->show_generic_error($locale->text("A group with that name does already exist."));
87 if ($form->{group_id} && $groups->{$form->{group_id}}) {
88 $group = $groups->{$form->{group_id}};
94 $group->{name} = $form->{name};
95 $group->{description} = $form->{description};
96 $group->{rights} = { map { ( $_ => $form->{"${_}_granted"} ? 1 : 0 ) } SL::Auth::all_rights() };
97 $group->{members} = [ grep { $users{$_} } @{ $form->{user_ids} || [] } ];
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}};
128 my %all_users = $main::auth->read_all_users();
130 { "right" => $_->[0],
131 "description" => $_->[1],
132 "is_section" => '--' eq substr($_->[0], 0, 2),
133 "granted" => defined $group->{rights}->{$_->[0]} ? $group->{rights}->{$_->[0]} : 0,
135 } SL::Auth::all_rights_full();
138 print $form->parse_html_template("admin/edit_group", { ALL_USERS => [ values %all_users ],
139 USER_IDS_IN_GROUP => $group->{members},
140 "RIGHTS" => \@rights,
141 "name" => $group->{name},
142 "description" => $group->{description} });
144 $main::lxdebug->leave_sub();
148 $main::lxdebug->enter_sub();
150 my $form = $main::form;
151 my $locale = $main::locale;
153 my $groups = $main::auth->read_groups();
155 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
156 $form->show_generic_error($locale->text("No group has been selected, or the group does not exist anymore."));
159 if ($form->{confirmed}) {
160 $main::auth->delete_group($form->{"group_id"});
162 $form->{message} = $locale->text("The group has been deleted.");
168 print $form->parse_html_template("admin/delete_group_confirm", $groups->{$form->{group_id}});
171 $main::lxdebug->leave_sub();
174 sub edit_group_membership {
175 $main::lxdebug->enter_sub();
177 my $form = $main::form;
178 my $locale = $main::locale;
180 my %users = $main::auth->read_all_users();
181 my $groups = $main::auth->read_groups();
182 $groups = [ sort { lc $a->{name} cmp lc $b->{name} } values %{ $groups } ];
184 my @headings = map { { 'title' => $_ } } map { $_->{name} } @{ $groups };
186 foreach my $group (@{ $groups }) {
187 $group->{members_h} = { map { $_ => 1 } @{ $group->{members} } };
192 foreach my $user (sort { lc $a->{login} cmp lc $b->{login} } values %users) {
195 'login' => $user->{login},
196 'name' => $user->{name},
197 'repeat_headings' => (scalar(@rows) % 20) == 0,
201 foreach my $group (@{ $groups }) {
202 push @{ $row->{GROUPS} }, {
203 'id' => $group->{id},
204 'is_member' => $group->{members_h}->{$user->{id}},
211 $form->{title} = $locale->text('Edit group membership');
213 print $form->parse_html_template('admin/edit_group_membership', { 'HEADINGS' => \@headings, 'USERS' => \@rows });
215 $main::lxdebug->leave_sub();
218 sub save_group_membership {
219 $main::lxdebug->enter_sub();
221 my $form = $main::form;
222 my $locale = $main::locale;
224 my %users = $main::auth->read_all_users();
225 my $groups = $main::auth->read_groups();
227 foreach my $group (values %{ $groups }) {
228 $group->{members} = [ ];
230 foreach my $user (values %users) {
231 push @{ $group->{members} }, $user->{id} if ($form->{"u_$user->{id}_g_$group->{id}"});
234 $main::auth->save_group($group);
237 $form->{message} = $locale->text('The group memberships have been saved.');
241 $main::lxdebug->leave_sub();