Module: Sinatra::Pebblebed::Helpers
- Defined in:
- lib/pebblebed/sinatra.rb
Constant Summary collapse
- IDENTITY_CACHE_TTL =
Cache identity for this amount of seconds. TTL will be reset each cache hit, so real TTL might be much longer than this.
7
Instance Method Summary collapse
- #cache_current_identity? ⇒ Boolean
- #current_identity ⇒ Object
- #current_identity_data ⇒ Object
- #current_profile ⇒ Object
- #current_session ⇒ Object (also: #checkpoint_session)
- #has_access_to_path?(path) ⇒ Boolean
- #limit_offset_collection(collection, options) ⇒ Object
-
#part(partspec, params = {}) ⇒ Object
Render the markup for a part.
- #parts_script_include_tags ⇒ Object
- #parts_stylesheet_include_tags ⇒ Object
- #pebbles ⇒ Object
- #require_access_to_path(path) ⇒ Object
- #require_action_allowed(action, uid, options = {}) ⇒ Object
- #require_god ⇒ Object
- #require_identity ⇒ Object
- #require_parameters(parameters, *keys) ⇒ Object
Instance Method Details
#cache_current_identity? ⇒ Boolean
68 69 70 |
# File 'lib/pebblebed/sinatra.rb', line 68 def cache_current_identity? settings.respond_to?(:cache_current_identity) && settings.cache_current_identity end |
#current_identity ⇒ Object
60 61 62 |
# File 'lib/pebblebed/sinatra.rb', line 60 def current_identity current_identity_data['identity'] end |
#current_identity_data ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pebblebed/sinatra.rb', line 39 def current_identity_data return DeepStruct.wrap({}) unless current_session return @current_identity_data if @current_identity_data_fetched @current_identity_data_fetched = true if cache_current_identity? cache_key = "identity-data-for-session-#{current_session}" @current_identity_data = ::Pebblebed.memcached.get(cache_key) if @current_identity_data # Reinstate this line when memcached version >= 1.4.8 # ::Pebblebed.memcached.touch(cache_key, IDENTITY_CACHE_TTL) return @current_identity_data end @current_identity_data = pebbles.checkpoint.get("/identities/me") # Cache identity only if there is an identity in the data returned from checkpoint ::Pebblebed.memcached.set(cache_key, @current_identity_data, IDENTITY_CACHE_TTL) if @current_identity_data['identity'] else @current_identity_data = pebbles.checkpoint.get("/identities/me") end @current_identity_data end |
#current_profile ⇒ Object
64 65 66 |
# File 'lib/pebblebed/sinatra.rb', line 64 def current_profile current_identity_data['profile'] end |
#current_session ⇒ Object Also known as: checkpoint_session
30 31 32 |
# File 'lib/pebblebed/sinatra.rb', line 30 def current_session params[:session] || request.[::Pebblebed.] end |
#has_access_to_path?(path) ⇒ Boolean
88 89 90 91 92 93 |
# File 'lib/pebblebed/sinatra.rb', line 88 def has_access_to_path?(path) return false unless current_identity return true if current_identity.god and path.split(".")[0] == current_identity.realm res = pebbles.checkpoint.get("/identities/#{current_identity.id}/access_to/#{path}") res['access'] and res['access']['granted'] == true end |
#limit_offset_collection(collection, options) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/pebblebed/sinatra.rb', line 110 def limit_offset_collection(collection, ) limit = ([:limit] || 20).to_i offset = ([:offset] || 0).to_i collection = collection.limit(limit+1).offset(offset) last_page = (collection.size <= limit) = {:limit => limit, :offset => offset, :last_page => last_page} collection = collection[0..limit-1] [collection, ] end |
#part(partspec, params = {}) ⇒ Object
Render the markup for a part. A partspec takes the form “<kit>.<partname>”, e.g. “base.post”
13 14 15 16 |
# File 'lib/pebblebed/sinatra.rb', line 13 def part(partspec, params = {}) params[:session] ||= current_session pebbles.parts.markup(partspec, params) end |
#parts_script_include_tags ⇒ Object
18 19 20 21 22 |
# File 'lib/pebblebed/sinatra.rb', line 18 def @script_include_tags ||= pebbles.parts.javascript_urls.map do |url| "<script src=\"#{url.to_s}\"></script>" end.join end |
#parts_stylesheet_include_tags ⇒ Object
24 25 26 27 28 |
# File 'lib/pebblebed/sinatra.rb', line 24 def @stylesheet_include_tags ||= pebbles.parts.stylesheet_urls.map do |url| "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"#{url.to_s}\">" end.join end |
#pebbles ⇒ Object
35 36 37 |
# File 'lib/pebblebed/sinatra.rb', line 35 def pebbles @pebbles ||= ::Pebblebed::Connector.new(checkpoint_session, :host => ::Pebblebed.host || request.host) end |
#require_access_to_path(path) ⇒ Object
95 96 97 98 |
# File 'lib/pebblebed/sinatra.rb', line 95 def require_access_to_path(path) require_identity halt 403, "Access denied." unless has_access_to_path?(path) end |
#require_action_allowed(action, uid, options = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/pebblebed/sinatra.rb', line 100 def require_action_allowed(action, uid, ={}) require_identity uid = ::Pebblebed::Uid.new(uid) if uid.is_a?(String) return if current_identity.god and uid.path.split(".")[0] == current_identity.realm res = pebbles.checkpoint.post("/callbacks/allowed/#{action}/#{uid}") return res['allowed'] if res['allowed'] == true or (res['allowed'] == "default" and [:default]) halt 403, ":#{action} denied for #{uid} : #{res['reason']}" end |
#require_god ⇒ Object
78 79 80 81 |
# File 'lib/pebblebed/sinatra.rb', line 78 def require_god require_identity halt 403, "Current identity #{current_identity.id} is not god" unless current_identity.god end |
#require_identity ⇒ Object
72 73 74 75 76 |
# File 'lib/pebblebed/sinatra.rb', line 72 def require_identity unless current_identity halt 403, "No current identity." end end |
#require_parameters(parameters, *keys) ⇒ Object
83 84 85 86 |
# File 'lib/pebblebed/sinatra.rb', line 83 def require_parameters(parameters, *keys) missing = keys.map(&:to_s) - (parameters ? parameters.keys : []) halt 409, "missing parameters: #{missing.join(', ')}" unless missing.empty? end |