« blib/lib/OpusVL/AppKit/View/SimpleXML.pm
↑ Summary
blib/lib/OpusVL/AppKit/Controller/AppKit/User.pm »
| 1 | package OpusVL::AppKit::FormFu::Constraint::AppKitUsername; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use base 'HTML::FormFu::Constraint'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 5 | ▶ | sub user_stashkey { my $self = shift; my ( $key ) = @_; $self->{user_stashkey} = $key; } | ||||||||||
|
Subroutine
user_stashkey: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 7 | |||||||||||||
| 8 | sub constrain_value | ||||||||||||
| 9 | { | ||||||||||||
| 10 | 6 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
constrain_value: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||
| 11 | 6 | my ( $value, $params) = @_; | |||||||||||
| 12 | 6 | my $c = $self->form->stash->{context}; | |||||||||||
| 13 | 6 | ▶ | my $stashkey = $self->{user_stashkey} || 'user'; | ||||||||||
|
Condition50%1/2
$self->{'user_stashkey'} || 'user'
MC/DC0%0/1
$self->{'user_stashkey'} || 'user'
$self->{'user_stashkey'}
Truth table50%1/2
A
A= $self->{'user_stashkey'}
| |||||||||||||
| 14 | 6 | ▶ | my $existing = $c->stash->{$stashkey}->id if ( exists $c->stash->{$stashkey} ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 15 | 6 | ▶ | my $matched = $c->model('AppKitAuthDB::User')->search( { username => $value, ( $existing ? ( id => { '!=' => $existing } ) : () ) } )->count; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 16 | 6 | $self->{message} = "Username already in use"; | |||||||||||
| 17 | 6 | ▶ | return ( $matched > 0 ) ? 0 : 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 18 | } | ||||||||||||
| 19 | |||||||||||||
| 20 | ## | ||||||||||||
| 21 | 2 | 1; | |||||||||||
| 22 | |||||||||||||
| 23 | __END__ | ||||||||||||
| 24 | |||||||||||||
| 25 | =pod | ||||||||||||
| 26 | |||||||||||||
| 27 | =encoding UTF-8 | ||||||||||||
| 28 | |||||||||||||
| 29 | =head1 NAME | ||||||||||||
| 30 | |||||||||||||
| 31 | OpusVL::AppKit::FormFu::Constraint::AppKitUsername | ||||||||||||
| 32 | |||||||||||||
| 33 | =head1 VERSION | ||||||||||||
| 34 | |||||||||||||
| 35 | version 2.22 | ||||||||||||
| 36 | |||||||||||||
| 37 | =head1 SYNOPSIS | ||||||||||||
| 38 | |||||||||||||
| 39 | - type: Text | ||||||||||||
| 40 | name: username | ||||||||||||
| 41 | constraints: | ||||||||||||
| 42 | - type: '+OpusVL::AppKit::FormFu::Constraint::AppKitUsername' | ||||||||||||
| 43 | user_stashkey: user_for_update | ||||||||||||
| 44 | |||||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||||
| 46 | |||||||||||||
| 47 | Ensures that duplicate user names aren't created. | ||||||||||||
| 48 | |||||||||||||
| 49 | To find existing user (the one we might be updating, therefore the username WILL exist) we check the | ||||||||||||
| 50 | context stash (Catalyst context) and look for stash key identified by 'user_stashkey'. | ||||||||||||
| 51 | |||||||||||||
| 52 | If the 'user' is in the stash, it will asume it to be a dbix object, pull its id and | ||||||||||||
| 53 | ignore that id when checking for existing usernames. | ||||||||||||
| 54 | |||||||||||||
| 55 | =head2 Adding a user. | ||||||||||||
| 56 | |||||||||||||
| 57 | Nothing required except and AppKitAuthDB model, which should be in every AppKit app. | ||||||||||||
| 58 | |||||||||||||
| 59 | =head2 Updating a user. | ||||||||||||
| 60 | |||||||||||||
| 61 | In the Catalyst stash there must be: | ||||||||||||
| 62 | 'user' - dbix object for the User. | ||||||||||||
| 63 | |||||||||||||
| 64 | =head1 NAME | ||||||||||||
| 65 | |||||||||||||
| 66 | OpusVL::AppKit::FormFu::Constraint::AppKitUsername - Username contraint for the AppKitAuthDB model. | ||||||||||||
| 67 | |||||||||||||
| 68 | =head1 METHODS | ||||||||||||
| 69 | |||||||||||||
| 70 | =head2 user_stashkey | ||||||||||||
| 71 | |||||||||||||
| 72 | Sets the key used to find an existing user in the context stash. | ||||||||||||
| 73 | |||||||||||||
| 74 | =head2 constrain_value | ||||||||||||
| 75 | |||||||||||||
| 76 | This method is used by formfu to hook into this constraints, constraining code. | ||||||||||||
| 77 | |||||||||||||
| 78 | Returns: | ||||||||||||
| 79 | boolean - 0 = did not validate, 1 = validated | ||||||||||||
| 80 | |||||||||||||
| 81 | =head1 AUTHOR | ||||||||||||
| 82 | |||||||||||||
| 83 | OpusVL - www.opusvl.com | ||||||||||||
| 84 | |||||||||||||
| 85 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 86 | |||||||||||||
| 87 | This software is copyright (c) 2010 by OpusVL - www.opusvl.com. | ||||||||||||
| 88 | |||||||||||||
| 89 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 90 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 91 | |||||||||||||
| 92 | =cut | ||||||||||||