358 lines
11 KiB
Perl
Executable File
358 lines
11 KiB
Perl
Executable File
#! /usr/bin/perl -w
|
|
# Mandy Wu's Website
|
|
# groups.cgi: The account group administration.
|
|
|
|
# Copyright (c) 2006-2021 imacat.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
# Author: imacat <imacat@mail.imacat.idv.tw>
|
|
# First written: 2006-11-14
|
|
|
|
use 5.008;
|
|
use strict;
|
|
use warnings;
|
|
use lib $ENV{"DOCUMENT_ROOT"} . qw(/magicat/lib/perl5);
|
|
use Selima::emandy;
|
|
local $SIG{"__DIE__"} = \&http_500;
|
|
my $d = new Selima::Destroy;
|
|
# Prototype declaration
|
|
sub main();
|
|
sub check_get();
|
|
sub check_post();
|
|
sub html_page($);
|
|
sub fetch_curitem();
|
|
sub import_selsubuser($);
|
|
sub import_selsubgroup($);
|
|
sub import_selsupgroup($);
|
|
|
|
initenv(-restricted => 1,
|
|
-this_table => "groups",
|
|
-dbi_lock => {"groups" => LOCK_EX,
|
|
"usermem" => LOCK_EX,
|
|
"groupmem" => LOCK_EX,
|
|
"users" => LOCK_SH,
|
|
"users AS members" => LOCK_SH,
|
|
"groups AS members" => LOCK_SH},
|
|
-lastmod => 1,
|
|
-page_param => {"keywords" => N_("groups")});
|
|
|
|
main;
|
|
exit 0;
|
|
|
|
sub main() {
|
|
local ($_, %_);
|
|
my ($error, $success, $processor);
|
|
|
|
# If the request is a GET query
|
|
if ($ENV{"REQUEST_METHOD"} ne "POST") {
|
|
$error = check_get;
|
|
# If an error occurs
|
|
if (defined $error) {
|
|
html_page $error;
|
|
|
|
# Display the page
|
|
} else {
|
|
html_page retrieve_status;
|
|
}
|
|
|
|
# If a form was POSTed from the client
|
|
} else {
|
|
$error = check_post;
|
|
# If an error occurs
|
|
if (defined $error) {
|
|
error_redirect $error;
|
|
|
|
# Else, save the data
|
|
} else {
|
|
$processor = new Selima::Processor::Group($POST);
|
|
$success = $processor->process;
|
|
success_redirect $success;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
# check_get: Check the GET arguments
|
|
sub check_get() {
|
|
local ($_, %_);
|
|
my ($error, $FORM, $sn);
|
|
|
|
# A form is requested
|
|
if (is_form) {
|
|
$_ = form_type;
|
|
# A form to create a new item
|
|
if ($_ eq "new") {
|
|
# Nothing to check on a new form
|
|
|
|
# A form to edit a current item
|
|
} elsif ($_ eq "cur") {
|
|
# Check at fetch_curitem()
|
|
$error = fetch_curitem;
|
|
return $error if defined $error;
|
|
|
|
# A form to delete a current item
|
|
} elsif ($_ eq "del") {
|
|
# Check the privilege to manage this table
|
|
$FORM = curform;
|
|
$sn = defined $FORM->param("sn")? $FORM->param("sn"): -1;
|
|
unauth if !is_su && $sn == su_group_sn;
|
|
# Check at fetch_curitem()
|
|
$error = fetch_curitem;
|
|
return $error if defined $error;
|
|
|
|
# Not a valid form
|
|
} else {
|
|
return {"msg"=>N_("Incorrect form: [_1]."),
|
|
"margs"=>[$_],
|
|
"isform"=>0};
|
|
}
|
|
}
|
|
# List handler handles its own error
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
# check_post: Check the POSTed form
|
|
sub check_post() {
|
|
local ($_, %_);
|
|
my ($checker, $error, $FORM, $sn);
|
|
$_ = form_type;
|
|
# A form to create a new item
|
|
if ($_ eq "new") {
|
|
# Run the checker
|
|
$checker = new Selima::Checker::Group(curform);
|
|
$checker->redir(qw(selsubuser selsubgroup selsupgroup));
|
|
$error = $checker->check(qw(id dsc subuser subgroup supgroup));
|
|
return $error if defined $error;
|
|
|
|
# A form to edit a current item
|
|
} elsif ($_ eq "cur") {
|
|
# Check at fetch_curitem()
|
|
$error = fetch_curitem;
|
|
return $error if defined $error;
|
|
# Run the checker
|
|
$checker = new Selima::Checker::Group(curform);
|
|
$checker->redir(qw(del selsubuser selsubgroup selsupgroup));
|
|
$error = $checker->check(qw(id dsc subuser subgroup supgroup));
|
|
return $error if defined $error;
|
|
|
|
# A form to delete a current item
|
|
} elsif ($_ eq "del") {
|
|
# Check the privilege to manage this table
|
|
$FORM = curform;
|
|
$sn = defined $FORM->param("sn")? $FORM->param("sn"): -1;
|
|
unauth if !is_su && $sn == su_group_sn;
|
|
# Check at fetch_curitem()
|
|
$error = fetch_curitem;
|
|
return $error if defined $error;
|
|
# Run the checker
|
|
$checker = new Selima::Checker::Group(curform);
|
|
$checker->redir(qw(cancel));
|
|
|
|
# Not a valid form
|
|
} else {
|
|
return {"msg"=>N_("Incorrect form: [_1]."),
|
|
"margs"=>[$_],
|
|
"isform"=>0};
|
|
}
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
# html_page: Display the page
|
|
sub html_page($) {
|
|
local ($_, %_);
|
|
my ($status, $LIST, $FORM);
|
|
$status = $_[0];
|
|
# A form is requested
|
|
if (is_form $status) {
|
|
$FORM = new Selima::Form::Group($status);
|
|
html_header $FORM->{"title"};
|
|
html_errmsg $status;
|
|
$FORM->html;
|
|
html_footer;
|
|
|
|
# List the available items
|
|
} else {
|
|
$LIST = new Selima::List::Groups;
|
|
html_header $LIST->{"title"}, $LIST->page_param;
|
|
html_errmsg $status;
|
|
$LIST->html;
|
|
html_footer;
|
|
}
|
|
return;
|
|
}
|
|
|
|
|
|
##################################
|
|
# Subroutines to manage the data #
|
|
##################################
|
|
# fetch_curitem: Fetch the current item
|
|
sub fetch_curitem() {
|
|
local ($_, %_);
|
|
my ($sn, $FORM, $sth, $sql, $row, $title);
|
|
|
|
# Return if fetched before
|
|
return if scalar(keys %CURRENT) > 0;
|
|
|
|
# Obtain the current form
|
|
$FORM = curform;
|
|
# No item specified
|
|
return {"msg"=>N_("Please select the group."),
|
|
"isform"=>0}
|
|
if !defined $FORM->param("sn");
|
|
$sn = $FORM->param("sn");
|
|
|
|
# Find the record
|
|
%CURRENT = fetchrec $sn, $THIS_TABLE;
|
|
# If this record exist
|
|
return {"msg"=>N_("This group does not exist anymore. Please select another one."),
|
|
"isform"=>0}
|
|
if scalar(keys %CURRENT) == 0;
|
|
|
|
# Obtain the user members list
|
|
$title = $DBH->strcat("users.id", "' ('", "users.name", "')'");
|
|
$sql = "SELECT users.sn AS sn,"
|
|
. " $title AS title"
|
|
. " FROM usermem"
|
|
. " INNER JOIN users ON usermem.member=users.sn"
|
|
. " WHERE usermem.grp=$sn"
|
|
. " ORDER BY users.id;\n";
|
|
$sth = $DBH->prepare($sql);
|
|
$sth->execute;
|
|
$CURRENT{"subusercount"} = $sth->rows;
|
|
for ($_ = 0; $_ < $CURRENT{"subusercount"}; $_++) {
|
|
$row = $sth->fetchrow_hashref;
|
|
$CURRENT{"subuser$_"} = 1;
|
|
$CURRENT{"subuser$_" . "sn"} = $$row{"sn"};
|
|
$CURRENT{"subuser$_" . "title"} = $$row{"title"};
|
|
}
|
|
|
|
# Obtain the group members list
|
|
$sql = "SELECT groups.sn AS sn,"
|
|
. " groups.dsc AS title FROM groupmem"
|
|
. " INNER JOIN groups ON groupmem.member=groups.sn"
|
|
. " WHERE groupmem.grp=$sn"
|
|
. " ORDER BY groups.id;\n";
|
|
$sth = $DBH->prepare($sql);
|
|
$sth->execute;
|
|
$CURRENT{"subgroupcount"} = $sth->rows;
|
|
for ($_ = 0; $_ < $CURRENT{"subgroupcount"}; $_++) {
|
|
$row = $sth->fetchrow_hashref;
|
|
$CURRENT{"subgroup$_"} = 1;
|
|
$CURRENT{"subgroup$_" . "sn"} = $$row{"sn"};
|
|
$CURRENT{"subgroup$_" . "title"} = $$row{"title"};
|
|
}
|
|
|
|
# Obtain the belonging groups list
|
|
$sql = "SELECT groups.sn AS sn,"
|
|
. " groups.dsc AS title FROM groupmem"
|
|
. " INNER JOIN groups ON groupmem.grp=groups.sn"
|
|
. " WHERE groupmem.member=$sn"
|
|
. " ORDER BY groups.id;\n";
|
|
$sth = $DBH->prepare($sql);
|
|
$sth->execute;
|
|
$CURRENT{"supgroupcount"} = $sth->rows;
|
|
for ($_ = 0; $_ < $CURRENT{"supgroupcount"}; $_++) {
|
|
$row = $sth->fetchrow_hashref;
|
|
$CURRENT{"supgroup$_"} = 1;
|
|
$CURRENT{"supgroup$_" . "sn"} = $$row{"sn"};
|
|
$CURRENT{"supgroup$_" . "title"} = $$row{"title"};
|
|
}
|
|
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
# import_selsubuser: Import the selected user into the retrieved form
|
|
sub import_selsubuser($) {
|
|
local ($_, %_);
|
|
my $FORM;
|
|
$FORM = $_[0];
|
|
# Sanity checks
|
|
if ( defined $GET->param("selsn")
|
|
&& check_sn_in ${$GET->param_fetch("selsn")}[0], "users AS members") {
|
|
# Get the current member list
|
|
%_ = map { $FORM->param($_) => 1 } grep /^subuser\d+sn$/, $FORM->param;
|
|
$_{$GET->param("selsn")} = 1;
|
|
@_ = sort { userid $a cmp userid $b } keys %_;
|
|
# Get the checked member list
|
|
%_ = map { $FORM->param($_ . "sn") => 1 }
|
|
grep /^subuser\d+$/ && defined $FORM->param($_ . "sn"), $FORM->param;
|
|
$_{$GET->param("selsn")} = 1;
|
|
# Remove the old values
|
|
$FORM->delete(grep /^subuser\d+/, $FORM->param);
|
|
# Add the current values
|
|
for ($_ = 0; $_ < @_; $_++) {
|
|
$FORM->param("subuser$_" . "sn", $_[$_]);
|
|
$FORM->param("subuser$_", 1) if exists $_{$_[$_]};
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
# import_selsubgroup: Import the selected user into the retrieved form
|
|
sub import_selsubgroup($) {
|
|
local ($_, %_);
|
|
my $FORM;
|
|
$FORM = $_[0];
|
|
# Sanity checks
|
|
if ( defined $GET->param("selsn")
|
|
&& check_sn_in ${$GET->param_fetch("selsn")}[0], "groups AS members") {
|
|
# Get the current member list
|
|
%_ = map { $FORM->param($_) => 1 } grep /^subgroup\d+sn$/, $FORM->param;
|
|
$_{$GET->param("selsn")} = 1;
|
|
@_ = sort { groupid $a cmp groupid $b } keys %_;
|
|
# Get the checked member list
|
|
%_ = map { $FORM->param($_ . "sn") => 1 }
|
|
grep /^subgroup\d+$/ && defined $FORM->param($_ . "sn"), $FORM->param;
|
|
$_{$GET->param("selsn")} = 1;
|
|
# Remove the old values
|
|
$FORM->delete(grep /^subgroup\d+/, $FORM->param);
|
|
# Add the current values
|
|
for ($_ = 0; $_ < @_; $_++) {
|
|
$FORM->param("subgroup$_" . "sn", $_[$_]);
|
|
$FORM->param("subgroup$_", 1) if exists $_{$_[$_]};
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
# import_selsupgroup: Import the selected user into the retrieved form
|
|
sub import_selsupgroup($) {
|
|
local ($_, %_);
|
|
my $FORM;
|
|
$FORM = $_[0];
|
|
# Sanity checks
|
|
if ( defined $GET->param("selsn")
|
|
&& check_sn_in ${$GET->param_fetch("selsn")}[0], "groups") {
|
|
# Get the current member list
|
|
%_ = map { $FORM->param($_) => 1 } grep /^supgroup\d+sn$/, $FORM->param;
|
|
$_{$GET->param("selsn")} = 1;
|
|
@_ = sort { groupid $a cmp groupid $b } keys %_;
|
|
# Get the checked member list
|
|
%_ = map { $FORM->param($_ . "sn") => 1 }
|
|
grep /^supgroup\d+$/ && defined $FORM->param($_ . "sn"), $FORM->param;
|
|
$_{$GET->param("selsn")} = 1;
|
|
# Remove the old values
|
|
$FORM->delete(grep /^supgroup\d+/, $FORM->param);
|
|
# Add the current values
|
|
for ($_ = 0; $_ < @_; $_++) {
|
|
$FORM->param("supgroup$_" . "sn", $_[$_]);
|
|
$FORM->param("supgroup$_", 1) if exists $_{$_[$_]};
|
|
}
|
|
}
|
|
return;
|
|
}
|