Module: Faalis::Concerns::Authorizable::ClassMethods

Defined in:
lib/faalis/concerns/authorizable.rb

Overview

Class methods which will add to model by including ‘Faalis::Concerns::Authorizable`

Constant Summary collapse

@@permissions =

Default permission hash

{
  index: nil,
  show: nil,
  create: nil,
  update: nil,
  destroy: nil,
  ownership: nil,
}
@@only_owner =
false

Instance Method Summary collapse

Instance Method Details

#only_his_objectsObject

This force user to have access to resources which is his.



70
71
72
# File 'lib/faalis/concerns/authorizable.rb', line 70

def only_his_objects
  @@only_owner = true
end

#only_his_objects?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/faalis/concerns/authorizable.rb', line 74

def only_his_objects?
  @@only_owner
end

#permission_strings(model) ⇒ Object

Return an array of strings representation of permissions



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/faalis/concerns/authorizable.rb', line 25

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: t("faalis.permission_string", action: key.to_s, model: humanize_name)
    }
  end

  strings
end

#permissions(*args) ⇒ Object

Define permissions using this method



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/faalis/concerns/authorizable.rb', line 50

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

#possible_permissionsObject



45
46
47
# File 'lib/faalis/concerns/authorizable.rb', line 45

def possible_permissions
  @@permissions.keys
end