use token in ability checks

This commit is contained in:
Chris Flipse 2018-06-08 16:58:49 -04:00
parent b16aa11f65
commit 6da3ece683
3 changed files with 18 additions and 1 deletions

View file

@ -473,6 +473,10 @@ class ApplicationController < ActionController::Base
# ... # ...
end end
def current_ability
@current_ability ||= Ability.new(current_user, current_token)
end
private private
# extract authorisation credentials from headers, returns user = nil if none # extract authorisation credentials from headers, returns user = nil if none

View file

@ -1,7 +1,9 @@
# frozen_string_literal: true
class Ability class Ability
include CanCan::Ability include CanCan::Ability
def initialize(user) def initialize(user, token)
can :index, :site can :index, :site
can [:permalink, :edit, :help, :fixthemap, :offline, :export, :about, :preview, :copyright, :key, :id, :welcome], :site can [:permalink, :edit, :help, :fixthemap, :offline, :export, :about, :preview, :copyright, :key, :id, :welcome], :site
@ -35,4 +37,8 @@ class Ability
# See the wiki for details: # See the wiki for details:
# https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
end end
def has_capability?(token, cap)
token && token.read_attribute(cap)
end
end end

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
require "test_helper"
class AbilityTest < ActiveSupport::TestCase
end