Class: JunglePath::Authentication::DataProvider::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/jungle_path/authentication/data_provider/default.rb

Instance Method Summary collapse

Constructor Details

#initialize(sinatra, cache, db, user_model, models, roles = {}, schema_filters = {}, role_schema_filters = {}, role_query_filters = nil, restriction_query_filters = nil, user_query_filters = nil, role_table_filters = nil, restriction_table_filters = nil, user_table_filters = nil, alternative_user_key_queries = nil) ⇒ Default



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jungle_path/authentication/data_provider/default.rb', line 10

def initialize sinatra, cache, db, user_model, models, roles={}, schema_filters={}, role_schema_filters={}, role_query_filters=nil, restriction_query_filters=nil, user_query_filters=nil, role_table_filters=nil, restriction_table_filters=nil, user_table_filters=nil, alternative_user_key_queries=nil
  @sinatra = sinatra
  @cache = cache
  @db = db
  @user_model = user_model
  @models = models # (parameter models usually from Schema::Base.models)
  @roles = roles
  @role_permissions = {}
  @role_restrictions = {}
  @roles.each do |key, role|
    @role_permissions[role[:name]] = role[:permissions]
    @role_restrictions[role[:name]] = role[:restrictions]
  end
  @schema_filters = schema_filters
  @role_schema_filters = role_schema_filters
  @role_query_filters = role_query_filters
  @restriction_query_filters = restriction_query_filters
  @user_query_filters = user_query_filters
  @role_table_filters = role_table_filters
  @restriction_table_filters = restriction_table_filters
  @user_table_filters = user_table_filters
  @alternative_user_key_queries = alternative_user_key_queries
end

Instance Method Details

#get_alternative_user_keys(user_id, no_cache = false) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/jungle_path/authentication/data_provider/default.rb', line 136

def get_alternative_user_keys(user_id, no_cache=false)
  cache_key = "#{user_id}_alt_keys"
  alt_keys = @cache[cache_key]
  if alt_keys == nil or no_cache
    alt_keys = {}
    alt_keys[:user_id] = user_id
    if @alternative_user_key_queries
      @alternative_user_key_queries.each do |alt_key_query|
        ds = @db.base[alt_key_query[:query], user_id]
        hash = ds.first
        if hash
          alt_key = hash[:alt_key]
          alt_keys[alt_key_query[:name]] = alt_key
        end
      end
    end
  end
  puts "alt_keys: #{alt_keys}."
  alt_keys
end

#get_authorization_filter(identity, no_cache = false) ⇒ Object



76
77
78
# File 'lib/jungle_path/authentication/data_provider/default.rb', line 76

def get_authorization_filter(identity, no_cache=false)
  JunglePath::Authorization::Filter.new([identity.role], @models, @role_permissions, @role_restrictions, @role_schema_filters, @schema_filters)
end

#get_query_filters(identity, no_cache = false) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jungle_path/authentication/data_provider/default.rb', line 80

def get_query_filters(identity, no_cache=false)
  filters = []

  temp_filters = (@role_query_filters && @role_query_filters.call(identity)[identity.role]) || []
  temp_filters.each do |filter|
    filters << JunglePath::Query::Filter.new(filter[:table_name], filter[:sub_select])
  end

  temp_filters = (@restriction_query_filters && @restriction_query_filters.call(identity)) || {}
  identity.authorization_filter.restrictions.each do |restriction|
    temp = temp_filters[restriction] || []
    temp.each do |filter|
      filters << JunglePath::Query::Filter.new(filter[:table_name], filter[:sub_select])
    end
  end

  temp_filters = (@user_query_filters && @user_query_filters.call(identity)) || {}
  temp = temp_filters[identity.user.id] || []
  temp.each do |filter|
    filters << JunglePath::Query::Filter.new(filter[:table_name], filter[:sub_select])
  end

  filters
end

#get_role(identity, no_cache = false) ⇒ Object



72
73
74
# File 'lib/jungle_path/authentication/data_provider/default.rb', line 72

def get_role(identity, no_cache=false)
  @roles[identity.user.role.to_sym]
end

#get_table_filters(identity, no_cache = false) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/jungle_path/authentication/data_provider/default.rb', line 105

def get_table_filters(identity, no_cache=false)
  puts "get_table_filters!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  filters = {}

  puts "@role_table_filters: #{@role_table_filters} ------------------------------------------------------------------------------------"
  puts "identity.role: #{identity.role}"
  temp_filters = (@role_table_filters && @role_table_filters[identity.role[:name]]) || []
  puts "temp_filters: #{temp_filters} ------------------------------------------------------------------------------------"
  temp_filters.each do |filter|
    puts "role filter: #{filter}."
    filters[filter[:table_name]] = filter
  end

  identity.authorization_filter.restrictions.each do |restriction|
    temp_filters = (@restriction_table_filters && @restriction_table_filters[restriction]) || []
    temp_filters.each do |filter|
      puts "restriction filter: #{filter}."
      filters[filter[:table_name]] = filter
    end
  end

  temp_filters = (@user_table_filters && @user_table_filters[identity.user.id]) || []
  temp_filters.each do |filter|
    puts "user filter: #{filter}."
    filters[filter[:table_name]] = filter
  end

  puts "filters: #{filters}"
  filters
end

#get_user(user_name, password, assume_identity = false, no_cache = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jungle_path/authentication/data_provider/default.rb', line 34

def get_user(user_name, password, assume_identity=false, no_cache=false)
  lower_case_user_name = nil
  lower_case_user_name = user_name.downcase.to_sym if user_name
  cache_key = "#{lower_case_user_name}.#{password}"
  user = @cache[cache_key]
  if user == nil or no_cache
    hash = JunglePath::SQL::User.by_user_name(@db, user_name)
    puts "hash: #{hash}."
    #ds = @db.base["select * from \"user\" where user_name = ?", lower_case_user_name]
    #hash = ds.first
    user = @user_model.new(hash, false) if hash
    @sinatra.halt 401, "Unauthorized" unless user
    @sinatra.halt 401, "Unauthorized: user #{user.user_name} is not marked as active." unless user.active
    user.is_valid = assume_identity
    user.is_valid = JunglePath::Authentication::PasswordHash.validate_password(password, user.hash) unless assume_identity
    user.password = password
    @cache[cache_key] = user if user
  end
  user
end

#get_user_by_key(key, assume_identity = false, no_cache = false, password = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jungle_path/authentication/data_provider/default.rb', line 55

def get_user_by_key(key, assume_identity=false, no_cache=false, password=nil)
  cache_key = "#{key}."
  user = @cache[cache_key]
  if user == nil or no_cache
    hash = JunglePath::SQL::User.by_key(@db, key)
    puts "hash: #{hash}."
    #ds = @db.base['select * from "user" where id in (select user_id from key where key = ?)', key]
    #hash = ds.first
    user = @user_model.new(hash, false) if hash
    @sinatra.halt 401, "Unauthorized" unless user
    @sinatra.halt 401, "Unauthorized: user #{user.user_name} is not marked as active." unless user.active
    @cache[cache_key] = user if user
  end
  user
  get_user(user_name, password, no_cache)
end