summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2014-04-17 19:27:47 +0200
committerAzul <azul@leap.se>2014-04-17 19:27:47 +0200
commit7a9ece43bd61246b450471ed6bb1089570321e38 (patch)
treea20362ee5512e160498902ef7c0a094b3135201d /app
parent614745c84cab37dd03f2bd8f06160fd01c7fabdb (diff)
make use of the UnauthorizedUser
Null Pattern for current_user - use it to get rid of some conditionals
Diffstat (limited to 'app')
-rw-r--r--app/controllers/v1/certs_controller.rb44
-rw-r--r--app/controllers/v1/messages_controller.rb5
-rw-r--r--app/models/service_level.rb14
-rw-r--r--app/models/unauthenticated_user.rb20
4 files changed, 39 insertions, 44 deletions
diff --git a/app/controllers/v1/certs_controller.rb b/app/controllers/v1/certs_controller.rb
index 64cfa7f..580c90c 100644
--- a/app/controllers/v1/certs_controller.rb
+++ b/app/controllers/v1/certs_controller.rb
@@ -1,50 +1,20 @@
class V1::CertsController < ApplicationController
- before_filter :require_login, :unless => :anonymous_certs_allowed?
+ before_filter :require_eip_access
# GET /cert
def show
- @cert = ClientCertificate.new(:prefix => certificate_prefix)
+ @cert = ClientCertificate.new(:prefix => service_level.cert_prefix)
render text: @cert.to_s, content_type: 'text/plain'
end
protected
- def anonymous_certs_allowed?
- APP_CONFIG[:allow_anonymous_certs]
+ def require_eip_access
+ access_denied unless service_level.provides?(:eip)
end
- #
- # this is some temporary logic until we store the service level in the user db.
- #
- # better logic might look like this:
- #
- # if logged_in?
- # service_level = user.service_level
- # elsif allow_anonymous?
- # service_level = service_levels[:anonymous]
- # else
- # service_level = nil
- # end
- #
- # if service_level.bandwidth == 'limited' && allow_limited?
- # prefix = limited
- # elsif allow_unlimited?
- # prefix = unlimited
- # else
- # prefix = nil
- # end
- #
- def certificate_prefix
- if logged_in?
- if APP_CONFIG[:allow_unlimited_certs]
- APP_CONFIG[:unlimited_cert_prefix]
- elsif APP_CONFIG[:allow_limited_certs]
- APP_CONFIG[:limited_cert_prefix]
- end
- elsif !APP_CONFIG[:allow_limited_certs]
- APP_CONFIG[:unlimited_cert_prefix]
- else
- APP_CONFIG[:limited_cert_prefix]
- end
+
+ def service_level
+ current_user.effective_service_level
end
end
diff --git a/app/controllers/v1/messages_controller.rb b/app/controllers/v1/messages_controller.rb
index f71d0f1..85156b7 100644
--- a/app/controllers/v1/messages_controller.rb
+++ b/app/controllers/v1/messages_controller.rb
@@ -7,12 +7,11 @@ module V1
respond_to :json
def index
- render json: (current_user ? current_user.messages : [] )
+ render json: current_user.messages
end
def update
- message = Message.find(params[:id])
- if (message and current_user)
+ if message = Message.find(params[:id])
message.mark_as_read_by(current_user)
message.save
render json: true
diff --git a/app/models/service_level.rb b/app/models/service_level.rb
index 31a713b..d0bd9b3 100644
--- a/app/models/service_level.rb
+++ b/app/models/service_level.rb
@@ -13,8 +13,20 @@ class ServiceLevel
end
def config_hash
- APP_CONFIG[:service_levels][@id]
+ @config_hash || APP_CONFIG[:service_levels][@id].with_indifferent_access
end
delegate :to_json, to: :config_hash
+
+ def provides?(service)
+ services.include? service.to_s
+ end
+
+ def services
+ config_hash[:services] || []
+ end
+
+ def cert_prefix
+ config_hash[:cert_prefix]
+ end
end
diff --git a/app/models/unauthenticated_user.rb b/app/models/unauthenticated_user.rb
index ba6470a..7845a6f 100644
--- a/app/models/unauthenticated_user.rb
+++ b/app/models/unauthenticated_user.rb
@@ -1,13 +1,27 @@
# The nil object for the user class
class UnauthenticatedUser < Object
- # will probably want something here to return service level as APP_CONFIG[:service_levels][0] but not sure how will be accessing.
+ def effective_service_level
+ ServiceLevel.new id: APP_CONFIG[:unauthenticated_service_level]
+ end
def is_admin?
false
end
- def effective_service_level
- ServiceLevel.new id: APP_CONFIG[:unauthenticated_service_level]
+ def id
+ nil
+ end
+
+ def email_address
+ nil
+ end
+
+ def login
+ nil
+ end
+
+ def messages
+ []
end
end