Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Adds CurrentUser processor #16

Closed
wants to merge 2 commits into from

Conversation

dlindahl
Copy link
Contributor

I'd like to add a few additional pages to my CASino powered application that require a user to be signed in.

Most authentication systems provide a user_signed_in? method to determine if a user is currently signed in and/or a current_user that returns that user's record. These methods are also typically used in a before_filter to restrict access to sensitive pages.

Browsing through the source code, it does not appear that there are any controller helpers that provide this information. The closest thing I could find was in app/controllers/casino/sessions_controller.rb on lines 5-6

processor(:TwoFactorAuthenticatorOverview).process(cookies, request.user_agent)
processor(:SessionOverview).process(cookies, request.user_agent)

However, I'm not sure that these methods would work very well in a helper method if they were simply ported over.

Do you have any thoughts on how to expose this information to application controllers outside of the context of CASino itself?

@calmyournerves
Copy link
Contributor

@dlindahl: Derek, is this still an issue?

@dlindahl
Copy link
Contributor Author

Yes, it is. I've realized that there is no way to easily get the current user, but I am about 75% done with a PR to add that support.

I'd like to keep this open so that I can convert it into a Pull Request once I am done, if thats OK with you.

@calmyournerves
Copy link
Contributor

Sounds good!

Rails declares ActionController::Base#cookies to be a private method.
CASino should respect this and not change it to be public just so that
listeners can interact with them.

This adds a convenience method to CASino::Listener that exposes an API
into ActionController::Base#cookies by calling #send in order to access
the private method.
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.23%) when pulling fbf1c97 on dlindahl:issues/16/current_user into a25fa57 on rbCAS:release/2.0.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.1%) when pulling fbf1c97 on dlindahl:issues/16/current_user into a25fa57 on rbCAS:release/2.0.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.1%) when pulling fbf1c97 on dlindahl:issues/16/current_user into a25fa57 on rbCAS:release/2.0.

@dlindahl
Copy link
Contributor Author

This extracts the "find user by TGT" logic that exists in all of the processors and puts it in a CurrentUser processor.

This processor is automatically run in a :before_filter for most controller actions. If a user is found, then the User record is assigned to the @current_user instance variable. From there it can be passed into the other processors as well as be used in the various CASino views.

An additional benefit of this feature is that the host application can also use this value in its own views:

# In my_app/app/controllers/widgets_controller.rb
class WidgetsController < ApplicationController
  include CASino::CurrentUserHelper

  def index
    # ...
  end
end

# In my_app/app/views/widgets/index.html.erb
<div class="widgets">
  <% if user_signed_in? %>
    <%= debug current_user %>
  <% end %>
</div>
  • Adds CurrentUser processor to encapsulate all user look-up based on TGT values
  • Provides #current_user and #user_signed_in? controller helper methods
  • Refactored some more complicated internal logic out into private methods for a handful of processors

end

def user_signed_in?
current_user
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be !!current_user to always return a boolean.

This extracts the "find user by TGT" logic that exists in all of the processors
and puts it in a CurrentUser processor.

This processor is automatically run in a :before_filter for most controller
actions. If a user is found, then the User record is assigned to the
@current_user instance variable. From there it can be passed into the other
processors as well as be used in the various CASino views.

An additional benefit of this feature is that the host application can also
use this value in its own views:

```ruby
class WidgetsController < ApplicationController
  include CASino::CurrentUserHelper

  def index
    # ...
  end
end

<div class="widgets">
  <% if user_signed_in? %>
    <%= debug current_user %>
  <% end %>
</div>
```

* Adds CurrentUser processor to encapsulate all user look-up based on TGT
  values

* Provides #current_user and #user_signed_in? controller helper methods

* Refactored some more complicated internal logic out into private methods
  for a handful of processors
@coveralls
Copy link

Coverage Status

Coverage remained the same when pulling 4519cc4 on dlindahl:issues/16/current_user into 0d650bc on rbCAS:release/2.0.

@luxflux
Copy link
Member

luxflux commented Apr 25, 2014

@pencil, what's up here?

@dlindahl
Copy link
Contributor Author

@luxflux I have some additional work to do based on comments from @pencil, but I just haven't had the time to address it. I'd like to keep this open for the time being, but if you want to close it out, that is also fine. I'll just re-open at a later date 😄

@luxflux
Copy link
Member

luxflux commented Apr 25, 2014

Okay, so i'll just keep it open 😄

@pencil
Copy link
Member

pencil commented Jul 19, 2014

I think we should plan a major overhaul of the processor/listener approach we currently use. It's too complicated and not of much use since we merged CASino and CASinoCore.

This was referenced Jul 28, 2014
@pencil
Copy link
Member

pencil commented Aug 28, 2014

CASino 4.0 will feature an easy way to access the desired information. (#63)

@pencil pencil closed this Aug 28, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants