Sha256: 3e2a1644186cf57c56f9ea518fc3c82369db980f9bc3338df9e3ea485e9da4ae

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

# TODO: This module should be removed
module Faalis
  module Concerns
    # Authorizable modules of Faalis which each resource model should
    # includes this concern. Without this concern, models can not be authorized
    module Authorizable
      extend ActiveSupport::Concern

      # Class methods which will add to model by including
      # `Faalis::Concerns::Authorizable`
      module ClassMethods
        # Default permission hash
        @@permissions = {
          read: nil,
          update: nil,
          create: nil,
          destroy: nil,
        }

        @@only_owner = false

        # Return an array of strings representation of permissions
        def permission_strings(model)
          strings = []
          model_name = model.to_s
          humanize_name = ActiveModel::Name.new(model).human
          if model.respond_to? :model_name
            model_name = model.model_name
            humanize_name = model_name.human
          end
          @@permissions.each do |key, value|
            strings << {
              name: "#{key}|#{model_name}",
              string: _("can %s %s") % [_(key.to_s), humanize_name]
            }
          end
          strings
        end

        def possible_permissions
          @@permissions.keys
        end

        # Define permissions using this method
        def permissions(*args)

          args.each do |permission|
            if permission.class == Symbol
              if not @@permissions.include? permission
                @@permission[permission] = nil

              elsif permission.class == Hash

                permission.each do |key, value|
                  @@permissions[key.to_sym] = value
                end

              end
            end


          end
        end

        # This force user to have access to resources which is his.
        def only_his_objects
          @@only_owner = true
        end

        def only_his_objects?
          @@only_owner
        end

      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
faalis-1.0.0 lib/faalis/concerns/authorizable.rb
faalis-1.0.0.alpha4 lib/faalis/concerns/authorizable.rb
faalis-1.0.0.alpha3 lib/faalis/concerns/authorizable.rb
faalis-1.0.0.alpha2 lib/faalis/concerns/authorizable.rb