So I am setting up a welcome message when a user signs up the website - previously I had set it up using gmail (http://stackoverflow.com/questions/5793296/rails-actionmailer-w-devise-google-apps-in-development-mode), but it's going to be using google apps - so if I'm correct another stackoverflow user claimed the set up is similar so that's not a problem. But since I only want a welcome email, I was thinking can I just use the confirmable set up so they get an email, and then in the config set it so that the user doesn't have to confirm till after say 1000 years or something large so basically it's not really a confirmation email? (If there is a better way to do this I'd appreciate such input)
you don't need to twist the Confirmable feature to achieve this, you can do it more elegantly with an ActiveRecord::Observer. Basically when you register/save a user the observer will get notified and from there you can call a mailer. You can see an example below.
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "something#something.com"
def welcome_mail(email)
mail(:to => email, :subject => "Welcome to Something").deliver
end
end
app/models/user_observer.rb
class UserObserver < ActiveRecord::Observer
# We check if it's a new user
def before_save(user)
#is_new_record = user.new_record?
true
end
def after_save(user)
# If it's not a new user we don't want to send them another welcome email
if #is_new_record then
UserMailer.welcome_mail(user.email)
end
end
end
Finally you need to configure rails to register the observer.
config/application.rb (just a extract)
config.active_record.observers = :user_observer
it's probably awfully late to answer, but i think there's after_create callback to shrink the solution above since you don't need to check if it's a new record!
Related
This question is very similar to Rails Devise: after_confirmation except I'm looking for more specifically a reconfirmation hook
My use case : I need to synchronise a new user email on some third party service (Intercom to be precise).
I have a first implementation using an API where I have put the logic I need there (all tidied up in a service)
However, I often end up doing a lot of maintenance using the console, and the most obvious things that come to mind is to perform
user.email = 'newemail#example.com'
user.confirm # or skip_reconfirmation
user.save
Using this, I do not fire my resynchronisation logic automatically. Is there a way to force some reconfirmation callback ? overriding after_confirmation does not seem to work
Not really an answer, but in the meantime I have monkeypatched my user class with the following (where xxx represents some custome methods added on the user class to sync to third party services the new email)
class User
include User::Console if defined?('Rails::Console')
end
module User::Console
extend ActiveSupport::Concern
included do
## Override Devise confirmable to add warnings
def confirmation_warning
puts "WARNING !!".red
puts "calling `#confirm` or `#skip_reconfirmation!` on the user does not sync to xxx !\n"\
'Please either use [Name of custom confirmation service], '\
'or use `user.sync_to_xxx` to propagate changes'.red
end
def confirm
confirmation_warning
super
end
def skip_reconfirmation!
confirmation_warning
super
end
end
end
I have the exact same use case, where I need to sync a new user email on a third party service.
The way I solved it is a before_update filter on the User model, and using email_changed?:
class User < ApplicationRecord
before_update :update_email_in_third_party_service
private
def update_email_in_third_party_service
return unless self.valid? && self.email_changed?
# We passed our check, update email in the third party service (preferably in a background job)
end
end
Actually I am using devise for login and registration and its working fine, it send a confirmation email which is really good. But I need to send confirmation email in specific cases. I am passing user type in URL and on behalf of that I want to set mail. I have two type of users, one will be confirm their account their self reset of the users can not confirm their account only admin can approve their accounts. I have override the create method
def create
super
if params[:type]=='xyz
#user.skip_confirmation_notification!
end
end
but it sends mail in both cases. Please tell where am wrong.
So according to devise confirmable module you can skip to send confirmation and email by following code.
def confirm_your_type_user_without_confirmation_email
# check your condition here and process the following
self.skip_confirmation!
self.confirm!
# condition may end here
end
Now lets call it on create hook.
class User < ActiveRecord::Base
after_create :confirm_your_type_user_without_confirmation_email
....
end
for more reference you may check this:
Devise Confirmable module
The solution should be something similar as I mentioned here above. And its best practice to avoid controller to handle these responsibilities, because its not something your controller should take. :)
I hope my answer will give you some way to solve your problems! Thanks!
I worked through some basic tutorials on Rails 3. The goal is a community-website on abilities and activities. I am using Devise for authentication. The creation of user profiles with avatars worked well (thanks to paperclip).
As a next step, I want to enable registered users to send an e-mail to a user from his (or her) profile page. I found a great tutorial on creating a contact form using Google Apps:
http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/
The mailer class in this tutorial looks like:
class NotificationsMailer < ActionMailer::Base
default :from => "noreply#youdomain.dev"
default :to => "you#youremail.dev"
def new_message(message)
#message = message
mail(:subject => "[YourWebsite.tld] #{message.subject}")
end
end
My question: What is the best way to replace you#youremail.dev with the receivers E-Mail-Address? (from the User-Model)
Thanks in advance!
You can modify the new_message to accept the user (or list of users) to whom you want to send the email. Or an array of email addresses if you want to. Then pass the receiver's email address to the mail method as the :to option.
def new_message(receiver, message)
#message = message
mail(:subject => "[YourWebsite.tld] #{message.subject}",
:to => receiver.email_address) # or something similar
end
Then you can invoke your mailer like this
NotificationEmail.new_message(a_user, a_message).deliver
To read the API see here or here (I prefer APIdock).
Also a more comprehensive guide on ActionMailer is available here. If you are new to Rails, you can find more guides here.
I am using Devise for authentication in my application.
How do I forbid certain users from signing in - kind of disable a user?
Do it like this:
Create a column called is_active for the User model.
Then add the code below to the User model:
class User < ActiveRecord::Base
#this method is called by devise to check for "active" state of the model
def active_for_authentication?
#remember to call the super
#then put our own check to determine "active" state using
#our own "is_active" column
super and self.is_active?
end
end
UPDATE
As Matt Huggins notes, the method is now called active_for_authentication? (Documentation)
Add a column to the User model: allowed_to_log_in.
Then add this to /app/models/user.rb:
def active_for_authentication?
super and self.allowed_to_log_in?
end
If you want to inform the user with a custom message you can add this as well:
def inactive_message
"You are not allowed to log in."
end
I think that is quite important because the standard message from Devise says:
"Your account is not activated yet."
That is confusing for users and the real reason is that you have "banned" them from logging in.
You want to do authorization, not authentication. Devise only does authetication, though.
I.e. devise only tells you that a user is who he says he is.
You need something else to forbid him from using the site.
Authorization is a popular topic and there's a whole list of gems that can help you with it:
http://ruby-toolbox.com/categories/rails_authorization.html
Take your pick.
Sounds like you may be interested in cancan
https://github.com/ryanb/cancan
http://railscasts.com/episodes/192-authorization-with-cancan
I would like to set a boolean flag upon user confirmation via Devise. Essentially I want to set the user as 'active'. However, Devise simply confirms the account and logs them in.
How can I create a callback of some sorts to update my user record to set that 'active' column to true upon confirmation?
Any help very appreciated!
Presuming that your authentication model is called User, you can do this:
class User < ActiveRecord::Base
def active?
super and (not self.confirmed_at.nil?)
end
end
With this, Devise will not login the user but will wait until the user confirms (the confirmed_at field will be non-NULL if a user has confirmed)
For your particular question, you're better off implementing your active? attribute as confirmed_at being nil, as suggested by Zabba.
But here is how to do what you're asking, since it may be helpful to people trying to set other values on the user after confirmation.
class Users::ConfirmationsController < Devise::ConfirmationsController
def show
# let Devise actually confirm the user
super
# if they're confirmed, it will also log them in
if current_user then
# and you can do whatever you want with their record
current_user.active = true
end
end
end
This is basically a comment on Turadg's Answer below. If you follow that suggestion (which I did) you will have a small problem when users attempt to use an invalid confirmation_token. You will get a "Missing template users/confirmations/new". What the Devise::ConfirmationsController is doing here is sending you to devise/confirmations/new to inform you the token is invalid and allow you to send another one.
Since I had already customized the Devise views, what I ended up doing to get around this minor issue is moving the devise/confirmations/new.html.haml file into the now expected location under user/confirmations/new.html.haml.