class ProxES::Permission

Private Class Methods

from_audit_log(audit_log) click to toggle source
# File lib/proxes/models/permission.rb, line 61
def from_audit_log(audit_log)
  return {} if audit_log.details.nil?

  match = audit_log.details.match(/^(\w)+ (\S+)/)
  return {} if match.nil?

  {
    verb: match[1],
    path: match[2]
  }
end
verbs() click to toggle source
# File lib/proxes/models/permission.rb, line 57
def verbs
  %w[GET POST PUT DELETE HEAD OPTIONS TRACE]
end

Public Instance Methods

for_request(request) click to toggle source
# File lib/proxes/models/permission.rb, line 24
def for_request(request)
  where(verb: request.request_method).all.select { |perm| perm.pattern_regex.match request.path }
end
for_user(usr) click to toggle source
# File lib/proxes/models/permission.rb, line 16
def for_user(usr)
  return where(id: -1) if usr.nil?

  # TODO: Injection of user fields into regex
  # permission.pattern.gsub(/\{user.(.*)\}/) { |_match| user.send(Regexp.last_match[1].to_sym) }
  where { Sequel.|({ role: usr.roles }, { user_id: usr.id }) }
end
index_regex() click to toggle source
# File lib/proxes/models/permission.rb, line 41
def index_regex
  regex index
end
pattern_regex() click to toggle source
# File lib/proxes/models/permission.rb, line 37
def pattern_regex
  regex pattern
end
validate() click to toggle source
Calls superclass method
# File lib/proxes/models/permission.rb, line 29
def validate
  super
  validates_presence %i[verb pattern]
  validates_presence :role_id unless user_id
  validates_presence :user_id unless role_id
  validates_includes self.class.verbs, :verb
end

Private Instance Methods

regex(str) click to toggle source
# File lib/proxes/models/permission.rb, line 47
def regex(str)
  str ||= ''
  return Regexp.new(str) if str.blank? || (str[0] == '|' && str[-1] == '|')

  str = str.gsub(/([^.])\*/, '\1.*')
  str = '.*' if str == '*' # My regex foo is not strong enough to combine the previous line and this one
  Regexp.new '^' + str
end