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, project_params, user_params| SqAuthUtils::default_draw_template(uri, project_params, user_params)}
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
15
16
# 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
  @forced_host = nil
  @forced_port = 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

#forced_hostObject

Returns the value of attribute forced_host.



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

def forced_host
  @forced_host
end

#forced_portObject

Returns the value of attribute forced_port.



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

def forced_port
  @forced_port
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



50
51
52
53
54
55
56
57
58
# File 'lib/sq_auth/sq_auth_access.rb', line 50

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.(, form_project_params(callback, roles, project), @session_provider.current_user_params))
  end
end

#check_session_providerObject



45
46
47
48
# File 'lib/sq_auth/sq_auth_access.rb', line 45

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:



18
19
20
21
22
23
24
25
26
27
# File 'lib/sq_auth/sq_auth_access.rb', line 18

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

#current_user_ip=(ip) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/sq_auth/sq_auth_access.rb', line 107

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

#current_user_name=(name) ⇒ Object



115
116
117
# File 'lib/sq_auth/sq_auth_access.rb', line 115

def current_user_name= name
  @session_provider.user.user_name = name
end

#data_when_not_logged_in(&block) ⇒ Object



33
34
35
# File 'lib/sq_auth/sq_auth_access.rb', line 33

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



73
74
75
# File 'lib/sq_auth/sq_auth_access.rb', line 73

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

#draw_when_not_logged_in(&block) ⇒ Object



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

def draw_when_not_logged_in &block
  @draw_proc = block
end

#form_project_params(callback, roles, project) ⇒ Object



64
65
66
# File 'lib/sq_auth/sq_auth_access.rb', line 64

def form_project_params callback, roles, project
  {callback: callback, roles: [*roles], auth_name: project}
end

#get_not_logged_in_proc(type = nil) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/sq_auth/sq_auth_access.rb', line 37

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

#login_toObject



123
124
125
126
127
# File 'lib/sq_auth/sq_auth_access.rb', line 123

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

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



77
78
79
# File 'lib/sq_auth/sq_auth_access.rb', line 77

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

#revoke_session(sqauthsession) ⇒ Object



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

def revoke_session sqauthsession
  @session_provider.delete_session(sqauthsession)
end

#roles_for_current_user(project = @project_name) ⇒ Object



91
92
93
# File 'lib/sq_auth/sq_auth_access.rb', line 91

def roles_for_current_user project = @project_name
  @session_provider.roles_for_current_user project
end

#save_session_for_current_user(sqauthsession) ⇒ Object



95
96
97
# File 'lib/sq_auth/sq_auth_access.rb', line 95

def save_session_for_current_user sqauthsession
  @session_provider.create_session_for_current_user sqauthsession
end

#save_username_for_current_user(username) ⇒ Object



103
104
105
# File 'lib/sq_auth/sq_auth_access.rb', line 103

def save_username_for_current_user username
  @session_provider.username = username
end

#session_for_current_userObject



119
120
121
# File 'lib/sq_auth/sq_auth_access.rb', line 119

def session_for_current_user
  @session_provider.session_for_current_user
end

#set_callback(actual_callback) ⇒ Object



129
130
131
132
133
134
# File 'lib/sq_auth/sq_auth_access.rb', line 129

def set_callback(actual_callback)
  callback_uri = URI.parse(actual_callback)
  callback_uri.port = @forced_port.to_i if @forced_port
  callback_uri.host = @forced_host if @forced_host
  @callback = callback_uri.to_s
end

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



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

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



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

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

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



81
82
83
84
85
86
87
88
89
# File 'lib/sq_auth/sq_auth_access.rb', line 81

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