Module: Challah::Controller::InstanceMethods
- Defined in:
- lib/challah/controller.rb
Instance Method Summary (collapse)
-
- (Object) access_denied!
protected
Stop execution of the current action and display the access denied message.
-
- (User?) current_user
protected
The user that is currently logged into this session.
-
- (Boolean) current_user?
(also: #logged_in?)
protected
Is there currently a logged in user? Returns true if it is safe to use the current_user method.
-
- (Session) current_user_session
protected
The current authentication session, if one exists.
-
- (Object) has(permission_key)
(also: #permission?)
protected
Checks the current user to see if they have the given permission key.
-
- (Object) login_required
protected
Restrict a controller to only authenticated users.
Instance Method Details
- (Object) access_denied! (protected)
Stop execution of the current action and display the access denied message.
If the user is not logged in, they are redirected to the login screen.
By default the built-in access denied message is displayed, but you can display a different message by setting the following option in an initializer:
Challah.[:access_denied_view] = 'controller/denied-view-name'
A status code of :unauthorized (401) will be returned.
Override this method if you'd like something different to happen when your users get an access denied notification.
89 90 91 92 93 94 95 96 |
# File 'lib/challah/controller.rb', line 89 def access_denied! if current_user? render :template => Challah.[:access_denied_view], :status => :unauthorized and return else session[:return_to] = request.url redirect_to login_path and return end end |
- (User?) current_user (protected)
This method is also available as a helper in your views.
The user that is currently logged into this session. If there is no user logged in, nil will be returned.
117 118 119 |
# File 'lib/challah/controller.rb', line 117 def current_user @current_user ||= current_user_session.user end |
- (Boolean) current_user? (protected) Also known as: logged_in?
This method is also available as a helper in your views.
Is there currently a logged in user? Returns true if it is safe to use the current_user method.
106 107 108 |
# File 'lib/challah/controller.rb', line 106 def current_user? !!current_user end |
- (Session) current_user_session (protected)
126 127 128 |
# File 'lib/challah/controller.rb', line 126 def current_user_session @current_user_session ||= Challah::Session.find(request, params) end |
- (Object) has(permission_key) (protected) Also known as: permission?
This method is also available as a helper in your views.
Checks the current user to see if they have the given permission key. If there is not a user currently logged in, false is always returned.
146 147 148 |
# File 'lib/challah/controller.rb', line 146 def has() current_user and current_user.has() end |
- (Object) login_required (protected)
Restrict a controller to only authenticated users. If someone tries to access a restricted action and is not logged in, they will be redirected to the login page.
This method is an alias for:
restrict_to_authenticated
174 175 176 177 178 179 |
# File 'lib/challah/controller.rb', line 174 def login_required unless logged_in? session[:return_to] = request.url redirect_to login_path and return end end |