Skip to content

Commit

Permalink
Item13322: Add support for an "onSave" checker
Browse files Browse the repository at this point in the history
This extends the Spec definition with "ONSAVE".  If set, the Save wizard
will call <checker>->onSave( $reporter, $key, $value ) for the key
before the value is processed for save.

The handler can just do cleanup work, such as in this case, removing the
language.cache file, so that the cache is refreshed. It can also modify
the value.  It should not change any other configuration values.

Modification is intended for custom encoding, such as hashing a
password.
  • Loading branch information
gac410 committed Mar 25, 2015
1 parent 8cea25e commit 6290beb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
20 changes: 17 additions & 3 deletions core/lib/Foswiki/Configure/Checkers/LANGUAGE.pm
Expand Up @@ -84,11 +84,25 @@ sub check_current_value {
}
}

sub refresh_cache {
my ( $this, $string, $reporter ) = @_;
=begin TML
---++ ObjectMethod onSave()
This routine is called during the Save wizard, for any key that being
saved, regardless of whether or not it has actually changed. This
is enabled by including the ONSAVE key in the Spec. (In this case it is
automatically enabled by the =Pluggable/LANGUAGES.pm=
This will remove the languages.cache file from the WorkingDir, so that
the cache can be regenerated.
=cut

sub onSave {
my ( $this, $reporter, $key, $val ) = @_;

if ( $Foswiki::cfg{UserInterfaceInternationalisation} ) {
my $dir = $Foswiki::cfg{LocalesDir};
my $dir = $Foswiki::cfg{WorkingDir};

if ( -f "$dir/languages.cache" ) {
if ( unlink("$dir/languages.cache") ) {
Expand Down
1 change: 1 addition & 0 deletions core/lib/Foswiki/Configure/Pluggables/LANGUAGES.pm
Expand Up @@ -73,6 +73,7 @@ sub construct {
CHECKER => 'LANGUAGE',
DISPLAY_IF => "{UserInterfaceInternationalisation}",
opts => 'CHECK="undefok emptyok"',
ONSAVE => 1,

);
$langs{$label} = $value;
Expand Down
1 change: 1 addition & 0 deletions core/lib/Foswiki/Configure/Value.pm
Expand Up @@ -77,6 +77,7 @@ use constant ATTRSPEC => {
MULTIPLE => {}, # Allow multiple select
SPELLCHECK => {},
LABEL => {},
ONSAVE => {}, # Call Checker->onSave() when set.

# Rename single character options (legacy)
H => 'HIDDEN',
Expand Down
14 changes: 14 additions & 0 deletions core/lib/Foswiki/Configure/Wizards/Save.pm
Expand Up @@ -20,6 +20,7 @@ use Fcntl;
use File::Spec ();
use Foswiki::Configure::Load ();
use Foswiki::Configure::LoadSpec ();
use Foswiki::Configure::Checker ();
use Foswiki::Configure::FileUtil ();

use Foswiki::Configure::Wizard ();
Expand Down Expand Up @@ -230,6 +231,19 @@ sub save {
if ( $this->param('set') ) {
while ( my ( $k, $v ) = each %{ $this->param('set') } ) {
my $spec = $root->getValueObject($k);

# If ONSAVE checking is specified, call the checker's onSave()
# routine. This could provide "cleanup" processing for example,
# when a configuration value is saved. It can modify the value
# being saved, such as to hash a password, but never modify other
# values.
if ( $spec && $spec->{ONSAVE} ) {
my $checker = Foswiki::Configure::Checker::loadChecker($spec);
if ( $checker && $checker->can('onSave') ) {
$checker->onSave( $reporter, $k, $v );
}
}

if ( defined $v ) {
$v =~ m/^(.*)$/s;
$v = $1; # untaint
Expand Down

0 comments on commit 6290beb

Please sign in to comment.