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);
38 $lxdebug->enter_sub();
40 my @groups = sort { lc $a->{name} cmp lc $b->{name} } values %{ $auth->read_groups() };
43 print $form->parse_html_template("admin/edit_groups", { 'GROUPS' => \@groups,
44 'num_groups' => scalar @groups });
46 $lxdebug->leave_sub();
50 $lxdebug->enter_sub();
52 delete $form->{group_id};
53 $form->{message} = $locale->text("The group has been added.");
57 $lxdebug->leave_sub();
61 $lxdebug->enter_sub();
63 $form->isblank('name', $locale->text('The group name is missing.'));
65 my $groups = $auth->read_groups();
67 foreach my $group (values %{$groups}) {
68 if (($form->{group_id} != $group->{id})
69 && ($form->{name} eq $group->{name})) {
70 $form->show_generic_error($locale->text("A group with that name does already exist."));
76 if ($form->{group_id} && $groups->{$form->{group_id}}) {
77 $group = $groups->{$form->{group_id}};
83 $group->{name} = $form->{name};
84 $group->{description} = $form->{description};
85 $group->{rights} = {};
87 map { $group->{rights}->{$_} = $form->{"${_}_granted"} ? 1 : 0 } SL::Auth::all_rights();
89 my $is_new = !$form->{group_id};
91 $auth->save_group($group);
93 $form->{message} ||= $locale->text('The group has been saved.');
102 $lxdebug->leave_sub();
106 $lxdebug->enter_sub();
108 my $groups = $auth->read_groups();
110 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
111 $form->show_generic_error($locale->text("No group has been selected, or the group does not exist anymore."));
114 $group = $groups->{$form->{group_id}};
116 my %all_users = $auth->read_all_users();
117 my %users_by_id = map { $_->{id} => $_ } values %all_users;
119 my @members = uniq sort { lc $a->{login} cmp lc $b->{login} } @users_by_id{ @{ $group->{members} } };
121 my %grouped = map { $_ => 1 } uniq @{ $group->{members} };
122 my @non_members = sort { lc $a->{login} cmp lc $b->{login} } grep { !$grouped{$_->{id}} } values %all_users;
125 { "right" => $_->[0],
126 "description" => $_->[1],
127 "is_section" => '--' eq substr($_->[0], 0, 2),
128 "granted" => defined $group->{rights}->{$_->[0]} ? $group->{rights}->{$_->[0]} : 0,
130 } SL::Auth::all_rights_full();
133 print $form->parse_html_template("admin/edit_group", { "USERS_IN_GROUP" => \@members,
134 "USERS_NOT_IN_GROUP" => \@non_members,
135 "RIGHTS" => \@rights,
136 "name" => $group->{name},
137 "description" => $group->{description} });
139 $lxdebug->leave_sub();
143 $lxdebug->enter_sub();
145 my $groups = $auth->read_groups();
147 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
148 $form->show_generic_error($locale->text("No group has been selected, or the group does not exist anymore."));
151 if ($form->{confirmed}) {
152 $auth->delete_group($form->{"group_id"});
154 $form->{message} = $locale->text("The group has been deleted.");
160 print $form->parse_html_template("admin/delete_group_confirm", $groups->{$form->{group_id}});
163 $lxdebug->leave_sub();
167 $lxdebug->enter_sub();
169 $form->isblank('user_id_not_in_group', $locale->text('No user has been selected.'));
171 my $groups = $auth->read_groups();
173 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
174 $form->show_generic_error($locale->text('No group has been selected, or the group does not exist anymore.'));
177 $group = $groups->{$form->{group_id}};
178 $group->{members} = [ uniq @{ $group->{members} }, $form->{user_id_not_in_group} ];
180 $auth->save_group($group);
182 $form->{message} = $locale->text('The user has been added to this group.');
185 $lxdebug->leave_sub();
188 sub remove_from_group {
189 $lxdebug->enter_sub();
191 $form->isblank('user_id_in_group', $locale->text('No user has been selected.'));
193 my $groups = $auth->read_groups();
195 if (!$form->{group_id} || !$groups->{$form->{group_id}}) {
196 $form->show_generic_error($locale->text('No group has been selected, or the group does not exist anymore.'));
199 $group = $groups->{$form->{group_id}};
200 $group->{members} = [ uniq grep { $_ ne $form->{user_id_in_group} } @{ $group->{members} } ];
202 $auth->save_group($group);
204 $form->{message} = $locale->text('The user has been removed from this group.');
207 $lxdebug->leave_sub();
210 sub edit_group_membership {
211 $lxdebug->enter_sub();
213 my %users = $auth->read_all_users();
214 my $groups = $auth->read_groups();
215 $groups = [ sort { lc $a->{name} cmp lc $b->{name} } values %{ $groups } ];
217 my @headings = map { { 'title' => $_ } } map { $_->{name} } @{ $groups };
219 foreach my $group (@{ $groups }) {
220 $group->{members_h} = { map { $_ => 1 } @{ $group->{members} } };
225 foreach my $user (sort { lc $a->{login} cmp lc $b->{login} } values %users) {
228 'login' => $user->{login},
229 'name' => $user->{name},
230 'repeat_headings' => (scalar(@rows) % 20) == 0,
234 foreach my $group (@{ $groups }) {
235 push @{ $row->{GROUPS} }, {
236 'id' => $group->{id},
237 'is_member' => $group->{members_h}->{$user->{id}},
244 $form->{title} = $locale->text('Edit group membership');
246 print $form->parse_html_template('admin/edit_group_membership', { 'HEADINGS' => \@headings, 'USERS' => \@rows });
248 $lxdebug->leave_sub();
251 sub save_group_membership {
252 $lxdebug->enter_sub();
254 my %users = $auth->read_all_users();
255 my $groups = $auth->read_groups();
257 foreach my $group (values %{ $groups }) {
258 $group->{members} = [ ];
260 foreach my $user (values %users) {
261 push @{ $group->{members} }, $user->{id} if ($form->{"u_$user->{id}_g_$group->{id}"});
264 $auth->save_group($group);
267 $form->{message} = $locale->text('The group memberships have been saved.');
271 $lxdebug->leave_sub();