Class: Origen::Users::User

Inherits:
Object show all
Defined in:
lib/origen/users/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ User

Returns a new instance of User



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/origen/users/user.rb', line 21

def initialize(*args)
  if args.last.is_a?(Symbol)
    @role = args.pop
  else
    @role = :user
  end
  if args.size == 2
    @name = args.first
  end
  id = args.pop
  if id.to_s =~ /(.*)@/
    @email = id
    @id = Regexp.last_match(1)
  else
    @id = id
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Provides methods to access attributes available from LDAP



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/origen/users/user.rb', line 143

def method_missing(method, *args, &block)
  l = Origen.ldap.lookup(self)
  if l
    if l.attribute_names.include?(method)
      l[method]
    else
      super
    end
  else
    super
  end
end

Instance Attribute Details

#email(options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/origen/users/user.rb', line 82

def email(options = {})
  if current?
    @email ||= ENV['ORIGEN_EMAIL'] || ENV['ORIGEN_USER_EMAIL'] || email_from_rc || begin
      if Origen.site_config.email_domain
        "#{id}@#{Origen.site_config.email_domain}"
      end
    end
  else
    @email ||= begin
      if Origen.site_config.email_domain
        "#{id}@#{Origen.site_config.email_domain}"
      end
    end
  end
end

#nameObject



74
75
76
# File 'lib/origen/users/user.rb', line 74

def name
  @name ||= ENV['ORIGEN_NAME'] || ENV['ORIGEN_USER_NAME'] || name_from_rc || @id
end

#roleObject (readonly)

Returns the value of attribute role



10
11
12
# File 'lib/origen/users/user.rb', line 10

def role
  @role
end

Class Method Details

.currentObject



17
18
19
# File 'lib/origen/users/user.rb', line 17

def self.current
  Origen.current_user
end

.current_user_idObject



13
14
15
# File 'lib/origen/users/user.rb', line 13

def self.current_user_id
  `whoami`.strip
end

Instance Method Details

#==(user) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/origen/users/user.rb', line 132

def ==(user)
  if user.is_a?(Origen::Users::User)
    user.id == id
  elsif user.is_a?(String)
    user.downcase == id
  else
    super
  end
end

#admin?Boolean

Returns true if the user is an admin for the current application

Returns:

  • (Boolean)


59
60
61
# File 'lib/origen/users/user.rb', line 59

def admin?
  role == :admin
end

#auth_sessionObject

Returns a private global Origen session store (stored in the user's home directory and only readable by them). See - origen-sdk.org/origen/guides/misc/session/#Global_Sessions



174
175
176
177
178
179
180
# File 'lib/origen/users/user.rb', line 174

def auth_session
  @session ||= begin
    @session = Origen.session.user
    @session.private = true
    @session
  end
end

#current?Boolean

Returns true if the user is the current user

Returns:

  • (Boolean)


64
65
66
# File 'lib/origen/users/user.rb', line 64

def current?
  id.to_s.downcase == self.class.current_user_id
end

#decrypt(text) ⇒ Object



209
210
211
# File 'lib/origen/users/user.rb', line 209

def decrypt(text)
  text
end

#email_from_rcObject



98
99
100
# File 'lib/origen/users/user.rb', line 98

def email_from_rc
  RevisionControl::Git.user_email
end

#encrypt(text) ⇒ Object



213
214
215
# File 'lib/origen/users/user.rb', line 213

def encrypt(text)
  text
end

#id(options = {}) ⇒ Object Also known as: core_id, username



50
51
52
53
54
# File 'lib/origen/users/user.rb', line 50

def id(options = {})
  # Way to force Origen to use the new user ID in case of WSL where the core ID might not match the WSL login name
  # User needs to setup the environment variable in their .bashrc or .tcshrc file
  ENV['ORIGEN_USER_ID'] || @id.to_s.downcase
end

#initialsObject

Returns the user's initials in lower case



69
70
71
72
# File 'lib/origen/users/user.rb', line 69

def initials
  initials = name.split(/\s+/).map { |n| n[0].chr }.join('')
  initials.downcase
end

#lookup(default = 'Unknown') ⇒ Object

Fetch user data from the FSL application directory

Examples:


User.new("r49409").lookup.motunixdomain   # => ["cde-tx32.sps.mot.com"]


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/origen/users/user.rb', line 107

def lookup(default = 'Unknown')
  data = Origen.ldap.lookup(self)
  if block_given?
    if data
      yield data
    else
      default
    end
  else
    data
  end
end

#name_and_emailObject

Returns a string like “Stephen McGinty <stephen.mcginty@nxp.com>”



167
168
169
# File 'lib/origen/users/user.rb', line 167

def name_and_email
  "#{name} <#{email}>"
end

#name_from_rcObject



78
79
80
# File 'lib/origen/users/user.rb', line 78

def name_from_rc
  RevisionControl::Git.user_name
end

#password(options = {}) ⇒ Object

Returns the password for the current user. If the user hasn't supplied it yet they will be prompted to enter it, it will then be stored

First, try in the global session, if its not defined, ask for it.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/origen/users/user.rb', line 186

def password(options = {})
  unless current?
    fail "You can only reference the password for the current user (#{self.class.current_user_id})!"
  end

  if options[:refresh]
    auth_session[:password] = nil
  end

  if auth_session[:password]
    password = decrypt(auth_session[:password])
  else
    puts 'Please enter your password:'
    password = (STDIN.noecho(&:gets) || '').chomp

    # TODO: Need some kind of callback here to optionally verify password correctness via LDAP or similar

    auth_session[:password] = encrypt(password)
  end

  password
end

#rawObject Also known as: display

Prints all raw data available on the given user from the FSL application directory.

Most of the useful data is already exposed through the available user methods, but if you want to get any of these parameters they can be fetched via the lookup method.



126
127
128
129
# File 'lib/origen/users/user.rb', line 126

def raw
  Origen.ldap.display(self)
  nil
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
164
# File 'lib/origen/users/user.rb', line 156

def respond_to?(method, include_private = false)
  super || begin
    if Origen.ldap.available?
      Origen.ldap.lookup(self) && Origen.ldap.lookup(self).attribute_names.include?(method.to_sym)
    else
      false
    end
  end
end

#send(options) ⇒ Object

Send the user an email

Examples:

User.current.send subject: "Complete", message: "Your job is done!"
User.new("r49409").send subject: "Complete", message: "Your job is done!"


44
45
46
47
48
# File 'lib/origen/users/user.rb', line 44

def send(options)
  options[:body] ||= options[:message]
  options[:to] = self
  Origen.mailer.send_email(options)
end