Module: SocialSnippet::RegistryCore::ConfigHelpers

Defined in:
lib/social_snippet/registry_core/config_helpers.rb

Instance Method Summary collapse

Instance Method Details

#sspm_enable_json_paramsObject



21
22
23
24
25
26
27
# File 'lib/social_snippet/registry_core/config_helpers.rb', line 21

def sspm_enable_json_params
  logger.info "Enable JSON Params: #{self}"

  use ::Rack::Parser, :parsers => {
    "application/json" => proc { |data| JSON.parse data },
  }
end

#sspm_enable_omniauthObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/social_snippet/registry_core/config_helpers.rb', line 48

def sspm_enable_omniauth
  return if @sspm_enable_omniauth_visited
  @sspm_enable_omniauth_visited = true

  if settings.sspm_session
    logger.info "Enable GitHub Authentication: #{self}"

    use OmniAuth::Builder do
      provider(
        :github,
        ENV["SSPM_GITHUB_CLIENT_ID"],
        ENV["SSPM_GITHUB_CLIENT_SECRET"],
        {
          :provider_ignores_state => true, # TODO: re-check
        }
      )
    end
  end
end

#sspm_enable_sessionObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/social_snippet/registry_core/config_helpers.rb', line 68

def sspm_enable_session
  if settings.sspm_session
    logger.info "Enable Session: #{self}"

    set :protection, false
    set :protect_from_csrf, false
    disable :sessions

    if ENV["SSPM_MEMCACHED_USERNAME"].nil?
      memcached_client = Dalli::Client.new(ENV["SSPM_MEMCACHED_HOST"])
    else
      memcached_client = Dalli::Client.new(
        ENV["SSPM_MEMCACHED_HOST"],
        username: ENV["SSPM_MEMCACHED_USERNAME"],
        password: ENV["SSPM_MEMCACHED_PASSWORD"],
      )
    end
    use ::Rack::Session::Dalli, {
      :cache => memcached_client,
      :expire_after => 14 * 24 * 3600,
    }

    use ::Rack::Protection
    use ::Rack::Protection::AuthenticityToken, :authenticity_param => '_csrf_token'
  end
end

#sspm_enable_trackerObject

call from padrino-app



13
14
15
16
17
18
19
# File 'lib/social_snippet/registry_core/config_helpers.rb', line 13

def sspm_enable_tracker
  unless ENV["SSPM_GOOGLE_ANALYTICS"].nil?
    use ::Rack::Tracker do
      handler :google_analytics, { tracker: ENV["SSPM_GOOGLE_ANALYTICS"] }
    end
  end
end

#sspm_enable_user_access_controlObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/social_snippet/registry_core/config_helpers.rb', line 29

def sspm_enable_user_access_control
  if settings.sspm_session
    logger.info "Enable User Access Control: #{self}"

    register ::Padrino::Admin::AccessControl
    set :admin_model, "::SocialSnippet::RegistryCore::Models::UserAccount"
    set :login_page, :login
    enable :authentication
    enable :store_location

    # check session
    before do
      if session[:user] && ( not logged_in? )
         session[:user]
      end
    end
  end
end