class JunglePath::Authentication::DataProvider::Default

Public Class Methods

new(sinatra, cache, db, user_model, models, roles={}) click to toggle source
# 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

Public Instance Methods

get_alternative_user_keys(user_id, no_cache=false) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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