1

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

user1107922
  • 610
  • 1
  • 12
  • 25
  • possible duplicate of [is there a way to read a clients windows login name using ruby on rails](http://stackoverflow.com/questions/5506932/is-there-a-way-to-read-a-clients-windows-login-name-using-ruby-on-rails) – bummi Jun 15 '15 at 23:08

4 Answers4

3

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

Community
  • 1
  • 1
Mario Zigliotto
  • 8,315
  • 7
  • 52
  • 71
2

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.

Pioz
  • 6,051
  • 4
  • 48
  • 67
0

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?

Community
  • 1
  • 1
Matt
  • 13,948
  • 6
  • 44
  • 68
0

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:

If you don't care about these issues, go ahead:

  1. In your rails application, add Rekado's gem to your Gemfile: gem 'ntlm-sso', '=0.0.1'

  2. 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
    
  3. On your application.rb file, add the line: config.middleware.use "NTLMAuthentication"

  4. 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.

Community
  • 1
  • 1
Flavio Wuensche
  • 9,460
  • 1
  • 57
  • 54