# Selima Website Content Management System # Group.pm: The account group 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 # First written: 2004-10-12 package Selima::Checker::Group; use 5.008; use strict; use warnings; use base qw(Selima::Checker); use Selima::CallForm; use Selima::ChkPriv; use Selima::DataVars qw($DBH :forms); use Selima::ShortCut; use Selima::UserName; use Selima::Checker::UserMem; use Selima::Checker::GroupMem; # new: Initialize the checker sub new : method { local ($_, %_); my $class; ($class, @_) = @_; $_[1] = "groups" if scalar(@_) < 2 || !defined $_[1]; return $class->SUPER::new(@_); } # _check_id: Check the group ID. sub _check_id : method { local ($_, %_); my ($self, $form, $error, $sth, $sql); $self = $_[0]; $form = $self->{"form"}; # Skip for a non-super-user editing a super-user group return if $self->{"iscur"} && !is_su && $self->{"sn"} == su_group_sn; # Check if it exists $error = $self->_missing("id"); return $error if defined $error; # Regularize it $self->_trim("id"); # Check if it is filled return {"msg"=>N_("Please fill in the group ID.")} if $form->param("id") eq ""; # Check the length return {"msg"=>N_("This group ID. is too long. (Max. length [#,_1])"), "margs"=>[${$self->{"maxlens"}}{"id"}]} if length $form->param("id") > ${$self->{"maxlens"}}{"id"}; return {"msg"=>N_("This group ID. is too short. (Min. length [#,_1])"), "margs"=>[${$self->{"minlens"}}{"id"}]} if length $form->param("id") < ${$self->{"minlens"}}{"id"}; # Check if the characters used are valid return {"msg"=>N_("Only lower-case English letters, numbers and underscores are allowed for the group ID.")} unless $form->param("id") =~ /^[a-z][a-z0-9_]*$/; # Check if this item is duplicated @_ = qw(); push @_, "id=" . $DBH->quote($form->param("id")); 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 group already exists. You cannot create a duplicated one.")} if $sth->rows > 0; # OK return; } # _check_dsc: Check the group description sub _check_dsc : method { local ($_, %_); my ($self, $form, $error); $self = $_[0]; $form = $self->{"form"}; # Check if it exists $error = $self->_missing("id"); return $error if defined $error; # Regularize it $self->_trim("dsc"); # Check if it is filled return {"msg"=>N_("Please fill in the privilege description.")} if $form->param("dsc") eq ""; # Check the length return {"msg"=>N_("This privilege description is too long. (Max. length [#,_1])"), "margs"=>[${$self->{"maxlens"}}{"dsc"}]} if length $form->param("dsc") > ${$self->{"maxlens"}}{"dsc"}; # OK return; } # _check_subuser: Check the user members sub _check_subuser : method { local ($_, %_); my ($self, $form, $error, $subform, $checker); $self = $_[0]; $form = $self->{"form"}; # Skip for a non-super-user editing a super-user group return if $self->{"iscur"} && !is_su && $self->{"sn"} == su_group_sn; # Get the selected items @_ = map $_ . "sn", grep /^subuser\d+/ && defined $form->param($_ . "sn"), $form->param; # Regularize them $self->_trim(@_); # Merge the duplicates %_ = map { ($form->param($_))[0] => 1 } @_; $subform = new CGI(""); $subform->param("grp", $self->{"sn"}) if $self->{"iscur"}; foreach (keys %_) { $subform->param("member", $_); $checker = new Selima::Checker::UserMem($subform); $error = $checker->check("member"); return $error if defined $error; } # OK return; } # _check_subgroup: Check the group members sub _check_subgroup : method { local ($_, %_); my ($self, $form, $error, $subform, $checker); $self = $_[0]; $form = $self->{"form"}; # Skip for a non-super-user editing a super-user group return if $self->{"iscur"} && !is_su && $self->{"sn"} == su_group_sn; # Get the selected items @_ = map $_ . "sn", grep /^subgroup\d+/ && defined $form->param($_ . "sn"), $form->param; # Regularize them $self->_trim(@_); # Merge the duplicates %_ = map { ($form->param($_))[0] => 1 } @_; $subform = new CGI(""); $subform->param("grp", $self->{"sn"}) if $self->{"iscur"}; foreach (keys %_) { $subform->param("member", $_); $checker = new Selima::Checker::GroupMem($subform); $error = $checker->check("member"); return $error if defined $error; } # OK return; } # _check_supgroup: Check the belonging groups sub _check_supgroup : method { local ($_, %_); my ($self, $form, $error, $subform, $checker); $self = $_[0]; $form = $self->{"form"}; # Skip for a non-super-user editing a super-user group return if $self->{"iscur"} && !is_su && $self->{"sn"} == su_group_sn; # Get the selected items @_ = map $_ . "sn", grep /^supgroup\d+/ && defined $form->param($_ . "sn"), $form->param; # Regularize them $self->_trim(@_); # Merge the duplicates %_ = map { ($form->param($_))[0] => 1 } @_; $subform = new CGI(""); $subform->param("member", $self->{"sn"}) if $self->{"iscur"}; foreach (keys %_) { $subform->param("grp", $_); $checker = new Selima::Checker::GroupMem($subform); $error = $checker->check("grp"); return $error if defined $error; } # OK return; } # _redir_selsubuser: Suspend and move to the subordinate user selection form sub _redir_selsubuser : method { local ($_, %_); my $self; $self = $_[0]; # Skip if not requested return if $self->_missing("selsubuser"); call_form FORM_USERS, undef, "import_selsubuser"; } # _redir_selsubgroup: Suspend and move to the subordinate group selection form sub _redir_selsubgroup : method { local ($_, %_); my $self; $self = $_[0]; # Skip if not requested return if $self->_missing("selsubgroup"); call_form FORM_GROUPS, undef, "import_selsubgroup"; } # _redir_selsupgroup: Suspend and move to the superordinate group selection form sub _redir_selsupgroup : method { local ($_, %_); my $self; $self = $_[0]; # Skip if not requested return if $self->_missing("selsupgroup"); call_form FORM_GROUPS, undef, "import_selsupgroup"; } return 1;