Implementing Single Sign On for Disqus in Rails - ruby-on-rails

So I am currently using Disqus for my commenting system on my website It works perfectly but the downfall is that users need to have two accounts. One to access the site and another to comment!
Disqus offers SSO for this exact reason to allow a user to sign up only once (on the site) and automatically be given an 'in-app' disqus account to comment.
The people at Disqus have activated SSO for me and have linked me to various documentation. I was wondering if there are any good tutorials/documentation to show you how to do this with rails?

disqus_rails gem - Disqus service RoR wrapper
acts_as_disquser and Single Sign On
Disqus provides SSO service which gives ability to link your local users info to Disqus users, read more in Disqus tutorial. To do this, as and for linking model to Disqus thread - you have to add 'acts_as_disquser' line in your users model. You need pass there four attributes: 'id', 'username', 'email' and 'avatar'(avatar is an optional field, so you can omit this). Here is example:
class User < ActiveRecord::Base
acts_as_disquser :username => :full_name, :email => :email, :avatar => Proc.new{ avatar.url }
...
end
As you see, you can pass there or symbols, or procs. First will try to get instance variable with such name from model's instance, second will evaluate code inside Proc with context of model's instance. Important - only Proc are available for second way of defining attribute, no lambdas. Also, you may not implicitly pass acts_as_disquser :id => :id - it will try to get id automatically if it is not defined. Next, you need to specify in disqus_init helper attributes 'disquser' with current user instance, and 'sso' as boolean to enable or disable SSO.
<%= disqus_init :disquser => current_user, :sso => true %>
After this is done, when users will post comments via Disqus, their username, email and avatar will be taken from your site.
https://github.com/sandric/disqus_rails

Related

ActiveAdmin details for Contact goes to Page with similar name

I realize that this issue is likely in my app code, but I'm stumped as to where to look at this point.
I've successfully installed ActiveAdmin 1.0.0.pre4 on rails 4.2.1 (following instructions these instructions. I turned off authentication, since we already have devise set up with a custom admin system and I will configure ActiveAdmin to use it once we know it's working for us. I also configured the default_namespace to be activeadmin (not admin) since we already have custom administration tools under /admin.
/activeadmin successfully shows the Dashboard. So I added two models (Contact and Flag). Flags (/activeadmin/flags) works as expected - it shows all the records and Id, View, and Edit all link to the appropriate details view.
For Contacts, the list view works as expected (/activeadmin/contacts). It shows the records and the filters seem to work.
The problem is if I click on the Id, View or Edit links for any Contact they all show the same content: the details view of one of my Page records (which I had not yet even configured for ActiveAdmin). They show the SAME Page details - it has the title "Contact Us" (and the slug "contact-us"). Everything in the ActiveAdmin UI looks as it should: the URL is /activeadmin/contacts/39, the breadcrumb is right, even the panel title says "Contact Details". But regardless of which contact I choose, it always shows the Contact Us page.
I can short-circuit it by editing the contact.rb file for ActiveAdmin. However, I can only get it to show the "title" (from the Page Contact Us), not any other field. Everything else (whether it's from the Page model or the Contact model) throws an exception: undefined local variable or method `body' for #<ActiveAdmin::Views::Pages::Show:0x007fdfff3e7c40> Note that it's clearly looking for Pages, not Contacts. The request parameter is: {"controller"=>"activeadmin/contacts", "action"=>"show", "id"=>"39"}
Previously I had thought it had to do with the slug, although I have removed the friendly_id gem and commented out the code that references it (and errors imply it's no longer working). It made no difference.
Also:
- /activeadmin/contacts/ all show the Contact Us page details
- /activeadmin/contact-us or contact (with or without a /) gives a routing error (No route matches [GET] "/activeadmin/...")
I tried adding my Page model and then customizing page.rb with
ActiveAdmin.register Page, :as => "fancy_page"
(as I'd read in issue 959). But this did not change the behavior. Fancy Pages does indeed list all my pages, and Id, View, and Edit all link to the correct details page.
Rewriting my entire application to rename the Page model would be extremely difficult (the front-end website and associated custom CMS is the one part of our app that is fully working and live). I'm hoping there is a simpler fix, but I'm not really sure where to look.
Any suggestions?
Thanks!
julie
You need to customize the 'Form' configuration of contact's activeadmin resource by such configuration:
form do |f|
f.inputs 'Details' do
f.translated_inputs 'ignored title', switch_locale: false do |t|
t.input :name, :hint => 'Contact name'
t.input :email, :hint => 'Contact email'
end
end
actions
end
Note that you have to change name & email based on your model fields.
And to do more customization on the view (show action), you can do it by:
show do |contact|
attributes_table do
row :id
row :name
row :email
end
end

application with multiple "accounts", each containing multiple users

I am trying to build an application with multiple "accounts", each containing multiple users.
I've started with the gem Devise to create user authentication, and I've created Accounts and Preferences MVC's with scaffolding.
The Account model has_one Preferences, and has_many Users
The User model and Preferences model belongs_to my Account model
I've tested these relationships in the console and they seem to work..
What I would like to do, is make sure that when users are logged in, they can only view records associated with their account. From what I've seen, the current_user helper can be used, but I would like to use something like a "current_account".
1. First question is, is there a resource, Gem, method that can help me to create this current_account variable?
2. Second question would be, what do I need to put in my View for this to work? For instance, if a user wants to update his Preferences associated with his account, how would we call this in the _form partial, would if be something like this?
<%= #account.preference.opt_in %>
and if they wanted to see all of the users in the account, would it be something like this?
<% #customer.user.each do |account| %>
<%= user.email %>
<% end %>
Please let me know if I'm overlooking anything major, I've been working on this the last few days.
If I understand correctly, you are looking for a multi-tenancy solution. If so, you don't need to come up with your own solution, as there are well established patterns out there.
You can try the Milia gem, which fits in nicely with devise.

Passing additional params to Devise new_user_session_path

I am trying to implement Devise and Ominauth for multiple providers. One scenario is: user sign in with Twitter for the first time without creating an account first.
To handle this, I redirect user from callback link (/auth/twitter/callback) to sign up page to fill in email address.
However, I want to bring authentication information from the callback link {:provider => "twitter", :uid =>"123"} to sign up page (new_registration_path). Then when user submit sign up form, the authentication will be captured together with account information.
How should I go about doing this? I've tried
redirect_to(new_user_session_path, {:service => service})
where service = {:provider => "twitter", :uid =>"123"}
but service doesn't get passed through as a params.
What did I do wrong? Do I need to modify source code for Devise?
Thank you.
You're probabbly looking for this:
redirect_to(new_user_session_path(:argument => "value"))
And in controller after redirect (probably UserSessions#new) you can access argument with:
params[:argument]
You need to pass arguments to path helper not to redirect_to method.
In your example it should be:
redirect_to(new_user_session_path(service))
And in controller after redirect (probably UserSessions#new) you can access argument with:
params[:provider]
params[:uid]
or
redirect_to(new_user_session_path(:service => service))
and
params[:service][:provider]
params[:service][:uid]

How can I create a dynamic login button in Ruby on Rails?

Anyone know how I can make a login button in Ruby on Rails for an application?
I assume the redirection will be something like:(I'm using current elements)
<%= link_to 'Log In', index_path, :class => 'btn btn-primary'%>
However, this will just create a button that redirects to the index, regardless if the "user" has filled in the required "Email" and "Password" fields. I assume I'll need a condition to check if the fields are filled and I assume a database table for accepted e-mails with the correct passwords?
There are actually several ways to handle authentication and user roles, I would suggest searching around for Devise to start with. after you understand how they are managed you might try yourself using DB relations or session hashes.
Have fun learning:
https://github.com/plataformatec/devise

Rails accepting terms before creation

Let's say before a user is allowed to view the creation page of a Model, they are required to accept some terms. The workflow would be like:
User clicks on "create MODEL" -> Brought to a terms page, must accept before moving on -> MODEL creation page
If a user copies the url to the creation page, they should be redirected to the terms page.
What's the best way of going about this? I was thinking of using the session variable somehow...but I can't think of a clever enough idea. Any suggestions?
You can have the following routes:
get 'terms' => 'MODEL#terms'
get 'new' => 'MODEL#terms'
post 'new' => 'MODEL#new'
The "create MODEL" should send a GET request for /terms. Accepting the terms should POST to /new. If the user pasted /new in the URL, he'll be directed to terms instead.
Alternatively (or additionally), you can have the /terms POST a variable, :terms_accepted => true to the MODEL creation page, and on that page, check if :terms_accepted == true. If not, redirect to the terms page.
You can add the acceptance of the terms to the model itself, then you don't need a construction with an extra page and redirect if someone enters in the wrong place. Instead the user can only submit the form for creation when he/she accepts the terms.
You can add the following to the model:
class Model < ActiveRecord::Base
validates :terms_of_service, :acceptance => true
end
And then make sure you have the checkbox for this in the new %{model} form.
Also see: http://guides.rubyonrails.org/active_record_validations_callbacks.html#acceptance and Accept terms of use rails

Resources