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
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'app/controllers/concerns/shoplift_client.rb', line 192 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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'app/controllers/concerns/shoplift_client.rb', line 143 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
174 175 176 |
# File 'app/controllers/concerns/shoplift_client.rb', line 174 def authenticate_user! unless authenticate_user end |
#authenticate_user_or_api! ⇒ Object
178 179 180 181 182 |
# File 'app/controllers/concerns/shoplift_client.rb', line 178 def authenticate_user_or_api! unless authenticate_company!(true) end end |
#client ⇒ Object
275 276 277 278 279 |
# File 'app/controllers/concerns/shoplift_client.rb', line 275 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/controllers/concerns/shoplift_client.rb', line 52 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
281 282 283 284 285 286 287 |
# File 'app/controllers/concerns/shoplift_client.rb', line 281 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
228 229 230 |
# File 'app/controllers/concerns/shoplift_client.rb', line 228 def current_auditor current_user.to_h['email'] end |
#current_company ⇒ Object
238 239 |
# File 'app/controllers/concerns/shoplift_client.rb', line 238 def current_company end |
#current_user ⇒ Object
232 233 234 235 236 |
# File 'app/controllers/concerns/shoplift_client.rb', line 232 def current_user return @current_user if @current_user.present? @current_user end |
#current_user_json ⇒ Object
224 225 226 |
# File 'app/controllers/concerns/shoplift_client.rb', line 224 def current_user_json current_user.to_json end |
#find_company_by_code(code, parameters = {}) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/controllers/concerns/shoplift_client.rb', line 123 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 <<-ERROR.strip_heredoc You have not defined a company, and that is compulsory even if you are not planning to add any additional fields. You do not need to seed it, so following is enough forever: rails g model company code:string; rake db:migrate ERROR end end end |
#get(url, params = {}) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 |
# File 'app/controllers/concerns/shoplift_client.rb', line 259 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
184 185 186 187 188 189 190 |
# File 'app/controllers/concerns/shoplift_client.rb', line 184 def () if request.format.html? else fail ActionController::RoutingError, end end |
#hide_search_for_this_action ⇒ Object
78 79 80 |
# File 'app/controllers/concerns/shoplift_client.rb', line 78 def hide_search_for_this_action @do_hide_search_for_this_action = true end |
#home_link_path ⇒ Object
289 290 291 |
# File 'app/controllers/concerns/shoplift_client.rb', line 289 def home_link_path root_path end |
#local_authlift_redirect_uri ⇒ Object
101 102 103 104 105 106 107 |
# File 'app/controllers/concerns/shoplift_client.rb', line 101 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.
247 248 249 250 251 252 253 254 255 256 257 |
# File 'app/controllers/concerns/shoplift_client.rb', line 247 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
109 110 111 112 113 114 115 116 117 |
# File 'app/controllers/concerns/shoplift_client.rb', line 109 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
119 120 121 |
# File 'app/controllers/concerns/shoplift_client.rb', line 119 def scope [Rails.configuration.settings['authlift_default_scope'], 'public'].compact.join ' ' end |
#search_remote ⇒ Object
82 83 84 |
# File 'app/controllers/concerns/shoplift_client.rb', line 82 def search_remote @search_remote || false end |
#session_cookie ⇒ Object
93 94 95 |
# File 'app/controllers/concerns/shoplift_client.rb', line 93 def session["authlift_session_id"] end |
#session_cookie=(new_value) ⇒ Object
97 98 99 |
# File 'app/controllers/concerns/shoplift_client.rb', line 97 def (new_value) session["authlift_session_id"] = new_value end |
#show_search_for_this_action(params = {}) ⇒ Object
86 87 88 89 90 91 |
# File 'app/controllers/concerns/shoplift_client.rb', line 86 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
271 272 273 |
# File 'app/controllers/concerns/shoplift_client.rb', line 271 def srv @token ||= client.client_credentials.get_token scope: scope end |
#user_signed_in? ⇒ Boolean
241 242 243 |
# File 'app/controllers/concerns/shoplift_client.rb', line 241 def user_signed_in? !current_user.nil? end |