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

@@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

- (Object) only_his_objects

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



68
69
70
# File 'lib/faalis/concerns/authorizable.rb', line 68

def only_his_objects
  @@only_owner = true
end

- (Boolean) only_his_objects?

Returns:

  • (Boolean)


72
73
74
# File 'lib/faalis/concerns/authorizable.rb', line 72

def only_his_objects?
  @@only_owner
end

- (Object) permission_strings(model)

Return an array of strings representation of permissions



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

- (Object) permissions(*args)

Define permissions using this method



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

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

- (Object) possible_permissions



42
43
44
# File 'lib/faalis/concerns/authorizable.rb', line 42

def possible_permissions
  @@permissions.keys
end