Sha256: a4b514bdd0740973396e2b4d30a21919d3ecdd4d722146c2d3874b4ba9e25bc7

Contents?: true

Size: 830 Bytes

Versions: 7

Compression:

Stored size: 830 Bytes

Contents

module Outpost
  module Model
    module Authorization
      extend ActiveSupport::Concern

      included do
        has_many :user_permissions
        has_many :permissions, through: :user_permissions
      end

      # Check if a user can manage the passed-in resource(s)
      #
      # If multiple resources are passed in, a user must be
      # allowed to manage ALL of them in order for this to
      # return true.
      #
      # Constants must be passed in.
      #
      def can_manage?(*resources)
        self.is_superuser? or (allowed_resources & resources) == resources
      end

      def allowed_resources
        @allowed_resources ||= begin
          p = self.is_superuser? ? Permission.all : self.permissions
          p.map { |p| p.resource.safe_constantize }.compact
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
outpost-cms-0.1.4 lib/outpost/model/authorization.rb
outpost-cms-0.1.3 lib/outpost/model/authorization.rb
outpost-cms-0.1.2 lib/outpost/model/authorization.rb
outpost-cms-0.1.1 lib/outpost/model/authorization.rb
outpost-cms-0.1.0 lib/outpost/model/authorization.rb
outpost-cms-0.0.5 lib/outpost/model/authorization.rb
outpost-cms-0.0.4 lib/outpost/model/authorization.rb