docbrown/app/controllers/admin/invitations_controller.rb
Jeremy Friesen b4f17ed7dc
Allowing UserQuery to filter on multiple roles (#17919)
I had looked at using `User.with_any_roles` but that creates one User
query per role passed.  Which is not ideal, given that we want to have
an ActiveRecord::Relation object on which we'd paginate.

This adjustment does it's best to mimic Forem's role structure and
adhear to how Rolify builds the underlying query.

An astute reader will notice that in these specs we use `role: "admin"`
and `roles: ["Admin"]`; the primary reason relates the user interface
being worked on in forem/forem#17884.  It is my understanding that the
`role` approach and `roles` approach will be separated by a feature
flag (we're rolling out the `roles` approach).

Related to
- forem/forem#17491
- forem/forem#17884
2022-06-15 08:50:03 -04:00

54 lines
1.7 KiB
Ruby

module Admin
class InvitationsController < Admin::ApplicationController
layout "admin"
def index
@invitations = Admin::UsersQuery.call(
relation: User.invited,
search: params[:search],
role: params[:role],
roles: params[:roles],
).page(params[:page]).per(50)
end
def new; end
def create
email = params.dig(:user, :email)
if User.exists?(email: email.downcase, registered: true)
flash[:error] = I18n.t("admin.invitations_controller.duplicate", email: email)
redirect_to admin_invitations_path
return
end
username = "#{email.split('@').first.gsub(/[^0-9a-z ]/i, '')}_#{rand(1000)}"
User.invite!(email: email,
username: username,
remote_profile_image_url: ::Users::ProfileImageGenerator.call,
registered: false)
flash[:success] = I18n.t("admin.invitations_controller.create_success")
redirect_to admin_invitations_path
end
def destroy
@invitation = User.where(registered: false).find(params[:id])
if @invitation.destroy
flash[:success] = I18n.t("admin.invitations_controller.destroy_success", email: @invitation.email)
else
flash[:danger] = @invitation.errors_as_sentence
end
redirect_to admin_invitations_path
end
def resend
@invited_user = User.where(registered: false).find(params[:id])
if @invited_user.invite!
flash[:success] = I18n.t("admin.invitations_controller.resend_success", email: @invited_user.email)
else
flash[:danger] = @invited_user.errors_as_sentence
end
redirect_to admin_invitations_path
end
end
end