Skip to content

Instantly share code, notes, and snippets.

@brunoandradd
Created September 24, 2020 14:50
Show Gist options
  • Save brunoandradd/b134de17e460ed46be0fbd8aa0159ee2 to your computer and use it in GitHub Desktop.
Save brunoandradd/b134de17e460ed46be0fbd8aa0159ee2 to your computer and use it in GitHub Desktop.
module AutorizacaoHelper
extend self
def autorizado_para_modulo?(usuario, modulo)
return true if Rails.env.development?
usuario.acoes.where(modulo: modulo).any?
end
def autorizado_para_secao?(usuario, modulo, secao)
return true if Rails.env.development?
query = usuario.acoes.where(modulo: modulo)
query = query.where(secao: secao) if secao.present?
query.any?
end
def autorizar_acao?(usuario, acao)
return true if Rails.env.development?
usuario.acoes.where(acao: acao.to_s).any?
end
def autorizado_para_acao?(usuario, modulo, secao, acao, method= :get)
return true if Rails.env.development?
return true if modulo.present? and secao.blank? and acao == 'index'
query = usuario.acoes.where(modulo: modulo)
query = query.where(secao: secao) if secao.present?
autorizado = false
if method == :delete
autorizado = query.where("acao like :acao", acao: "%destroy%").any?
elsif method == :update
autorizado = query.where(acao: 'edit,update').any?
else
query.each do |autorizacao_acao|
autorizado ||= autorizacao_acao.acao.split(',').include?(acao)
end
end
autorizado
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment