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
|
2019-02-01 19:40:24 +00:00
|
|
|
class Role < ApplicationRecord
|
2019-07-30 21:29:24 +00:00
|
|
|
Names = [
|
|
|
|
:super_admin, # global access
|
|
|
|
:super_associate, # global access to everything except bank acct info
|
|
|
|
:nonprofit_admin, # npo scoped access to everything
|
|
|
|
:nonprofit_associate, # npo scoped access to everything except bank acct info
|
|
|
|
:campaign_editor, # fundraising tools, without dashboard access
|
|
|
|
:event_editor # //
|
|
|
|
].freeze
|
|
|
|
|
2019-08-06 14:07:55 +00:00
|
|
|
# :name,
|
|
|
|
# :user_id, :user,
|
|
|
|
# :host, :host_id, :host_type # nil, "Nonprofit", "Campaign", "Event"
|
2019-07-30 21:29:24 +00:00
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :host, polymorphic: true
|
|
|
|
|
|
|
|
scope :super_admins, -> { where(name: :super_admin) }
|
|
|
|
scope :super_associate, -> { where(name: :super_associate) }
|
|
|
|
scope :nonprofit_admins, -> { where(name: :nonprofit_admin) }
|
|
|
|
scope :nonprofit_personnel, -> { where(name: %i[nonprofit_associate nonprofit_admin]) }
|
|
|
|
scope :campaign_editors, -> { where(name: :campaign_editor) }
|
|
|
|
scope :event_editors, -> { where(name: :event_editor) }
|
|
|
|
|
|
|
|
validates :user, presence: true
|
|
|
|
validates :name, inclusion: { in: Names }
|
|
|
|
validates :host, presence: true, unless: %i[super_admin? super_associate?]
|
|
|
|
|
|
|
|
def name
|
|
|
|
super.to_sym
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def super_admin?
|
|
|
|
name == :super_admin
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def super_associate?
|
|
|
|
name == :super_associate
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.create_for_nonprofit(role_name, email, nonprofit)
|
|
|
|
user = User.find_or_create_with_email(email)
|
|
|
|
role = Role.create(user: user, name: role_name, host: nonprofit)
|
|
|
|
return role unless role.valid?
|
|
|
|
|
|
|
|
if user.confirmed?
|
2019-11-07 19:29:32 +00:00
|
|
|
RoleAddedJob.perform_later role
|
2019-07-30 21:29:24 +00:00
|
|
|
else
|
2019-11-07 19:33:17 +00:00
|
|
|
UserInviteCreateJob.perform_later role, user.make_confirmation_token!
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
|
|
|
role
|
|
|
|
end
|
|
|
|
end
|