Rails get username of currently logged windows user - ruby-on-rails

I need to get the username of the currently logged windows user. Could it be done easily?

The username of the account running the script can be accessed via something like:
puts ENV['USERNAME']
Beware that if you're running it as a system service the username will probably come back as "SYSTEM"
If that isn't enough to suite your needs there is an alternative method outlined here: https://stackoverflow.com/a/3544741/648695

You can use the Ruby etc module
require 'etc'
Etc.getlogin
The doc is avaiable here: http://ruby-doc.org/stdlib-2.0.0/libdoc/etc/rdoc/Etc.html#method-c-getlogin
Returns the short user name of the currently logged in user.

As far as I know, unless you're using active directory that would require a microsoft framework website, I don't think you'll find a way in rails.
There are a few discussion points here that may help: Can you get a Windows (AD) username in PHP?

Same question here: is there a way to read a clients windows login name using ruby on rails
Anyways, just copying my own answer below...
This is what worked for me but there are some limitations:
won't work in Chrome: undefined method 'encode' for nil:NilClass
won't validate user credentials
If you don't care about these issues, go ahead:
In your rails application, add Rekado's gem to your Gemfile: gem 'ntlm-sso', '=0.0.1'
Create an initialiser config/initializers/ntlm-sso.rb with:
require 'rack'
require 'rack/auth/ntlm-sso'
class NTLMAuthentication
def initialize(app)
#app = app
end
def call(env)
auth = Rack::Auth::NTLMSSO.new(#app)
return auth.call(env)
end
end
On your application.rb file, add the line: config.middleware.use "NTLMAuthentication"
Call request.env["REMOTE_USER"] on your view or controller to get current username.
PS: Let me know if you find anyway to make it work on Chrome or to validate user credentials.

Related

Where I need to place ruby-trello gem's config code?

I am creating rails API app for managing Trello related boards and lists.
I have decided to use "ruby-trello" gem.
There is a config example at github ruby-trello page (https://github.com/jeremytregunna/ruby-trello).
Config looks like this:
Trello.configure do |config|
config.developer_public_key = 'my_trello_key'
config.member_token = 'my_trello_token'
end
I got key and token from trello devs.
I have added this code to my config/application.rb in class Application.
But when I am trying to get something data from Trello I see this error:
Trello::ConfigurationError (Trello has not been configured to make authorized requests.)
Can anybody help? Where I need to place this config code?
Sorry for my English.
The Rails convention is to put that in config/initializers/trello.rb so that it's picked up when Rails boots.
It's odd they don't seem to mention that in the documentation.

How to re-generate the user.confirmation_token

I'm looking for a way to regenerate the user.confirmation_token. In my app, I allow users to deactivate their account with user.deleted_at. If a deactivate user tries to re-activate their account, I want to see a confirmation_instructions mail but need a confirmation token set. Is there a way with devise to re-generate the user.confirmation_token inside the RegistrationsController?
Theres the generate_confirmation_token method in the Confirmable module which is applied to other classes via ActiveSupport::Concern. Like you see here:
https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb
Reapllying this method on your User object should do the Trick.
// The generate_confirmation_token method is protected so it cant be called from outside the class. You can run send_confirmation_instructions which generates the token and delivers a registration mail.
// Watching at the source if the confirmable module of the gem let it look like the method does. If it isnt possible by default you need to costumize the gem. You can get the gemsets path by the following command in the bash
rvm gemset path
then in the gems directory you will find the version of devise installed. In the lib/devise/models/confirmable.rb file you can add a not protected method that calls the generate_confirmation_token method. You can only call it from that module because its protected.

Why can't I deauthorize Rack-mini-profiler for non admin users

I am trying to install Rack-mini-profiler on my ROR application.
I installed the gem and the profiler works great in development but I can't deauthorize specific requests for non admin users.
I placed the following code in my ApplicationController before_filter
def authorize_mini_profiler
if current_user.nil?
Rack::MiniProfiler.deauthorize_request
return
elsif is_admin_user
Rack::MiniProfiler.authorize_request
return
end
Rack::MiniProfiler.deauthorize_request
end
In debug I saw that the deauthorize method is called but the profiler is still displayed.
I even tried using this code
def authorize_mini_profiler
Rack::MiniProfiler.deauthorize_request
end
but still, every request by any user displays the profiler.
Does anyone knows what might be the problem?
Well, for those who run into the same problem...
Deeper debugging found that the gem is configured for ignoring the authorization mechanism on init. In order to enable profiling only on some cases (e.g. non production or only for admin users) you need to override the default configuration in application.rb (or preferably some specific config file):
Rack::MiniProfiler.config.authorization_mode = :whitelist if Rails.env.production?
otherwise the configuration is set to :allowall

Ruby on Rails - Prompting user for password

I'm trying to add some basic file-download functionality to my Rails application and have the password_hash and password_salt fields for my file model. I also have a function in which a download link to the file is generated, however I'm not totally sure (as I'm sort of reasonably new to Ruby and Rails) how I'd go about actually prompting the user for a password and checking this before proceeding with the file download.
Any help would be appreciated.
You should target you link to a controller and send the file like this:
before_filter :login_required
def download
send_file '/home/railsway/downloads/huge.zip', :type=>"application/zip"
end
this way you can check the password in you before filter using HTTP Basic Auth for example.
more info: http://www.therailsway.com/2009/2/22/file-downloads-done-right

vimeo ruby gem auth_token example for oAuth?

Anyone know what the correct syntax to get an auth_token from Vimeo using the recently updated vimeo gem (http://github.com/matthooks/vimeo) using oAuth?
I'm trying this:
def authorize
base = Vimeo::Advanced::Base.new(VIMEO_API_KEY, VIMEO_SECRET)
redirect_to base.web_login_link("delete")
end #end method
--- get redirected to vimeo, allow access, then get redirected to app with frob variable ---
def callback
vimeo = Vimeo::Advanced::Auth.new(VIMEO_API_KEY, VIMEO_SECRET)
auth_token = vimeo.get_token(params[:frob])
end
-- now I get the following error:
{"err"=>{"msg"=>"Missing required parameter", "code"=>"307", "expl"=>"A required parameter was missing: oauth_consumer_key"}, "stat"=>"fail", "generated_in"=>"0.0054"}
Anyone familiar with this gem or the correct syntax to get the auth_token for making authenticated calls in the future
Based on the documentation on authenticating as a website in the readme, it looks like you're missing a few steps. The github README should have the latest info, so I won't paste it here.
Are you trying to accomplish something other than the example given is showing you how to do?
Have you tried vendoring the code instead of using the gem? Perhaps recent updates weren't deployed as a gem yet, as it does look like OAuth is a new feature based on the github history.

Resources