Class: SqAuth::SqAuthAccess

Inherits:
Object
  • Object
show all
Defined in:
lib/sq_auth/sq_auth_access.rb

Constant Summary collapse

DEFAULT_ACCESS_PATH =
"/access_partial"
DEFAULT_DRAW_PROC =
Proc.new {|uri, callback, role, project, session| SqAuthUtils::default_draw_template(uri, callback, role, project, session)}
DEFAULT_DATA_PROC =
Proc.new { "Forbidden" }
DEFAULT_OPTIONS =
{project: "Project", https: true}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SqAuthAccess

Returns a new instance of SqAuthAccess.



8
9
10
11
12
13
14
# File 'lib/sq_auth/sq_auth_access.rb', line 8

def initialize options={}
  @options = options
  @data_proc = DEFAULT_DATA_PROC
  @draw_proc = DEFAULT_DRAW_PROC
  @login_path = DEFAULT_ACCESS_PATH
  @gateway_ip = nil
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



7
8
9
# File 'lib/sq_auth/sq_auth_access.rb', line 7

def callback
  @callback
end

#gateway_ipObject

Returns the value of attribute gateway_ip.



7
8
9
# File 'lib/sq_auth/sq_auth_access.rb', line 7

def gateway_ip
  @gateway_ip
end

#login_pathObject

Returns the value of attribute login_path.



7
8
9
# File 'lib/sq_auth/sq_auth_access.rb', line 7

def 
  @login_path
end

#project_nameObject

Returns the value of attribute project_name.



7
8
9
# File 'lib/sq_auth/sq_auth_access.rb', line 7

def project_name
  @project_name
end

Instance Method Details

#api_filter(roles, project = @project_name, when_ok, when_no_role, options) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/sq_auth/sq_auth_access.rb', line 46

def api_filter(roles, project = @project_name, when_ok, when_no_role, options)
  check_session_provider
  not_logged_in_proc = get_not_logged_in_proc(options[:draw])
  if sq_auth_filter(roles, project, options)
    when_ok.call
  else
    when_no_role.(not_logged_in_proc.(, callback, roles, project, @session_provider.session_for_current_user))
  end
end

#check_session_providerObject



41
42
43
44
# File 'lib/sq_auth/sq_auth_access.rb', line 41

def check_session_provider
  raise "Authentication service not specified" unless @session_provider
#  raise "Authentication service unavailable" unless @session_provider.available?
end

#connect(options = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



16
17
18
19
20
21
22
23
# File 'lib/sq_auth/sq_auth_access.rb', line 16

def connect options = {}
  options = DEFAULT_OPTIONS.merge(options)
  @project_name = options[:project]
  @gateway_ip = options[:gateway_ip]
  yield self if block_given?
  SqAuthIntegration.alter_environment options
  @session_provider = SqAuthClient.new options
end

#current_user_ip=(ip) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/sq_auth/sq_auth_access.rb', line 88

def current_user_ip= ip
  if @gateway_ip
    @session_provider.user.user_ip = @gateway_ip
  else
    @session_provider.user.user_ip = ip
  end
  p @session_provider.user.user_ip
end

#data_when_not_logged_in(&block) ⇒ Object



29
30
31
# File 'lib/sq_auth/sq_auth_access.rb', line 29

def data_when_not_logged_in &block
  @data_proc = Proc.new {|*args| block.call(*args)}
end

#draw_when_not_authenticated(roles, project = @project_name) ⇒ Object



66
67
68
# File 'lib/sq_auth/sq_auth_access.rb', line 66

def draw_when_not_authenticated(roles, project = @project_name)
  when_not_authenticated(roles, project, {:draw => true})
end

#draw_when_not_logged_in(&block) ⇒ Object



25
26
27
# File 'lib/sq_auth/sq_auth_access.rb', line 25

def draw_when_not_logged_in &block
  @draw_proc = block
end

#get_not_logged_in_proc(type = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/sq_auth/sq_auth_access.rb', line 33

def get_not_logged_in_proc type = nil
  if type == true || type.nil?
    @draw_proc
  else
    @data_proc
  end
end

#login_toObject



97
98
99
100
101
# File 'lib/sq_auth/sq_auth_access.rb', line 97

def 
  uri = @session_provider.auth_server_uri
  uri.path = "#@login_path"
  uri.to_s
end

#message_when_not_authenticated(roles = [], project = @project_name) ⇒ Object



70
71
72
# File 'lib/sq_auth/sq_auth_access.rb', line 70

def message_when_not_authenticated(roles = [], project = @project_name)
  when_not_authenticated(roles, project, {:draw => false})
end

#save_session_for_current_user(sqauthsession) ⇒ Object



84
85
86
# File 'lib/sq_auth/sq_auth_access.rb', line 84

def save_session_for_current_user sqauthsession
  @session_provider.create_session_for_current_user sqauthsession
end

#sq_auth_filter(roles, project = @project_name, options = {}) ⇒ Object



56
57
58
# File 'lib/sq_auth/sq_auth_access.rb', line 56

def sq_auth_filter(roles, project = @project_name, options = {})
  @session_provider.session_for_current_user && @session_provider.role_exist_for_current_user?(roles, project)
end

#when_not_authenticated(roles, project, options = {}) ⇒ Object



61
62
63
64
# File 'lib/sq_auth/sq_auth_access.rb', line 61

def when_not_authenticated(roles, project, options = {})
  not_logged_in_proc = get_not_logged_in_proc(options[:draw])
  not_logged_in_proc.call(, callback, roles, project, @session_provider.session_for_current_user)
end

#with_sq_auth(roles, project = @project_name, options = {}, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/sq_auth/sq_auth_access.rb', line 74

def with_sq_auth(roles, project = @project_name, options = {}, &block)
  check_session_provider
  binded_self = options[:binding].is_a?(Binding) ? eval("self", options[:binding]) : self
  if sq_auth_filter(roles, project, options)
    binded_self.instance_eval(&block) if block_given?
  else
    when_not_authenticated(roles, project, options)
  end
end