2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
module QueryRoles
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.user_has_role?(user_id, role_names, host_id = nil)
|
|
|
|
expr = Qx.select('COUNT(roles)').from(:roles)
|
|
|
|
.where('name IN ($names)', names: Array(role_names))
|
|
|
|
.and_where(user_id: user_id)
|
|
|
|
expr = expr.and_where(host_id: host_id) if host_id
|
|
|
|
expr.execute.first['count'] > 0
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# Get host tables -- host can be nonprofit, campaign, event
|
|
|
|
def self.host_ids(user_id, role_names)
|
|
|
|
Qx.select('host_id').from(:roles)
|
|
|
|
.where(user_id: user_id)
|
|
|
|
.and_where('roles.name IN ($names)', names: role_names)
|
|
|
|
.execute.map { |h| h['host_id'] }
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.is_nonprofit_user?(user_id, np_id)
|
|
|
|
user_has_role?(user_id, %i[nonprofit_admin nonprofit_associate], np_id)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.is_authorized_for_nonprofit?(user_id, np_id)
|
|
|
|
user_has_role?(user_id, [:super_admin]) || is_nonprofit_user?(user_id, np_id)
|
|
|
|
end
|
|
|
|
end
|