Module: Permisi::PermissionUtil

Defined in:
lib/permisi/permission_util.rb

Defined Under Namespace

Classes: InvalidNamespace, Serializer

Class Method Summary collapse

Class Method Details

.allows?(hash, action_path) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/permisi/permission_util.rb', line 8

def allows?(hash, action_path)
  return false unless hash.is_a?(Hash)

  action_path_arr = action_path.split(".")
  begin
    !Permisi.config.default_permissions.dig(*action_path_arr).nil?
  rescue StandardError
    false
  end &&
    hash.dig(*action_path_arr) == true
end

.sanitize_permissions(permission_hash) ⇒ Object



64
65
66
# File 'lib/permisi/permission_util.rb', line 64

def sanitize_permissions(permission_hash)
  __deeply_sanitize_permissions(permission_hash, template: Permisi.config.default_permissions)
end

.transform_namespace(namespace, current_path: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/permisi/permission_util.rb', line 20

def transform_namespace(namespace, current_path: nil)
  HashWithIndifferentAccess.new.tap do |transformed|
    namespace.each_pair do |key, value|
      if !value.is_a? Array
        raise InvalidNamespace,
              "`#{[current_path, key].compact.join(".")}` should be an array"
      end

      if key.to_s.include?(".")
        raise InvalidNamespace, "namespace or action should not contain period: `#{key}`"
      end

      value.each.with_index do |arr_v, arr_i|
        case arr_v
        when Symbol
          if arr_v.to_s.include?(".")
            raise InvalidNamespace, "namespace or action should not contain period: `#{arr_v}`"
          end

          transformed[key] ||= ::HashWithIndifferentAccess.new
          if transformed[key].key? arr_v
            raise InvalidNamespace, "duplicate entry: `#{[current_path, key, arr_v].compact.join(".")}`"
          end

          transformed[key][arr_v] = false
        when Hash
          transform_namespace(arr_v,
                              current_path: [current_path, key].compact.join(".")).each_pair do |ts_k, ts_v|
            transformed[key] ||= ::HashWithIndifferentAccess.new
            if transformed[key].key? ts_k
              raise InvalidNamespace, "duplicate entry: `#{[current_path, key, ts_k].compact.join(".")}`"
            end

            transformed[key][ts_k] = ts_v
          end
        else
          raise InvalidNamespace,
                "`#{[current_path, key].compact.join(".")}[#{arr_i}]` should be a symbol or a hash"
        end
      end
    end
  end
end