# Copyright (c) 1999-2008 bivio Software, Inc. All rights reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: Email.pm,v 2.8 2011/10/04 20:13:48 moeller Exp $ package Bivio::Biz::Model::Email; use strict; use Bivio::Base 'Model.LocationBase'; our($VERSION) = sprintf('%d.%02d', q$Revision: 2.8 $ =~ /\d+/g); my($_E) = b_use('Type.Email'); sub create { my($self, $values) = (shift, shift); $values->{want_bulletin} = 1 unless defined($values->{want_bulletin}); return $self->SUPER::create($self->internal_prepare_query($values), @_); } sub internal_initialize { return { version => 2, table_name => 'email_t', columns => { realm_id => ['RealmOwner.realm_id', 'PRIMARY_KEY'], location => ['Location', 'PRIMARY_KEY'], email => ['Email', 'NOT_NULL_UNIQUE'], want_bulletin => ['Boolean', 'NOT_NULL'], }, auth_id => 'realm_id', other => [ [qw(realm_id RealmOwner.realm_id)], ], }; } sub internal_prepare_query { my($self, $values) = (shift, shift); $values->{email} = lc($values->{email}) if $values->{email}; return $self->SUPER::internal_prepare_query($values, @_); } sub invalidate { my($self) = @_; my($address) = $self->get('email'); my($prefix) = $self->get_field_type('email')->INVALID_PREFIX; return if $address =~ /^\Q$prefix/o; my($other) = $self->new_other('Email'); my($i) = 0; $i++ while $other->unauth_load({email => $prefix . $i . $address}); $self->update({ email => $prefix . $i . $address, want_bulletin => 0, }); return; } sub is_ignore { my($proto, $model, $model_prefix) = shift->internal_get_target(@_); return $_E->is_ignore($model->get($model_prefix.'email')); } sub unsafe_user_id_from_email { my($self, $email) = @_; # guard against duplicate email, use user with role, or oldest id my($user_ids) = $self->map_iterate( 'realm_id', 'unauth_iterate_start', 'realm_id ASC', { email => $email, }); return undef unless @$user_ids; if (@$user_ids > 1) { foreach my $user_id (@$user_ids) { return $user_id if @{$self->new_other('RealmUser')->map_iterate('role', { user_id => $user_id, })}; } } return $user_ids->[0]; } sub update { my($self, $values) = (shift, shift); return $self->SUPER::update($self->internal_prepare_query($values), @_); } 1;