# Selima Website Content Management System # UserPref.pm: The user preference 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-14 package Selima::Checker::UserPref; use 5.008; use strict; use warnings; use base qw(Selima::Checker); use Selima::CallForm; use Selima::DataVars qw($DBH :forms); use Selima::ShortCut; # new: Initialize the checker sub new : method { local ($_, %_); my $class; ($class, @_) = @_; $_[1] = "userpref" 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 $_{"usr"} && exists $_{"domain"} && exists $_{"name"}) { $error = $self->__check_dup(); return $error if defined $error; } return; } # _check_usr: Check the user sub _check_usr : method { local ($_, %_); my ($self, $form, $error); $self = $_[0]; $form = $self->{"form"}; # "everyone not set" has a different form context return {"msg"=>N_("Please select the user.")} if $self->_missing("everyone"); # Regularize it $self->_trim("everyone"); # Check the option value return {"msg"=>N_("This option is invalid. Please select a proper user.")} unless $form->param("everyone") =~ /^(?:true|false)$/; # Check the user if not everyone if ($form->param("everyone") eq "false") { $error = $self->SUPER::_check_usr; return $error if defined $error; } # OK return; } # _check_domain: Check the domain sub _check_domain : method { local ($_, %_); my ($self, $form, $error); $self = $_[0]; $form = $self->{"form"}; # "everywhere not set" has a different form context return {"msg"=>N_("Please set the preference domain.")} if $self->_missing("everywhere"); # Regularize it $self->_trim("everywhere"); # Check the option value return {"msg"=>N_("This option is invalid. Please set a proper preference domain.")} unless $form->param("everywhere") =~ /^(?:true|false)$/; # Check the domain if not everywhere if ($form->param("everywhere") eq "false") { # Check if it exists $error = $self->_missing("domain"); return $error if defined $error; # Regularize it $self->_trim("domain"); # Check if it is filled return {"msg"=>N_("Please fill in the preference domain.")} if $form->param("domain") eq ""; # Check the length return {"msg"=>N_("This preference domain is too long. (Max. length [#,_1])"), "margs"=>[${$self->{"maxlens"}}{"domain"}]} if length $form->param("domain") > ${$self->{"maxlens"}}{"domain"}; } # OK return; } # _check_name: Check the preference name sub _check_name : method { local ($_, %_); my ($self, $form, $error); $self = $_[0]; $form = $self->{"form"}; # Check if it exists $error = $self->_missing("name"); return $error if defined $error; # Regularize it $self->_trim("name"); # Check if it is filled return {"msg"=>N_("Please fill in the preference name.")} if $form->param("name") eq ""; # Check the length return {"msg"=>N_("This preference name is too long. (Max. length [#,_1])"), "margs"=>[${$self->{"maxlens"}}{"name"}]} if length $form->param("name") > ${$self->{"maxlens"}}{"name"}; # OK return; } # _check_value: Check the preference value sub _check_value : method { local ($_, %_); my ($self, $form, $error); $self = $_[0]; $form = $self->{"form"}; # Check if it exists $error = $self->_missing("value"); return $error if defined $error; # Regularize it $self->_trim("value"); # Check if it is filled return {"msg"=>N_("Please fill in the preference value.")} if $form->param("value") eq ""; # Check the length return {"msg"=>N_("This preference value is too long. (Max. length [#,_1])"), "margs"=>[${$self->{"maxlens"}}{"value"}]} if length $form->param("value") > ${$self->{"maxlens"}}{"value"}; # 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(); if ($form->param("everyone") eq "true") { push @_, "usr IS NULL"; } else { push @_, "usr=" . $form->param("usr"); } if ($form->param("everywhere") eq "true") { push @_, "domain IS NULL"; } else { push @_, "domain=" . $DBH->quote($form->param("domain")); } push @_, "name=" . $DBH->quote($form->param("name")); 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 user preference already exists. You cannot create a duplicated one.")} if $sth->rows > 0; return; } # _redir_selusr: Suspend and move to the user selection form sub _redir_selusr : method { local ($_, %_); my $self; $self = $_[0]; # Skip if not requested return if $self->_missing("selusr"); call_form FORM_USERS, undef, "import_selusr"; } # _redir_delusr: Remove the user sub _redir_delusr : method { local ($_, %_); my $self; $self = $_[0]; # Skip if not requested return if $self->_missing("delusr"); $self->{"form"}->delete("usr"); success_redirect undef; } return 1;