Class: Users::Registry

Inherits:
Object show all
Includes:
Omega::Server::Registry
Defined in:
lib/users/registry.rb

Overview

Primary server side entity tracker for the Users module.

Provides a thread safe registry through which users can be accessed and managed.

Also provides thread safe methods which to query users and privileges based on a session id and other parameters

Singleton class, access via Users::Registry.instance.

Constant Summary

Constants included from Omega::Server::Registry

Omega::Server::Registry::DEFAULT_COMMAND_POLL, Omega::Server::Registry::DEFAULT_EVENT_POLL, Omega::Server::Registry::DEFAULT_LOOP_POLL

Config options collapse

Attributes included from Omega::Server::Registry

#backup_excludes, #node, #retrieval, #user, #validation_methods

Config options collapse

Instance Method Summary collapse

Methods included from Omega::Server::Registry

#<<, #cleanup_event, #clear!, #delete, #entities, #entity, #exclude_from_backup, #join, #on, #proxies_for, #proxy_for, #raise_event, #restore, #run, #running?, #safe_exec, #save, #start, #stop, #to_s, #update, #validation_callback

Constructor Details

#initializeRegistry

Users::Registry intitializer


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/users/registry.rb', line 79

def initialize
  init_registry

  exclude_from_backup Users::Session
  exclude_from_backup Omega::Server::EventHandler

  # validate user/role id or session's user id is unique on creation
  self.validation_callback { |r,e|
    e.kind_of?(Omega::Server::Event) ||
    e.kind_of?(Omega::Server::EventHandler) ||
    ([User, Role, Session].include?(e.class) &&

     (e.is_a?(Session) ?
        r.select  { |re| re.is_a?(Session)      }.
          find    { |s|  s.user.id == e.user.id }.nil? :
        r.find    { |re| re.class == e.class && re.id == e.id }.nil?))
  }

  # set user timestamps on creation
  on(:added) { |e|
    if e.is_a?(User)
      e.created_at       = Time.now
      e.last_modified_at = Time.now
    end
  }

  # sanity checks on user
  on(:added)   { |e|    check_user(e)    if e.is_a?(Users::User) }
  on(:updated) { |e,oe| check_user(e,oe) if e.is_a?(Users::User) }

  # sanity checks on session
  on(:added)   { |e|    check_session(e) if e.is_a?(Users::Session) }

  # uniqueness checks on event handlers
  on(:added)   { |e| sanitize_event_handlers(e) if e.kind_of?(Omega::Server::EventHandler) }

  # run local events
  run { run_events }
end

Class Attribute Details

.user_perms_enabledObject

Boolean toggling if user permission system is enabled / disabled. Disabling permissions will result in all require/check privileges calls returning true

TODO ideally would have this in rjr adapter like user_attributes. To do this, all require/check privilege calls (as invoked by other subsystems) would have to go through rjr


34
35
36
# File 'lib/users/registry.rb', line 34

def user_perms_enabled
  @user_perms_enabled
end

Class Method Details

.set_config(config) ⇒ Object

Set config options using Omega::Config instance


39
40
41
# File 'lib/users/registry.rb', line 39

def set_config(config)
  self.user_perms_enabled = config.user_perms_enabled
end

Instance Method Details

#check_privilege(args = {}) ⇒ true, false

Return true/false indicating if user correspoding to current session has specified privileges

Options Hash (args):

  • :session (String)

    id of session to use to lookup user

  • :privilege (String)

    id of privilege which to check on user

  • :entity (String)

    id or of entity corresponding to privilege to check

  • :any (Array<Hash<Symbol,String>>)

    array of hashes containing :privilege and :entity attributes corresponding to ids of privileges and corresponding entities to look for. So long as the user has *at least one* of these privilege / entity pairs, true will be returned


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/users/registry.rb', line 204

def check_privilege(args = {})
  return true unless Registry.user_perms_enabled

  # TODO incorporate session privilege checks into a larger generic rjr ACL subsystem (allow generic acl validation objects to be registered for each handler)
  session_id    = args[:session]
  privilege_ids = Array(args[:privilege])
  entity_ids    = Array(args[:entity])

  args[:any].to_a.each{ |pe|
    privilege_ids << pe[:privilege]
    entity_ids    << pe[:entity]
  }

  log_args = args.reject { |k,v| k == :registry }.inspect

  session = entities { |e| e.is_a?(Session) && e.id == session_id }.first
  if session.nil?
    ::RJR::Logger.warn "check_privilege(#{log_args}): session not found"
    return false
  end

  if session.timed_out?
    destroy_session :session_id => session.id
    ::RJR::Logger.warn "check_privilege(#{log_args}): session timeout"
    return false
  end

  0.upto(privilege_ids.size-1){ |pi|
    privilege_id = privilege_ids[pi]
    entity_id    = entity_ids[pi]

    if (entity_id != nil && session.user.has_privilege_on?(privilege_id, entity_id)) ||
       (entity_id == nil && session.user.has_privilege?(privilege_id))
         return true
    end
  }

  return false
end

#create_session(user, source_node) ⇒ Object

Return session for the specified user, if none exists create one first


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/users/registry.rb', line 139

def create_session(user, source_node)
  # just return user session if already existing
  session = self.entities { |e|
    e.is_a?(Session) && e.user.id == user.id
  }.first

  # remove session if timed out
  if !session.nil? && session.timed_out?
    destroy_session :session_id => session.id
    session = nil
  end

  # FIXME update endpoint_id if session not nil
  return session unless session.nil?

  user. = Time.now
  session = Session.new :user           => user,
                        :refreshed_time => user.,
                        :endpoint_id    => source_node
  self << session
  return session
end

#current_session(args = {}) ⇒ Users::Session?

Return the active Session cooresponding to the specified id. If session has expired, it is invalidated and nil is returned

Options Hash (args):

  • :id (String)

    id of the session to lookup


271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/users/registry.rb', line 271

def current_session(args = {})
  session_id = args[:id]

  session = self.entity{ |e| e.is_a?(Session) && e.id == session_id }

  return nil if session.nil?
  if session.timed_out?
    destroy_session :session_id => session.id
    return nil
  end

  session
end

#current_user(args = {}) ⇒ Users::User?

Return the User corresponding to the specified active session id If session has expired, it is invalided and nil is returned

Options Hash (args):

  • :session (String)

    id of the session to lookup corresponding user for


250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/users/registry.rb', line 250

def current_user(args = {})
  session_id = args[:session]

  session = self.entities{ |e| e.is_a?(Session) && e.id == session_id }.first

  return nil if session.nil?
  if session.timed_out?
    destroy_session :session_id => session.id
    return nil
  end

  session.user
end

#destroy_session(args = {}) ⇒ Object

Destroy the session specified by the given args


167
168
169
170
171
172
173
# File 'lib/users/registry.rb', line 167

def destroy_session(args = {})
  self.delete { |e|
    e.is_a?(Session) &&
    (e.id      == args[:session_id] ||
     e.user.id == args[:user_id])
  }
end

#require_privilege(args = {}) ⇒ Object

Wrapper around #check_privilege that raises error if user does not have privilege

Takes same parameter list as #require_privilege

Raises:


181
182
183
184
185
186
187
188
189
190
# File 'lib/users/registry.rb', line 181

def require_privilege(args = {})
  unless check_privilege(args)
    log_args = args.reject { |k,v| k == :registry }.inspect
    err = "require_privilege(#{log_args}): user does not have required privilege"

    # TODO custom error messages
    ::RJR::Logger.warn err
    raise Omega::PermissionError, err
  end
end

#valid_login?(user_id, password) ⇒ true/false

Return boolean indicating if login credentials for the specified user are valid


126
127
128
129
130
131
132
# File 'lib/users/registry.rb', line 126

def valid_login?(user_id, password)
  @lock.synchronize {
    user = @entities.find { |e| e.is_a?(User) && e.id == user_id }
    return false if user.nil?
    user.valid_login?(user_id, password)
  }
end