Class: Instance::UserRoleRequest

Inherits:
ApplicationRecord show all
Defined in:
app/models/instance/user_role_request.rb

Instance Method Summary collapse

Instance Method Details

#approve_and_destroy!Array(Boolean, InstanceUser)

Set the corresponding instance user to the requested role and destroy the request.

Returns:

  • (Array(Boolean, InstanceUser))

    returns success status and the updated instance user.


13
14
15
16
17
18
19
20
21
22
# File 'app/models/instance/user_role_request.rb', line 13

def approve_and_destroy!
  instance_user = InstanceUser.find_or_initialize_by(user_id: user_id)
  instance_user.role = role
  success = self.class.transaction do
    raise ActiveRecord::Rollback unless instance_user.save && destroy
    true
  end

  [success, instance_user]
end

#send_new_request_email(instance) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/instance/user_role_request.rb', line 24

def send_new_request_email(instance)
  ActsAsTenant.without_tenant do
    admins = instance.instance_users.administrator.map(&:user).to_set

    # Also send emails to global admins if it's default instance.
    admins += User.administrator if instance.default? || admins.empty?

    admins.each do |admin|
      InstanceUserRoleRequestMailer.new_role_request(self, admin).deliver_later
    end
  end
end