1 package LJ
::Event
::SecurityAttributeChanged
;
11 my ($class, $u, $opts) = @_;
12 croak
'Not an LJ::User' unless LJ
::isu
($u);
14 my $_get_logtime = sub {
21 die "Missing credentials" unless $ip && $action;
23 # $action == 1 -- deleted
24 my $extra = (1 == $action) ?
'new=D&old=V' : 'new=V&old=D';
26 # TODO: change this to use LJ::User::Userlog
27 my $dbr = LJ
::get_cluster_reader
($u);
28 my $sth = $dbr->prepare(
31 " WHERE userid=? AND extra=?".
32 " ORDER BY logtime DESC LIMIT 2");
33 $sth->execute($u->{userid
},$extra);
34 my ($logtime, $logip) = $sth->fetchrow_array;
37 die "This event (uid=$u->{userid}, extra=$extra) was not found in logs" unless $logtime;
39 my ($logtime2, $logip2) = $sth->fetchrow_array;
40 die "Second record about this event was found in log"
41 if $logtime2 && $logtime2 == $logtime && ($logip2 ne $logip);
43 die "The event (uid=$u->{userid}, extra=$extra, logtime=$logtime) was found in log,".
44 " but with wrong ip address ($logip, but not $ip)"
50 my $_get_rename_id = sub {
56 my $old_username = $opts->{old_username
};
57 my $userid = $u->{userid
};
59 # TODO: check is $u a user object?
60 die "Missing credentials" unless $ip && $action && $old_username;
62 my $infohistory = LJ
::User
::InfoHistory
->get( $u, 'username' );
65 sort { $a->timechange_unix <=> $b->timechange_unix }
68 die "This event (uid=$userid, what=username) was not found in logs"
69 unless $latest_record;
71 my $timechange = $latest_record->timechange_unix;
72 my $oldvalue = $latest_record->oldvalue;
74 die "Event (uid=$userid, what=username) was not found in logs".
75 " has wrong old username: $oldvalue instead of $old_username"
76 if $oldvalue ne $old_username;
82 'account_deleted' => [ 1, $_get_logtime ],
83 'account_activated' => [ 2, $_get_logtime ],
84 'account_renamed' => [ 3, $_get_rename_id ],
87 die 'Wrong action parameter' unless exists($actions{$opts->{action
}});
89 my $action = $actions{$opts->{action
}}[0];
91 $class->SUPER::new
($u,$action,$actions{$opts->{action
}}[1]->($u,$action,$opts));
94 sub is_common
{ 1 } # As seen in LJ/Event.pm, event fired without subscription
96 # Override this with a false value make subscriptions to this event not show up in normal UI
99 # Whether Inbox is always subscribed to
100 sub always_checked
{ 0 }
102 sub is_significant
{ 1 }
104 # override parent class subscriptions method to always return
105 # a subscription object for the user
107 my ($self, %args) = @_;
108 my $cid = delete $args{'cluster'}; # optional
109 my $limit = delete $args{'limit'}; # optional
110 croak
("Unknown options: " . join(', ', keys %args)) if %args;
111 croak
("Can't call in web context") if LJ
::is_web_context
();
116 if ($cid == $u->clusterid) {
117 my $row = { userid
=> $self->u->{userid
},
118 ntypeid
=> LJ
::NotificationMethod
::Email
->ntypeid, # Email
121 push @subs, LJ
::Subscription
->new_from_row($row);
125 push @subs, eval { $self->SUPER::subscriptions
(cluster
=> $cid,
139 return 'esn.security_attribute_changed.' . $ml_actions[$action-1] . '.';
144 my $lang = $u->prop('browselang');
145 return LJ
::Lang
::get_text
($lang, _arg1_to_mlkey
($self->arg1) . 'alert',
148 'user' => $u->ljuser_display()
152 sub as_email_subject
{
154 my $lang = $u->prop('browselang');
155 return LJ
::Lang
::get_text
($lang, _arg1_to_mlkey
($self->arg1) . 'email_subject',
163 my ($self, $u, $is_html) = @_;
165 my $lang = $u->prop('browselang');
166 my $action = $self->arg1;
167 my $logtime = $self->arg2;
169 my $_get_params_from_logtime = sub {
170 my ($u, $logtime) = @_;
172 # TODO: change this to use LJ::User::Userlog
173 my $userid = $u->{userid
};
174 my $dbr = LJ
::get_cluster_reader
($u);
175 my ($datetime, $remoteid, $ip, $uniq) = $dbr->selectrow_array(
176 "SELECT FROM_UNIXTIME(logtime), remoteid, ip, uniq".
178 " WHERE userid=$userid AND logtime=$logtime LIMIT 1");
179 return undef unless $remoteid;
181 datetime
=> $datetime,
182 remoteid
=> $remoteid,
189 my $_get_params_from_rename_id = sub {
190 my ($u, $timechange_stamp) = @_;
191 my $userid = $u->{userid
};
193 my $infohistory = LJ
::User
::InfoHistory
->get( $u, 'username' );
194 my ($infohistory_record) =
195 grep { $_->timechange_unix == $timechange_stamp }
198 unless ($infohistory_record) {
199 croak
"This event (uid=$userid, what=username) was not found in logs";
203 my $old_name = $infohistory_record->oldvalue;
204 my $other = $infohistory_record->other;
206 # Convert $timechange from GMT to local for user
208 LJ
::get_timezone
($u, \
$offset);
209 my $timechange = LJ
::TimeUtil
->mysql_time($timechange_stamp + 60*60*$offset, 0);
215 oldname
=> $old_name,
217 datetime
=> $timechange,
222 $_get_params_from_logtime,
223 $_get_params_from_logtime,
224 $_get_params_from_rename_id,
227 my %logparams = $actions[$action-1]($u, $logtime);
229 if (%logparams && $logparams{datetime
}) {
230 ($logparams{date
}, $logparams{time}) = split(/ /, $logparams{datetime
});
234 'user' => $u->{user
},
235 'username' => $u->{name
},
236 'sitename' => $LJ::SITENAME
,
237 'siteroot' => $LJ::SITEROOT
,
241 return LJ
::Lang
::get_text
($lang, _arg1_to_mlkey
($action) . 'email_text', undef, $vars);
244 sub as_email_string
{
247 return _as_email
($self, $u, 0);
253 return _as_email
($self, $u, 1);