Class: Lanes::API::AuthenticationProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/lanes/access/authentication_provider.rb,
lib/lanes/api/null_authentication_provider.rb

Constant Summary collapse

USER =
DummyUser.new

Instance Method Summary collapse

Constructor Details

#initialize(session: nil, params: nil, request_type: type) ⇒ AuthenticationProvider

Returns a new instance of AuthenticationProvider.



5
6
7
8
9
# File 'lib/lanes/access/authentication_provider.rb', line 5

def initialize(session:nil, params:nil, request_type: type)
    @session = session
    @params  = params
    @request_type = request_type
end

Instance Method Details

#allowed_access_to?(klass) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lanes/access/authentication_provider.rb', line 29

def allowed_access_to?(klass)
    return false if current_user.nil?
    case @request_type
    when 'GET'
        klass.can_read_attributes?(@params,current_user)
    when 'POST','PATCH','PUT'
        klass.can_write_attributes?(@params,current_user)
    when 'DELETE'
        klass.can_delete_attributes?(@params,current_user)
    else
        false
    end
end

#current_userObject



11
12
13
# File 'lib/lanes/access/authentication_provider.rb', line 11

def current_user
    @current_user ||= Lanes::User.where(id: @session['user_id']).first
end

#error_messageObject



15
16
17
# File 'lib/lanes/access/authentication_provider.rb', line 15

def error_message
    current_user ? "User not found" : error_message_for_access
end

#error_message_for_accessObject



19
20
21
22
23
24
25
26
27
# File 'lib/lanes/access/authentication_provider.rb', line 19

def error_message_for_access
    return "Unable to " + case @request_type
                          when 'GET' then "read"
                          when 'POST','PATCH','PUT' then "write"
                          when 'DELETE' then "delete"
                          else
                              "perform action"
                          end
end

#wrap_request(model, req) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lanes/access/authentication_provider.rb', line 44

def wrap_request(model, req)
    if allowed_access_to?(model)
        ::Lanes::User.scoped_to(current_user) do | user |
            yield
        end
    else
        Lanes.logger.warn "Unauthorized access attempted to #{req}"
        req.halt( 401, Oj.dump({
          success:false, errors: {user: "Access Denied"}, message: "Access Denied"
        }))
    end
end