| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::Plugin::User::Mixin::Model::User; | ||
| 5 | use Jifty::DBI::Schema; | ||
| 6 | |||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Jifty::Plugin::User::Mixin::Model::User - user model base mixin | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | package MyApp::Model::User; | ||
| 15 | use Jifty::DBI::Schema; | ||
| 16 | use MyApp::Record schema { | ||
| 17 | # column definitions | ||
| 18 | }; | ||
| 19 | |||
| 20 | # Import columns: name, email and email_confirmed | ||
| 21 | use Jifty::Plugin::User::Mixin::Model::User; | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This mixin may be added to a model to give your user accounts a name and an email address. This module may be used as the basic building block for building account models in your application. It can be combined with mixins from an authentication plugin to create an object suitable for a given authentication mechanism. | ||
| 26 | |||
| 27 | =head1 SCHEMA | ||
| 28 | |||
| 29 | This mixin model adds the following columns to the model. | ||
| 30 | |||
| 31 | =head2 name | ||
| 32 | |||
| 33 | This is the username/nickname for the user of the account. | ||
| 34 | |||
| 35 | =head2 email | ||
| 36 | |||
| 37 | This is the email address of the account. It is intended as a bare minimum confirmation of identity and for communication of password resets and other account information. | ||
| 38 | |||
| 39 | =head2 email_confirmed | ||
| 40 | |||
| 41 | This is a flag indicating whether the user has confirmed ownership of the given email address. | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | use base 'Jifty::DBI::Record::Plugin'; | ||
| 46 | use Jifty::Plugin::User::Record schema { | ||
| 47 | column | ||
| 48 | name => type is 'text', | ||
| 49 | label is _('Nickname'), | ||
| 50 | hints is _('How should I display your name to other users?'); | ||
| 51 | column | ||
| 52 | email => type is 'text', | ||
| 53 | label is _('Email address'), default is '', is immutable, is distinct; | ||
| 54 | column | ||
| 55 | email_confirmed => label is _('Email address confirmed?'), | ||
| 56 | render as 'Unrendered', | ||
| 57 | type is 'boolean'; | ||
| 58 | |||
| 59 | }; | ||
| 60 | |||
| 61 | =head1 METHODS | ||
| 62 | |||
| 63 | =head2 set_email ADDRESS | ||
| 64 | |||
| 65 | Whenever a user's email is set to a new value, we need to make | ||
| 66 | sure they reconfirm it. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | { | ||
| 71 | no warnings 'redefine'; | ||
| 72 | |||
| 73 | sub set_email { | ||
| 74 | my $self = shift; | ||
| 75 | my $new_address = shift; | ||
| 76 | my $email = $self->__value('email'); | ||
| 77 | |||
| 78 | my @ret = $self->_set( column => 'email', value => $new_address); | ||
| 79 | |||
| 80 | unless ( $email eq $self->__value('email') ) { | ||
| 81 | $self->__set( column => 'email_confirmed', value => '0' ); | ||
| 82 | Jifty->app_class('Notification','ConfirmEmail')->new( to => $self )->send; | ||
| 83 | } | ||
| 84 | |||
| 85 | return (@ret); | ||
| 86 | } | ||
| 87 | |||
| 88 | } | ||
| 89 | |||
| 90 | =head2 validate_email | ||
| 91 | |||
| 92 | Makes sure that the email address looks like an email address and is | ||
| 93 | not taken. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub validate_email { | ||
| 98 | my $self = shift; | ||
| 99 | my $new_email = shift; | ||
| 100 | |||
| 101 | return ( 0, _("That %1 doesn't look like an email address.", $new_email) ) | ||
| 102 | if $new_email !~ /\S\@\S/; | ||
| 103 | |||
| 104 | my $temp_user = Jifty->app_class('Model','User')->new( current_user => Jifty->app_class('CurrentUser')->superuser ); | ||
| 105 | $temp_user->load_by_cols( 'email' => $new_email ); | ||
| 106 | |||
| 107 | # It's ok if *we* have the address we're looking for | ||
| 108 | return ( 0, _('It looks like somebody else is using that address. Is there a chance you have another account?') ) | ||
| 109 | if $temp_user->id && ( !$self->id || $temp_user->id != $self->id ); | ||
| 110 | |||
| 111 | return 1; | ||
| 112 | } | ||
| 113 | |||
| 114 | =head1 SEE ALSO | ||
| 115 | |||
| 116 | L<Jifty::Plugin::Authentication::Password>, L<Jifty::Plugin::Authentication::Password::Mixin::Model::User> | ||
| 117 | |||
| 118 | =head1 LICENSE | ||
| 119 | |||
| 120 | Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. | ||
| 121 | Jifty is distributed under the same terms as Perl itself. | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | 1; | ||
| 126 |