124 lines
3.5 KiB
Perl
124 lines
3.5 KiB
Perl
# Selima Website Content Management System
|
|
# UserMem.pm: The user-to-group membership form checker.
|
|
|
|
# Copyright (c) 2004-2018 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: 2004-10-10
|
|
|
|
package Selima::Checker::UserMem;
|
|
use 5.008;
|
|
use strict;
|
|
use warnings;
|
|
use base qw(Selima::Checker);
|
|
|
|
use Selima::ChkFunc;
|
|
use Selima::CallForm;
|
|
use Selima::DataVars qw($DBH :forms);
|
|
use Selima::ShortCut;
|
|
|
|
# new: Initialize the checker
|
|
sub new : method {
|
|
local ($_, %_);
|
|
my $class;
|
|
($class, @_) = @_;
|
|
$_[1] = "usermem" if scalar(@_) < 2 || !defined $_[1];
|
|
return $class->SUPER::new(@_);
|
|
}
|
|
|
|
# check: Run a list of checks
|
|
sub check : method {
|
|
local ($_, %_);
|
|
my ($self, $error, @cols);
|
|
($self, @cols) = @_;
|
|
# Run the parent method first
|
|
$error = $self->SUPER::check(@cols);
|
|
return $error if defined $error;
|
|
# See if we need to check the duplicates. Check it in the end.
|
|
%_ = map { $_ => 1 } @cols;
|
|
if (exists $_{"grp"} && exists $_{"member"}) {
|
|
$error = $self->__check_dup();
|
|
return $error if defined $error;
|
|
}
|
|
return;
|
|
}
|
|
|
|
# _check_grp: Check the group
|
|
# Use the default group checker
|
|
|
|
# _check_member: Check the member
|
|
sub _check_member : method {
|
|
local ($_, %_);
|
|
my ($self, $form, $error);
|
|
$self = $_[0];
|
|
$form = $self->{"form"};
|
|
# Check if it exists
|
|
$error = $self->_missing("member");
|
|
return $error if defined $error;
|
|
# Regularize it
|
|
$self->_trim("member");
|
|
# Check if it is filled
|
|
return {"msg"=>N_("Please select a member.")}
|
|
if $form->param("member") eq "";
|
|
# Check if this user exists
|
|
return {"msg"=>N_("This member does not exist anymore. Please select another one.")}
|
|
if !check_sn_in ${$form->param_fetch("member")}[0], "users AS usrmembers";
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
# __check_dup: Check if this item is duplicated
|
|
sub __check_dup : method {
|
|
local ($_, %_);
|
|
my ($self, $form, $error, $sth, $sql);
|
|
$self = $_[0];
|
|
$form = $self->{"form"};
|
|
@_ = qw();
|
|
push @_, "grp=" . $form->param("grp");
|
|
push @_, "member=" . $form->param("member");
|
|
push @_, "sn!=" . $self->{"sn"}
|
|
if $self->{"iscur"};
|
|
$sql = "SELECT * FROM " . $DBH->quote_identifier($self->{"table"})
|
|
. " WHERE " . join(" AND ", @_) . ";\n";
|
|
$sth = $DBH->prepare($sql);
|
|
$sth->execute;
|
|
return {"msg"=>N_("This membership record already exists. You cannot create a duplicated one.")}
|
|
if $sth->rows > 0;
|
|
return;
|
|
}
|
|
|
|
# _redir_selmember: Suspend and move to the member selection form
|
|
sub _redir_selmember : method {
|
|
local ($_, %_);
|
|
my $self;
|
|
$self = $_[0];
|
|
# Skip if not requested
|
|
return if $self->_missing("selmember");
|
|
call_form FORM_USERS, undef, "import_selmember";
|
|
}
|
|
|
|
# _redir_delmember: Remove the member
|
|
sub _redir_delmember : method {
|
|
local ($_, %_);
|
|
my $self;
|
|
$self = $_[0];
|
|
# Skip if not requested
|
|
return if $self->_missing("delmember");
|
|
$self->{"form"}->delete("member");
|
|
success_redirect undef;
|
|
}
|
|
|
|
return 1;
|