class Adeia::Authorization

Public Instance Methods

authorize!() click to toggle source
# File lib/adeia/authorization.rb, line 8
def authorize!
  token_rights = token_rights(right_name)
  raise LoginRequired if token_rights[:rights].empty? && @user.nil?
  action_rights = @user.nil? ? {} : send("#{right_name}_rights")
  merge_permissions(token_rights, action_rights)
  raise AccessDenied unless @rights.any? && authorize?
end
can?() click to toggle source
# File lib/adeia/authorization.rb, line 31
def can?
  rights? && authorize?
end
check_permissions!() click to toggle source
# File lib/adeia/authorization.rb, line 16
def check_permissions!
  load_permissions
  if !@user && @rights.empty?
    raise LoginRequired
  elsif @rights.empty?
    raise AccessDenied
  end
end
rights?() click to toggle source
# File lib/adeia/authorization.rb, line 25
def rights?
  action_rights = @user.nil? ? {} : send("#{right_name}_rights")
  merge_permissions(token_rights(right_name), action_rights)
  @rights.any?
end

Private Instance Methods

all_entries?() click to toggle source
# File lib/adeia/authorization.rb, line 41
def all_entries?
  @rights.any? { |r| r.permission_type == "all_entries" }
end
authorize?() click to toggle source
# File lib/adeia/authorization.rb, line 37
def authorize?
  all_entries? || on_ownerships? || on_entry?
end
load_permissions() click to toggle source
# File lib/adeia/authorization.rb, line 61
def load_permissions
  merge_permissions(token_rights(right_name), send("#{right_name}_rights"))
end
merge_permissions(collection1, collection2) click to toggle source
# File lib/adeia/authorization.rb, line 65
def merge_permissions(collection1, collection2)
  rights = collection1.merge(collection2) { |key, v1, v2| v1 + v2 }
  @rights, @resource_ids = rights[:rights], rights[:resource_ids]
end
on_entry?() click to toggle source
# File lib/adeia/authorization.rb, line 49
def on_entry?
  @resource && @resource_ids.include?(@resource.id)
end
on_ownerships?() click to toggle source
# File lib/adeia/authorization.rb, line 45
def on_ownerships?
  @user && @resource && @rights.any? { |r| r.permission_type == "on_ownerships" } && @resource.user == @user
end
right_name() click to toggle source
# File lib/adeia/authorization.rb, line 57
def right_name
  right_names.select { |k, v| v.include? @action.to_sym }.keys[0] || :action
end
right_names() click to toggle source
# File lib/adeia/authorization.rb, line 53
def right_names
  {read: [:read, :index, :show], create: [:new, :create], update: [:edit, :update], destroy: [:destroy]} 
end