How can I include devise usernames on rails logs (using graylog2) - ruby-on-rails

Is there a way to include the logged in user in rails logs? I am using devise and graylog2, the latter of which I assume is inconsequential to the answer.

Devise provides a current_user helper in the controller which can be used to access the currently signed-in user.
Assuming the user model has a name attribute, you can do the following:
::Rails.logger.info "The currently logged-in user is #{current_user.name}"

Related

How to authenticate two types of user with same authentication callback in rails without devise gem?

I want to create a webpage using rails 6.0.0, which has two types of users(model) without devise gem.
Admin
User/viewer
But the problems is that I want to authenticate (sign up /sign in) for both users with facebook or google_oauth2 with same callbacks address without devise gem:
/auth/:provider/callback
On internet, github and youtube I have found articles and videos only for authenticate single model. But I want to authenticate (sign up /sign in) for two models.
Can anyone help?
Please give an idea to authenticate both model with some example without devise gem that an beginner can understand.
Generally it is not advised to authenticate two models. Only in really rare cases this is needed. (Think of Uber which has drivers and clients booking the rides).
If you want to have an admin and a normal user, the way to do it is to add a column in the user model called admin which is a boolean. This way you only have one user model - most of the times the admin can do at leat what the user can do.
If your user is an admin, you just set the admin boolean to true :)

Accessing current_user devise method from rails controller

I'm using devise in a rails app I'm building. I'd like to get the ID of the user that is currently signed in, with current_user.id, in my controller, without passing it as a parameter from my view. Is there any way I can do this? Thanks
If you have installed Devise properly and have not broken something by overriding the Devise controllers, you should be able to access current_user.id from the controller. This method will only work if a user is signed in which you can test with the user_signed_in? method. Finally, this assumes that the resource name Devise is using is indeed user. That is the default, but it is possible to configure Devise to work with different resource names.

Logout users with devise gem rails

In admin section, I'm showing a list of currently logged in users.
Now admin can select one or more user/users and destroy their session(logout them).
I'm not able to figure where to start from,please help me.
You can use the sign_out method in the controller action by passing in the user object:
# Make sure only admins can do this
def sign_out_user
#user = User.find(params[:id])
sign_out #user
end
More info here:
http://rubydoc.info/github/plataformatec/devise/master/Devise/TestHelpers%3asign_out
Considering users is the collection of your required users,
for user in users
sign_out user
end
It should solve your issue.
Hope it helps :)
The sign_out method provided by Devise won't help. I know the documentation says that it will logout the "resource" you requested, but if you dig into the gems themselves (devise and warden) you'll find that when you give it an object, like a user, it merely figures out what scope (ie, :user) that object belongs to, and it logs out that entire scope.
A scope in Devise is a namespace for logins. You might have a Customer model that requires logins, but also a Vendor model that also requires logins, and you'd use different scopes for those. Most applications only use a single scope, tied to the User model.
You're probably using :cookie_store for your session storage, which is the Rails default. In this case, it isn't possible to log out any single user except yourself. Devise stores your login info in the session, which is stored in a cookie, and not in your database. Their browser has the credentials, so you can't directly remove that.

Rails 3 current site

Hey there I am using devise authentication for my rails app and I am aware that I can use the helper current_user to access current user in my model or controller. Where I am lost is how can I find the site_id that a user owns. If i look in the active records I can see the users site id. Also I am using the subdomains app from github so my code is identical at the moment.
How can I create a method that tells rails to get the users site_id that is logged in?
You said that the user model has a site_id attribute, and current_user returns a user model, so by the transitive property, current_user should have a site_id attribute, right?
Apparently you can also access the site object itself by using current_user.site.

Devise will not let me register a user while logged in

I'm using devise for the user system but I have one problem. I'd like for a logged-in user to register new user. It's a question of security. However, a logged-in user can not currently register a new user.
I dont know how fix this.
Thanks by help!
I'm pretty sure the easist way would be just to turn off the devise config option :registerable, this will get rid of the sign_up paths and links.
Then just build your own user controller actions and views to interact directly with your User model.
The default devise registrations controller wants to auto create a new session for the newly created user which is why it won't let logged in users create another user.
Hope this helps.
The Devise documentation explains how to do that kind of thing.
You don't need to over-ride anything in devise. Just treat it like any other namespaced resource.

Resources