Module: Maybee::AuthorizationObject

Extended by:
ActiveSupport::Concern
Defined in:
lib/maybee/authorization_object.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#allow?(access, subject = authorization_subject) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/maybee/authorization_object.rb', line 39

def allow?(access, subject = authorization_subject)
  authorizations = self.class.authorizations[access] or return(false)
  authorizations.any? { |authorization| authorization.granted?(self, subject) }
end

#authorize?(access, subject = authorization_subject) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
# File 'lib/maybee/authorization_object.rb', line 44

def authorize?(access, subject = authorization_subject)
  errors.clear
  return true if allow?(access, subject)
  defaults = (self.class.lookup_ancestors + [ActiveRecord::Base]).map do |klass|
    :"#{self.class.i18n_scope}.authorizations.#{klass.model_name.i18n_key}.#{access}"
  end
  key = defaults.shift
  errors.add(:base, :not_authorized, :access => I18n.translate(key, :default => defaults))
  false
end