Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for running custom code before save session #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Added support for running custom code before save session #22

wants to merge 1 commit into from

Conversation

tonidas
Copy link

@tonidas tonidas commented Oct 16, 2013

A nice feature missing is to customize the Session class, adding attributes, indexes and another stuff useful to the application.

To do this you can simply add code to the class Session inside ActionDispatch::Session::MongoidStore (or the others stores available).
But inside this class you can use almost nothing of your app, like the environment or the session data (before it's converted to binary).

A simple solution is in this pull request: Add a call to a method called "before_save_session" passing the major variables availables: "env", "sid", "session_data" and "options". This method is suposed to be implemented by the application using this gem.

Now you can do this (if you're using warden) to save the user_id in the session:

module ActionDispatch
  module Session
    class MongoidStore < MongoStoreBase
      class Session

        field :user_id, :type => Moped::BSON::ObjectId
        index({ user_id: 1 })

        def before_save_session(env, sid, session_data, options)
          puts "before_save_session"
          if !self.user_id && env['warden'] && env['warden'].user
            self.user_id = env['warden'].user._id
          end
        end

      end
    end
  end
end

With the user_id in the session (in database) you can do fun things like remove all sessions from a specific user.

Cheers

@brianhempel
Copy link
Owner

Why would you do it here rather than at the controller level?

@tonidas
Copy link
Author

tonidas commented Oct 21, 2013

The rails model callbacks aren't supposed to be in the model?

I think it would be neat to mess with one and only one file to do this kind of change. In this case it will be one file called mongoid_store.rb that could be saved in the lib folder, or in the config/initializers folder, because it's necessary to add one attribute and one index at the Session model. Something like occurs in the ActiveRecord Store. The difference is that we need the enviroment stuff in this "distant" model that cannot access anything useful.

But I'll be glad to hear another solution in the controller level.

Thanks for the reply and for keeping this repo.

@brianhempel brianhempel force-pushed the master branch 3 times, most recently from 9d3f5ea to d29c4d9 Compare March 30, 2015 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants