Sha256: ece0bb6da1a2d6e5ff99c82f8d38d7b7250fbe7fc22f35234dbae0001c9fbd49

Contents?: true

Size: 1.12 KB

Versions: 34

Compression:

Stored size: 1.12 KB

Contents

module Workarea
  module Authorization
    extend ActiveSupport::Concern

    module ClassMethods
      def required_permissions(*values)
        values = @required_permissions if values.blank?
        @required_permissions ||= values || []
      end

      def reset_permissions!
        @required_permissions = nil
      end
    end

    def require_admin
      unless current_user.try(:admin?)
        flash[:error] = 'You are not authorized to perform this action'
        redirect_to storefront.root_path
        return false
      end
    end

    def required_permissions
      self.class.required_permissions
    end

    def check_authorization
      return if current_user.blank? || current_user.super_admin?
      return if required_permissions.blank?

      unauthorized_user and return false unless authorized?
    end

    def authorized?
      current_user.admin? && Array(required_permissions).all? do |area|
        current_user.send("#{area}_access?")
      end
    end

    def unauthorized_user
      flash[:error] = 'You are not authorized to access this area'
      redirect_back fallback_location: root_path
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-core-3.4.25 app/controllers/workarea/authorization.rb
workarea-core-3.4.24 app/controllers/workarea/authorization.rb
workarea-core-3.4.23 app/controllers/workarea/authorization.rb
workarea-core-3.4.22 app/controllers/workarea/authorization.rb
workarea-core-3.4.21 app/controllers/workarea/authorization.rb
workarea-core-3.4.20 app/controllers/workarea/authorization.rb
workarea-core-3.4.19 app/controllers/workarea/authorization.rb
workarea-core-3.4.18 app/controllers/workarea/authorization.rb
workarea-core-3.4.17 app/controllers/workarea/authorization.rb
workarea-core-3.4.16 app/controllers/workarea/authorization.rb
workarea-core-3.4.15 app/controllers/workarea/authorization.rb
workarea-core-3.4.14 app/controllers/workarea/authorization.rb
workarea-core-3.4.13 app/controllers/workarea/authorization.rb
workarea-core-3.4.12 app/controllers/workarea/authorization.rb