Module: Sinatra::Backstage::User::Helper

Included in:
Sinatra::Backstage::User
Defined in:
lib/sinatra/backstage/user/user_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(app) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sinatra/backstage/user/user_helper.rb', line 16

def self.included(app)
	if app.ancestors.include? Sinatra::Base
		app.set(:user_access) do |roles|
			condition do
				settings.site_routes.each do |controller, hash|
					if request.path =~ /^(#{hash['href']}$|#{hash['href']}\/.*$)/
						# puts "-- Backstage::UserHelper :user_access ( controller = #{controller} )"
						# puts "-- Backstage::UserHelper :user_access ( hash = #{hash} )"
						return hash['access'].compare(roles)
					end
				end
			end
		end

		## Hooks
		app.before :user_access => [:anon] do
			# puts "-- Backstage::User before authorized? = #{authorized?}"
			redirect '/' if authorized?
			# settings.site_routes[request.path]['active'] = true if settings.site_routes[request.path]
		end

		# app.after :user_access => [:anon] do
		# 	settings.site_routes[request.path]['active'] = false if settings.site_routes[request.path]
		# end
	end
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/sinatra/backstage/user/user_helper.rb', line 53

def authorized?
	authorized_user.role != :anon
end

#authorized_userObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sinatra/backstage/user/user_helper.rb', line 57

def authorized_user
	if @authorized_user.nil?
		if cookies[:username] && cookies[:session]
			user = settings.user_class.first :username => cookies[:username]
			if user && user.session == cookies[:session]
				return @authorized_user = user
			end
		end
		@authorized_user = settings.user_class.new
	end
	@authorized_user
end

#set_session(user) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/sinatra/backstage/user/user_helper.rb', line 43

def set_session(user)
	if user
		cookies[:username] = user.username
		cookies[:session] = user.session
	else
		cookies[:username] = nil
		cookies[:session] = nil
	end
end