Module: Beta::AccessHelpers

Defined in:
lib/beta/access_helpers.rb

Overview

AccessHelpers encapsulates the functionality for whitespacing. This will usually be included in ApplicationController and acccessed as before_filters

Instance Method Summary collapse

Instance Method Details

#current_user_on_whitelist?Boolean

Uses ‘is_whitelisted?` with the value of `current_user`

Returns:

  • (Boolean)


33
34
35
# File 'lib/beta/access_helpers.rb', line 33

def current_user_on_whitelist?
  is_whitelisted? current_user
end

#is_whitelisted?(user) ⇒ Boolean

Uses redis whitelist to detect if the given user has beta access

Parameters:

  • the (User)

    user to be checked against the list

Returns:

  • (Boolean)


28
29
30
# File 'lib/beta/access_helpers.rb', line 28

def is_whitelisted? user
  Beta.redis.sismember("#{Beta.namespace}:#{::Rails.env}:beta", user.try(Beta.uid))
end

#whitelist(redirection = nil) ⇒ Object

Uses redis whitelist and cookies to detect beta access. Redirects on failure.

Parameters:

  • Location (String, nil)

    to redirect to on failure. Defaults to system config of Beta URL



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/beta/access_helpers.rb', line 8

def whitelist redirection = nil
  return true unless Beta.environments.include?(::Rails.env)

  if cookies.signed["#{Beta.namespace}-beta"] == "#{Beta.namespace}-beta-#{request.remote_addr}"
    return true
  end

  return false unless authenticate

  unless current_user_on_whitelist?
    redirect_to(redirection || Beta.redirect_url)
    return
  end

  cookies.signed["#{Beta.namespace}-beta"] = "#{Beta.namespace}-beta-#{request.remote_addr}"
end