Question

I am using Authlogic and net-ldap to authenticate users to Active Directory in my new Rails 4 app. Works nicely...

However, I would like to import all users from another database so they don't have to create their accounts from scratch (and, because each user must select another user as his supervisor to complete his profile - and it turns into a chicken-egg kind of problem if I start with an empty users table). There are required fields in my new users table that are not in the source user table, so I want to force users to complete their profiles upon initial login, before they can get into the app.

Is there a way I can maybe do a before_create user_session validation on login_count is null or something like that? Is there a better way to handle this sort of thing?

Any suggestions are appreciated. Thank you.

Was it helpful?

Solution

I would add a field to users table called completed_profile of boolean type:

rails g add_completed_profile_to_users completed_profile:boolean

then create a filter method in application_controller.rb

  def complete_profile
    if current_user.completed_profile?
      redirect_to the_path_after_log_in
    else
      redirect_to edit_profile_path, error: "Please update your profile."
    end
  end

*field_one and field_two are the required fields to fill in before proceeding to app.*

In other controller of your app:

before_filter :complete_profile

this filter should not be applied to controller and action that responds for rendering the edit profile page or new account page, if edit_profile_path = users#edit means in users controller your filter will look like:

before_filter :complete_profile, except: ['edit', 'update', 'new', 'create']

variant 2, without migration:

create a filter method in application_controller.rb

  def complete_profile
    if current_user.field_one.present? && current_user.field_two.present?
      redirect_to the_path_after_log_in
    else
      redirect_to edit_profile_path, error: "Please update your field_one and field_two."
    end
  end

in other controller of your app:

before_filter :complete_profile
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top