Sha256: 1cdaf50c9b5595248c4621edf1dcec336c979eda9ca9c90a7c7580e15b8aaa75

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

module Faalis
  module Permissions
    extend ActiveSupport::Concern


    module ClassMethods

      # Default permission hash
      @@permissions = {
        :read => nil,
        :update => nil,
        :create => nil,
        :destory => 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
faalis-0.11.1 lib/faalis/permissions.rb