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 #======================================================================
36 $lxdebug->enter_sub();
38 my @groups = sort { lc $a->{name} cmp lc $b->{name} } values %{ $auth->read_groups() };
41 print $form->parse_html_template("admin/edit_groups", { 'GROUPS' => \@groups,
42 'num_groups' => scalar @groups });
44 $lxdebug->leave_sub();
48 $lxdebug->enter_sub();
50 delete $form->{group_id};
51 $form->{message} = $locale->text("The group has been added.");
55 $lxdebug->leave_sub();
59 $lxdebug->enter_sub();
61 $form->isblank('name', $locale->text('The group name is missing.'));
63 my $groups = $auth->read_groups();
65 foreach my $group (values %{$groups}) {
66 if (($form->{group_id} != $group->{id})
67 && ($form->{name} eq $group->{name})) {
68 $form->show_generic_error($locale->text("A group with that name does already exist."));
74 if ($form->{group_id} && $groups->{$form->{group_id}}) {
75 $group = $groups->{$form->{group_id}};
81 $group->{name} = $form->{name};
82 $group->{description} = $form->{description};
83 $group->{rights} = {};
85 map { $group->{rights}->{$_} = $form->{"${_}_granted"} ? 1 : 0 } SL::Auth::all_rights();
87 my $is_new = !$form->{group_id};
89 $auth->save_group($group);
91 $form->{message} ||= $locale->text('The group has been saved.');
100 $lxdebug->leave_sub();
104 $lxdebug->enter_sub();
106 my $groups = $auth->read_groups();
108 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
109 $form->show_generic_error($locale->text("No group has been selected, or the group does not exist anymore."));
112 $group = $groups->{$form->{group_id}};
114 my %all_users = $auth->read_all_users();
115 my %users_by_id = map { $_->{id} => $_ } values %all_users;
117 my @members = sort { lc $a->{login} cmp lc $b->{login} } @users_by_id{ @{ $group->{members} } };
119 my %grouped = map { $_ => 1 } @{ $group->{members} };
120 my @non_members = sort { lc $a->{login} cmp lc $b->{login} } grep { !$grouped{$_->{id}} } values %all_users;
123 { "right" => $_->[0],
124 "description" => $_->[1],
125 "is_section" => '--' eq substr($_->[0], 0, 2),
126 "granted" => defined $group->{rights}->{$_->[0]} ? $group->{rights}->{$_->[0]} : 0,
128 } SL::Auth::all_rights_full();
131 print $form->parse_html_template("admin/edit_group", { "USERS_IN_GROUP" => \@members,
132 "USERS_NOT_IN_GROUP" => \@non_members,
133 "RIGHTS" => \@rights,
134 "name" => $group->{name},
135 "description" => $group->{description} });
137 $lxdebug->leave_sub();
141 $lxdebug->enter_sub();
143 my $groups = $auth->read_groups();
145 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
146 $form->show_generic_error($locale->text("No group has been selected, or the group does not exist anymore."));
149 if ($form->{confirmed}) {
150 $auth->delete_group($form->{"group_id"});
152 $form->{message} = $locale->text("The group has been deleted.");
158 print $form->parse_html_template("admin/delete_group_confirm", $groups->{$form->{group_id}});
161 $lxdebug->leave_sub();
165 $lxdebug->enter_sub();
167 $form->isblank('user_id_not_in_group', $locale->text('No user has been selected.'));
169 my $groups = $auth->read_groups();
171 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
172 $form->show_generic_error($locale->text('No group has been selected, or the group does not exist anymore.'));
175 $group = $groups->{$form->{group_id}};
176 push @{ $group->{members} }, $form->{user_id_not_in_group};
178 $auth->save_group($group);
180 $form->{message} = $locale->text('The user has been added to this group.');
183 $lxdebug->leave_sub();
186 sub remove_from_group {
187 $lxdebug->enter_sub();
189 $form->isblank('user_id_in_group', $locale->text('No user has been selected.'));
191 my $groups = $auth->read_groups();
193 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
194 $form->show_generic_error($locale->text('No group has been selected, or the group does not exist anymore.'));
197 $group = $groups->{$form->{group_id}};
198 $group->{members} = [ grep { $_ ne $form->{user_id_in_group} } @{ $group->{members} } ];
200 $auth->save_group($group);
202 $form->{message} = $locale->text('The user has been removed from this group.');
205 $lxdebug->leave_sub();
208 sub edit_group_membership {
209 $lxdebug->enter_sub();
211 my %users = $auth->read_all_users();
212 my $groups = $auth->read_groups();
213 $groups = [ sort { lc $a->{name} cmp lc $b->{name} } values %{ $groups } ];
215 my @headings = map { { 'title' => $_ } } map { $_->{name} } @{ $groups };
217 foreach my $group (@{ $groups }) {
218 $group->{members_h} = { map { $_ => 1 } @{ $group->{members} } };
223 foreach my $user (sort { lc $a->{login} cmp lc $b->{login} } values %users) {
226 'login' => $user->{login},
227 'name' => $user->{name},
228 'repeat_headings' => (scalar(@rows) % 20) == 0,
232 foreach my $group (@{ $groups }) {
233 push @{ $row->{GROUPS} }, {
234 'id' => $group->{id},
235 'is_member' => $group->{members_h}->{$user->{id}},
242 $form->{title} = $locale->text('Edit group membership');
244 print $form->parse_html_template('admin/edit_group_membership', { 'HEADINGS' => \@headings, 'USERS' => \@rows });
246 $lxdebug->leave_sub();
249 sub save_group_membership {
250 $lxdebug->enter_sub();
252 my %users = $auth->read_all_users();
253 my $groups = $auth->read_groups();
255 foreach my $group (values %{ $groups }) {
256 $group->{members} = [ ];
258 foreach my $user (values %users) {
259 push @{ $group->{members} }, $user->{id} if ($form->{"u_$user->{id}_g_$group->{id}"});
262 $auth->save_group($group);
265 $form->{message} = $locale->text('The group memberships have been saved.');
269 $lxdebug->leave_sub();