Module: ShopliftClient
- Extended by:
- ActiveSupport::Concern
- Includes:
- Shopapp3
- Included in:
- ApiController, AuthController, UserAuthenticatedController, UserAuthenticatedOrApiController
- Defined in:
- app/controllers/concerns/shoplift_client.rb
Instance Method Summary collapse
- #authenticate_company!(soft = false) ⇒ Object
- #authenticate_user ⇒ Object
- #authenticate_user! ⇒ Object
- #authenticate_user_or_api! ⇒ Object
- #client ⇒ Object
- #company_info(company_id = nil) ⇒ Object
- #company_logo_path_defined ⇒ Object
- #current_auditor ⇒ Object
- #current_company ⇒ Object
- #current_user ⇒ Object
- #current_user_json ⇒ Object
- #find_company_by_code(code, parameters = {}) ⇒ Object
- #get(url, params = {}) ⇒ Object
- #handle_not_authorized(message) ⇒ Object
- #hide_search_for_this_action ⇒ Object
- #home_link_path ⇒ Object
- #local_authlift_redirect_uri ⇒ Object
-
#post(url, params) ⇒ Object
To create/update a model, params must be of form { model_name: { attr1: value1, attr2: value2 } } and attr1, attr2 must be in the list of allowed params the Rails way.
- #redirect_unauthorized ⇒ Object
- #scope ⇒ Object
- #search_remote ⇒ Object
- #session_cookie ⇒ Object
- #session_cookie=(new_value) ⇒ Object
- #show_search_for_this_action(params = {}) ⇒ Object
- #srv ⇒ Object
- #user_signed_in? ⇒ Boolean
Methods included from Shopapp3
Instance Method Details
#authenticate_company!(soft = false) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'app/controllers/concerns/shoplift_client.rb', line 184 def authenticate_company!(soft = false) return true if authenticate_user @api_key = if params['key'].present? params['key'].match(/[0-9a-f]+/).to_s elsif request.headers['AUTHORIZATION'].present? && !request.headers['AUTHORIZATION'].include?('Basic') request.headers['AUTHORIZATION'].gsub(/^Bearer ?/, '') else Rails.configuration.settings['authlift_default_app_key'] end if @api_key.blank? return false if soft 'Authentication token missing' end response = srv.post 'auth/api_key', body: { api_key: api_key, requested_action: "#{self.controller_name}##{self.action_name}" } if response.blank? return false if soft 'Request not authorized' end @authentication = JSON.parse response.body find_company_by_code authentication['company'] true end |
#authenticate_user ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'app/controllers/concerns/shoplift_client.rb', line 135 def authenticate_user if .present? @token = OAuth2::AccessToken.new client, , scope: scope begin x = srv.get '/api/users/profile' @current_user_json_hash = @current_user = JSON.parse x.response.body unless @current_user['scopes'].is_a? String user_scopes = @current_user['scopes'] else user_scopes = JSON.parse @current_user['scopes'] end unless user_scopes.include? 'admin' (self.class.required_scopes || []).each do |required_scope| unless user_scopes.include? required_scope render(file: 'shopapp/403.html', status: 403, layout: false, locals: { missing_scope: required_scope }) return false end end end find_company_by_code current_user['company']['code'], name: current_user['company']['name'], logo_code: current_user['company']['logo_code'] rescue OAuth2::Error return false end else return false end true end |
#authenticate_user! ⇒ Object
166 167 168 |
# File 'app/controllers/concerns/shoplift_client.rb', line 166 def authenticate_user! unless authenticate_user end |
#authenticate_user_or_api! ⇒ Object
170 171 172 173 174 |
# File 'app/controllers/concerns/shoplift_client.rb', line 170 def authenticate_user_or_api! unless authenticate_company!(true) end end |
#client ⇒ Object
267 268 269 270 271 |
# File 'app/controllers/concerns/shoplift_client.rb', line 267 def client @oauth ||= OAuth2::Client.new Rails.configuration.settings['authlift_app_id'], Rails.configuration.settings['authlift_app_secret'], site: Rails.configuration.settings['authlift_url'] end |
#company_info(company_id = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/controllers/concerns/shoplift_client.rb', line 44 def company_info(company_id = nil) @company_info ||= {} return @company_info[company_id] if @company_info[company_id].present? @company_info[company_id] = JSON.parse srv.get(['/api/users/company_info', company_id].compact.join '/').body ["clients", "suppliers"].each do |partner_type| @company_info[company_id][partner_type].each do |partner| partner[:company] = Company.find_or_create_by! code: partner['code'] partner[:company].name = partner['name'] partner[:company].info ||= {} # Very ugly bugfix: investigate how comes this is string in the first place if partner[:company].info.is_a? String begin partner[:company].info = JSON.parse(partner[:company].info) rescue end end partner[:company].info['company_info'] = partner['info'] partner[:company].save! end end @company_info[company_id] end |
#company_logo_path_defined ⇒ Object
273 274 275 276 277 278 279 |
# File 'app/controllers/concerns/shoplift_client.rb', line 273 def company_logo_path_defined if defined? self.company_logo_path company_logo_path else "https://media.shoplift.fi/company_logos/#{@current_user_json_hash['company']['logo_code']}_company_logo_24.png" end end |
#current_auditor ⇒ Object
220 221 222 |
# File 'app/controllers/concerns/shoplift_client.rb', line 220 def current_auditor current_user.to_h['email'] end |
#current_company ⇒ Object
230 231 |
# File 'app/controllers/concerns/shoplift_client.rb', line 230 def current_company end |
#current_user ⇒ Object
224 225 226 227 228 |
# File 'app/controllers/concerns/shoplift_client.rb', line 224 def current_user return @current_user if @current_user.present? @current_user end |
#current_user_json ⇒ Object
216 217 218 |
# File 'app/controllers/concerns/shoplift_client.rb', line 216 def current_user_json current_user.to_json end |
#find_company_by_code(code, parameters = {}) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/controllers/concerns/shoplift_client.rb', line 115 def find_company_by_code(code, parameters = {}) begin @current_company ||= Company.find_or_create_by! code: code do |new_company| fail if parameters.empty? new_company.name = parameters[:name] new_company.info = parameters.to_json end rescue ActiveRecord::StatementInvalid if $!.cause.is_a? PG::UndefinedTable fail " You have not defined a company, and that is compulsory even if\n you are not planning to add any additional fields. You do not need to\n seed it, so following is enough forever:\n\n rails g model company code:string; rake db:migrate\n ERROR\n end\n end\nend\n".strip_heredoc |
#get(url, params = {}) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 |
# File 'app/controllers/concerns/shoplift_client.rb', line 251 def get(url, params = {}) puts 'co_cli: get' puts "url: #{url}" puts "params: #{params}" response = srv.request(:get, url, body: params) JSON.parse(response.body) rescue OAuth2::Error raise "Server fault, could not perform post to #{srv.client.site}#{url}" rescue raise "Unknown error, could not perform post to #{srv.client.site}#{url}" end |
#handle_not_authorized(message) ⇒ Object
176 177 178 179 180 181 182 |
# File 'app/controllers/concerns/shoplift_client.rb', line 176 def () if request.format.html? else fail ActionController::RoutingError, end end |
#hide_search_for_this_action ⇒ Object
70 71 72 |
# File 'app/controllers/concerns/shoplift_client.rb', line 70 def hide_search_for_this_action @do_hide_search_for_this_action = true end |
#home_link_path ⇒ Object
281 282 283 |
# File 'app/controllers/concerns/shoplift_client.rb', line 281 def home_link_path root_path end |
#local_authlift_redirect_uri ⇒ Object
93 94 95 96 97 98 99 |
# File 'app/controllers/concerns/shoplift_client.rb', line 93 def local_authlift_redirect_uri if respond_to? :app_authlift_redirect_uri app_authlift_redirect_uri else Rails.configuration.settings['authlift_redirect_uri'] end end |
#post(url, params) ⇒ Object
To create/update a model, params must be of form { model_name: { attr1: value1, attr2: value2 } } and attr1, attr2 must be in the list of allowed params the Rails way.
239 240 241 242 243 244 245 246 247 248 249 |
# File 'app/controllers/concerns/shoplift_client.rb', line 239 def post(url, params) puts 'co_cli: post' puts "url: #{url}" puts "params: #{params}" response = srv.request(:post, url, body: params) JSON.parse(response.body) rescue OAuth2::Error raise "Server fault, could not perform post to #{srv.client.site}#{url}" rescue raise "Unknown error, could not perform post to #{srv.client.site}#{url}" end |
#redirect_unauthorized ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'app/controllers/concerns/shoplift_client.rb', line 101 def return if performed? session.clear session[:previous_url] = request.fullpath redirect_to client.auth_code.( redirect_uri: local_authlift_redirect_uri, scope: scope) end |
#scope ⇒ Object
111 112 113 |
# File 'app/controllers/concerns/shoplift_client.rb', line 111 def scope [Rails.configuration.settings['authlift_default_scope'], 'public'].compact.join ' ' end |
#search_remote ⇒ Object
74 75 76 |
# File 'app/controllers/concerns/shoplift_client.rb', line 74 def search_remote @search_remote || false end |
#session_cookie ⇒ Object
85 86 87 |
# File 'app/controllers/concerns/shoplift_client.rb', line 85 def session["authlift_session_id"] end |
#session_cookie=(new_value) ⇒ Object
89 90 91 |
# File 'app/controllers/concerns/shoplift_client.rb', line 89 def (new_value) session["authlift_session_id"] = new_value end |
#show_search_for_this_action(params = {}) ⇒ Object
78 79 80 81 82 83 |
# File 'app/controllers/concerns/shoplift_client.rb', line 78 def show_search_for_this_action(params = {}) @do_hide_search_for_this_action = false @search_placeholder = params[:placeholder] if params[:placeholder].present? @search_path = params[:search_path] if params[:search_path].present? @search_remote = params[:search_remote] if params[:search_remote].present? end |
#srv ⇒ Object
263 264 265 |
# File 'app/controllers/concerns/shoplift_client.rb', line 263 def srv @token ||= client.client_credentials.get_token scope: scope end |
#user_signed_in? ⇒ Boolean
233 234 235 |
# File 'app/controllers/concerns/shoplift_client.rb', line 233 def user_signed_in? !current_user.nil? end |