Module: Sinatra::Helpers

Defined in:
lib/sinatra-authentication.rb

Instance Method Summary collapse

Instance Method Details

#current_userObject



205
206
207
208
209
210
211
# File 'lib/sinatra-authentication.rb', line 205

def current_user
  if session[:user]
    User.get(:id => session[:user])
  else
    GuestUser.new
  end
end

#get_view_as_string(filename) ⇒ Object

BECAUSE sinatra 9.1.1 can’t load views from different paths properly



222
223
224
225
226
227
228
229
230
# File 'lib/sinatra-authentication.rb', line 222

def get_view_as_string(filename)
  view = options.sinatra_authentication_view_path + filename
  data = ""
  f = File.open(view, "r")
  f.each_line do |line|
    data += line
  end
  return data
end

#hash_to_query_string(hash) ⇒ Object



190
191
192
# File 'lib/sinatra-authentication.rb', line 190

def hash_to_query_string(hash)
  hash.collect {|k,v| "#{k}=#{v}"}.join('&')
end

#logged_in?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/sinatra-authentication.rb', line 213

def logged_in?
  !!session[:user]
end

#login_requiredObject



194
195
196
197
198
199
200
201
202
203
# File 'lib/sinatra-authentication.rb', line 194

def 
  #not as efficient as checking the session. but this inits the fb_user if they are logged in
  if current_user.class != GuestUser
    return true
  else
    session[:return_to] = request.fullpath
    redirect '/login'
    return false
  end
end


263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/sinatra-authentication.rb', line 263

def render_facebook_connect_link(text = 'Login using facebook', options = {:size => 'small'})
    if options[:size] == 'small'
      size = 'Small'
    elsif options[:size] == 'medium'
      size = 'Medium'
    elsif options[:size] == 'large'
      size = 'Large'
    elsif options[:size] == 'xlarge'
      size = 'BigPun'
    else
      size = 'Small'
    end

    %[<a href="#" onclick="FB.Connect.requireSession(function(){document.location = '/connect';}); return false;" class="fbconnect_login_button FBConnectButton FBConnectButton_#{size}">
        <span id="RES_ID_fb_login_text" class="FBConnectButton_Text">
          #{text}
        </span>
      </a>]
end

#render_login_logout(html_attributes = {:class => ""}) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/sinatra-authentication.rb', line 232

def (html_attributes = {:class => ""})
css_classes = html_attributes.delete(:class)
parameters = ''
html_attributes.each_pair do |attribute, value|
  parameters += "#{attribute}=\"#{value}\" "
end

  result = "<div id='sinatra-authentication-login-logout' >"
  if logged_in?
    logout_parameters = html_attributes
    # a tad janky?
    logout_parameters.delete(:rel)
    result += "<a href='/users/#{current_user.id}/edit' class='#{css_classes} sinatra-authentication-edit' #{parameters}>Edit account</a> "
    if Sinatra.const_defined?('FacebookObject')
      if fb[:user]
        result += "<a href='javascript:FB.Connect.logoutAndRedirect(\"/logout\");' class='#{css_classes} sinatra-authentication-logout' #{logout_parameters}>Logout</a>"
      else
        result += "<a href='/logout' class='#{css_classes} sinatra-authentication-logout' #{logout_parameters}>Logout</a>"
      end
    else
      result += "<a href='/logout' class='#{css_classes} sinatra-authentication-logout' #{logout_parameters}>Logout</a>"
    end
  else
    result += "<a href='/signup' class='#{css_classes} sinatra-authentication-signup' #{parameters}>Signup</a> "
    result += "<a href='/login' class='#{css_classes} sinatra-authentication-login' #{parameters}>Login</a>"
  end

  result += "</div>"
end

#use_layout?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/sinatra-authentication.rb', line 217

def use_layout?
  !request.xhr?
end