Add tracking of certain important user actions. These events include: * Logins * Logouts * Failed login attempts * Account creations * Account deletions * Password reset requests * Password changes * Email address changes This is similar to the mod actions log, except for account activity related to a single user. The information tracked includes the user, the event type (login, logout, etc), the timestamp, the user's IP address, IP geolocation information, the user's browser user agent, and the user's session ID from their session cookie. This information is visible to mods only. This is done with three models. The UserEvent model tracks the event type (login, logout, password change, etc) and the user. The UserEvent is tied to a UserSession, which contains the user's IP address and browser metadata. Finally, the IpGeolocation model contains the geolocation information for IPs, including the city, country, ISP, and whether the IP is a proxy. This tracking will be used for a few purposes: * Letting users view their account history, to detect things like logins from unrecognized IPs, failed logins attempts, password changes, etc. * Rate limiting failed login attempts. * Detecting sockpuppet accounts using their login history. * Detecting unauthorized account sharing.
70 lines
2.1 KiB
Ruby
70 lines
2.1 KiB
Ruby
class EmailsController < ApplicationController
|
|
respond_to :html, :xml, :json
|
|
|
|
def index
|
|
@email_addresses = authorize EmailAddress.visible(CurrentUser.user).paginated_search(params, count_pages: true)
|
|
@email_addresses = @email_addresses.includes(:user)
|
|
respond_with(@email_addresses)
|
|
end
|
|
|
|
def show
|
|
if params[:user_id]
|
|
@email_address = authorize EmailAddress.find_by_user_id!(params[:user_id])
|
|
else
|
|
@email_address = authorize EmailAddress.find(params[:id])
|
|
end
|
|
|
|
respond_with(@email_address)
|
|
end
|
|
|
|
def edit
|
|
@user = authorize User.find(params[:user_id]), policy_class: EmailAddressPolicy
|
|
respond_with(@user)
|
|
end
|
|
|
|
def update
|
|
@user = authorize User.find(params[:user_id]), policy_class: EmailAddressPolicy
|
|
|
|
if @user.authenticate_password(params[:user][:password])
|
|
UserEvent.build_from_request(@user, :email_change, request)
|
|
@user.update(email_address_attributes: { address: params[:user][:email] })
|
|
else
|
|
@user.errors.add(:base, "Password was incorrect")
|
|
end
|
|
|
|
if @user.errors.none?
|
|
flash[:notice] = "Email updated. Check your email to confirm your new address"
|
|
UserMailer.email_change_confirmation(@user).deliver_later
|
|
respond_with(@user, location: settings_url)
|
|
else
|
|
flash[:notice] = @user.errors.full_messages.join("; ")
|
|
respond_with(@user)
|
|
end
|
|
end
|
|
|
|
def verify
|
|
@user = User.find(params[:user_id])
|
|
@email_address = @user.email_address
|
|
|
|
if @email_address.blank?
|
|
redirect_to edit_user_email_path(@user)
|
|
elsif params[:email_verification_key].present?
|
|
authorize @email_address
|
|
@email_address.verify!
|
|
flash[:notice] = "Email address verified"
|
|
redirect_to @email_address.user
|
|
else
|
|
authorize @email_address
|
|
respond_with(@user)
|
|
end
|
|
end
|
|
|
|
def send_confirmation
|
|
@user = authorize User.find(params[:user_id]), policy_class: EmailAddressPolicy
|
|
UserMailer.welcome_user(@user).deliver_later
|
|
|
|
flash[:notice] = "Confirmation email sent to #{@user.email_address.address}. Check your email to confirm your address"
|
|
redirect_to @user
|
|
end
|
|
end
|