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

Instance Method Details

#cache_current_identity?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/pebblebed/sinatra.rb', line 77

def cache_current_identity?
  settings.respond_to?(:cache_current_identity) && settings.cache_current_identity
end

#current_identityObject



69
70
71
# File 'lib/pebblebed/sinatra.rb', line 69

def current_identity
  current_identity_data['identity']
end

#current_identity_dataObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pebblebed/sinatra.rb', line 45

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?
    memcached = ::Pebblebed.memcached
    cache_key = "identity-data-for-session-#{current_session}"
    @current_identity_data = memcached.get(cache_key)
    if @current_identity_data
      if memcached.respond_to?(:touch)
        memcached.touch(cache_key, IDENTITY_CACHE_TTL)
      end
      return @current_identity_data
    end
    @current_identity_data = pebbles.checkpoint.get("/identities/me")
    if @current_identity_data['identity']
      memcached.set(cache_key, @current_identity_data, IDENTITY_CACHE_TTL)
    end
  else
    @current_identity_data = pebbles.checkpoint.get("/identities/me")
  end
  @current_identity_data
end

#current_profileObject



73
74
75
# File 'lib/pebblebed/sinatra.rb', line 73

def current_profile
  current_identity_data['profile']
end

#current_sessionObject Also known as: checkpoint_session



31
32
33
# File 'lib/pebblebed/sinatra.rb', line 31

def current_session
  params[:session] || request.cookies[::Pebblebed.session_cookie]
end

#has_access_to_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'lib/pebblebed/sinatra.rb', line 97

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



119
120
121
122
123
124
125
126
127
# File 'lib/pebblebed/sinatra.rb', line 119

def limit_offset_collection(collection, options)
  limit = (options[:limit] || 20).to_i
  offset = (options[: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”



14
15
16
17
# File 'lib/pebblebed/sinatra.rb', line 14

def part(partspec, params = {})
  params[:session] ||= current_session
  pebbles.parts.markup(partspec, params)
end

#parts_script_include_tagsObject



19
20
21
22
23
# File 'lib/pebblebed/sinatra.rb', line 19

def parts_script_include_tags
  @script_include_tags ||= pebbles.parts.javascript_urls.map do |url|
    "<script src=\"#{url.to_s}\"></script>"
  end.join
end

#parts_stylesheet_include_tagsObject



25
26
27
28
29
# File 'lib/pebblebed/sinatra.rb', line 25

def parts_stylesheet_include_tags
  @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

#pebblesObject



36
37
38
39
40
41
42
43
# File 'lib/pebblebed/sinatra.rb', line 36

def pebbles
  return @pebbles if @pebbles
  connector_options = {
    :host => ::Pebblebed.host || request.host || ::Pebblebed.default_host,
    :scheme => request.scheme
  }
  @pebbles = ::Pebblebed::Connector.new(checkpoint_session, connector_options)
end

#require_access_to_path(path) ⇒ Object



104
105
106
107
# File 'lib/pebblebed/sinatra.rb', line 104

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



109
110
111
112
113
114
115
116
117
# File 'lib/pebblebed/sinatra.rb', line 109

def require_action_allowed(action, uid, options={})
  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 options[:default])
  halt 403, ":#{action} denied for #{uid} : #{res['reason']}"
end

#require_godObject



87
88
89
90
# File 'lib/pebblebed/sinatra.rb', line 87

def require_god
  require_identity
  halt 403, "Current identity #{current_identity.id} is not god" unless current_identity.god
end

#require_identityObject



81
82
83
84
85
# File 'lib/pebblebed/sinatra.rb', line 81

def require_identity
  unless current_identity
    halt 403, "No current identity."
  end
end

#require_parameters(parameters, *keys) ⇒ Object



92
93
94
95
# File 'lib/pebblebed/sinatra.rb', line 92

def require_parameters(parameters, *keys)
  missing = keys.map(&:to_s) - (parameters ? parameters.keys : [])
  halt 409, "missing parameters: #{missing.join(', ')}" unless missing.empty?
end