Sha256: 1044361f16f614f184934f12c5479ff41af3a549c7b277de4515b4d7a320a9f7

Contents?: true

Size: 1.89 KB

Versions: 9

Compression:

Stored size: 1.89 KB

Contents

module Typus
  module Orm
    module ActiveRecord
      module User
        module InstanceMethods

          def to_label
            email
          end

          def resources
            Typus::Configuration.roles[role.to_s].compact
          end

          def models
            Typus.models.delete_if { |m| cannot?('read', m) }
          end

          def applications
            Typus.applications.delete_if { |a| application(a).empty? }
          end

          def application(name)
            Typus.application(name).delete_if { |r| !resources.keys.include?(r) }
          end

          def can?(action, resource, options = {})
            resource = resource.model_name if resource.is_a?(Class)

            return false if !resources.include?(resource)
            return true if resources[resource].include?("all")

            action = options[:special] ? action : action.acl_action_mapper

            resources[resource].extract_settings.include?(action)
          end

          def cannot?(*args)
            !can?(*args)
          end

          def is_root?
            role == Typus.master_role
          end

          def is_not_root?
            !is_root?
          end

          def active?
            status && Typus::Configuration.roles.has_key?(role)
          end

          def locale
            (preferences && preferences[:locale]) ? preferences[:locale] : ::I18n.default_locale
          end

          def locale=(locale)
            self.preferences ||= {}
            self.preferences[:locale] = locale
          end

          def owns?(resource)
            id == resource.send(Typus.user_foreign_key)
          end

          def set_token
            token = "#{SecureRandom.hex(6)}-#{SecureRandom.hex(6)}"
            token.encode!('UTF-8') if token.respond_to?(:encode)
            self.token = token
          end
          protected :set_token

        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
typus-3.1.10 lib/typus/orm/active_record/user/instance_methods.rb
typus-3.1.9 lib/typus/orm/active_record/user/instance_methods.rb
typus-3.1.8 lib/typus/orm/active_record/user/instance_methods.rb
typus-3.1.7 lib/typus/orm/active_record/user/instance_methods.rb
typus-3.1.6 lib/typus/orm/active_record/user/instance_methods.rb
typus-3.1.5 lib/typus/orm/active_record/user/instance_methods.rb
typus-3.1.4 lib/typus/orm/active_record/user/instance_methods.rb
typus-3.1.3 lib/typus/orm/active_record/user/instance_methods.rb
typus-3.1.2 lib/typus/orm/active_record/user/instance_methods.rb