2019-07-04 16:23:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-10 20:28:28 +00:00
|
|
|
# Controlador de relación entre usuaries y sitios
|
|
|
|
#
|
|
|
|
# XXX: Debería llamarse SiteUsuariesController?
|
2019-07-04 16:23:43 +00:00
|
|
|
class UsuariesController < ApplicationController
|
|
|
|
include Pundit
|
|
|
|
before_action :authenticate_usuarie!
|
|
|
|
|
2021-05-09 16:08:18 +00:00
|
|
|
# TODO: Traer los comunes desde ApplicationController
|
|
|
|
breadcrumb -> { current_usuarie.email }, :edit_usuarie_registration_path
|
|
|
|
breadcrumb 'sites.index', :sites_path, match: :exact
|
|
|
|
breadcrumb -> { site.title }, -> { site_posts_path(site, locale: locale) }, match: :exact
|
|
|
|
|
2019-07-04 16:23:43 +00:00
|
|
|
# Mostrar todes les usuaries e invitades de un sitio
|
|
|
|
def index
|
2021-05-09 16:11:15 +00:00
|
|
|
site_usuarie = SiteUsuarie.new(site, current_usuarie)
|
2019-07-04 16:23:43 +00:00
|
|
|
authorize site_usuarie
|
|
|
|
|
2021-05-09 16:08:18 +00:00
|
|
|
breadcrumb 'usuaries.index', ''
|
|
|
|
|
2019-07-04 16:23:43 +00:00
|
|
|
@policy = policy(site_usuarie)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Desasociar une usuarie de un sitio
|
|
|
|
def destroy
|
|
|
|
@site = find_site
|
|
|
|
authorize SiteUsuarie.new(@site, current_usuarie)
|
|
|
|
|
|
|
|
@usuarie = Usuarie.find(params[:id])
|
|
|
|
|
2019-07-10 20:58:05 +00:00
|
|
|
if @site.usuaries.count > 1
|
|
|
|
# Mágicamente elimina el rol
|
|
|
|
@usuarie.sites.delete(@site)
|
|
|
|
else
|
|
|
|
flash[:warning] = I18n.t('usuaries.index.destroy.denied')
|
|
|
|
end
|
2019-07-04 16:23:43 +00:00
|
|
|
|
|
|
|
redirect_to site_usuaries_path
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convertir une usuarie en invitade
|
|
|
|
def demote
|
|
|
|
@site = find_site
|
|
|
|
authorize SiteUsuarie.new(@site, current_usuarie)
|
|
|
|
|
|
|
|
@usuarie = Usuarie.find(params[:usuarie_id])
|
2019-07-10 20:58:05 +00:00
|
|
|
|
|
|
|
if @site.usuaries.count > 1
|
2023-03-23 20:38:48 +00:00
|
|
|
@usuarie.rol_for_site(@site).update_attribute :rol, Rol::INVITADE
|
2019-07-10 20:58:05 +00:00
|
|
|
else
|
|
|
|
flash[:warning] = I18n.t('usuaries.index.demote.denied')
|
|
|
|
end
|
2019-07-04 16:23:43 +00:00
|
|
|
|
|
|
|
redirect_to site_usuaries_path
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convertir invitade en usuarie
|
|
|
|
def promote
|
|
|
|
@site = find_site
|
|
|
|
authorize SiteUsuarie.new(@site, current_usuarie)
|
|
|
|
|
|
|
|
@usuarie = Usuarie.find(params[:usuarie_id])
|
2023-03-23 20:38:48 +00:00
|
|
|
@usuarie.rol_for_site(@site).update_attribute :rol, Rol::USUARIE
|
2019-07-04 16:23:43 +00:00
|
|
|
|
|
|
|
redirect_to site_usuaries_path
|
|
|
|
end
|
2019-07-05 23:55:59 +00:00
|
|
|
|
|
|
|
# Poder invitar
|
|
|
|
def invite
|
|
|
|
@site = find_site
|
|
|
|
site_usuarie = SiteUsuarie.new(@site, current_usuarie)
|
|
|
|
authorize site_usuarie
|
|
|
|
|
2023-03-23 20:50:24 +00:00
|
|
|
params[:invite_as] = invite_as
|
2023-03-23 20:21:05 +00:00
|
|
|
|
2019-07-05 23:55:59 +00:00
|
|
|
@policy = policy(site_usuarie)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Envía las invitaciones
|
|
|
|
def send_invitations
|
|
|
|
@site = find_site
|
|
|
|
authorize SiteUsuarie.new(@site, current_usuarie)
|
|
|
|
|
|
|
|
# Enviar la invitación si es necesario y agregar al sitio
|
2023-03-23 19:24:53 +00:00
|
|
|
invitaciones.each do |address|
|
|
|
|
next if Usuarie.where(id: @site.roles.pluck(:usuarie_id)).find_by_email(address)
|
2023-03-20 20:17:53 +00:00
|
|
|
|
2023-03-23 18:38:46 +00:00
|
|
|
Usuarie.transaction do
|
2023-03-23 19:24:53 +00:00
|
|
|
usuarie = Usuarie.find_by_email(address)
|
|
|
|
usuarie ||= Usuarie.invite!({ email: address, skip_invitation: true }).tap do |u|
|
2023-03-23 18:38:46 +00:00
|
|
|
u.send :generate_invitation_token!
|
|
|
|
end
|
2023-03-20 20:22:40 +00:00
|
|
|
|
2023-03-23 19:24:53 +00:00
|
|
|
role = @site.roles.create(usuarie: usuarie, temporal: true, rol: invited_as)
|
2023-03-23 14:44:47 +00:00
|
|
|
|
2023-03-23 18:38:46 +00:00
|
|
|
# XXX: La invitación tiene que ser enviada luego de crear el rol
|
2023-03-23 19:24:53 +00:00
|
|
|
if role.persisted?
|
|
|
|
usuarie.deliver_invitation
|
|
|
|
else
|
|
|
|
raise ArgumentError, role.errors.full_messages
|
|
|
|
end
|
|
|
|
rescue ArgumentError => e
|
|
|
|
ExceptionNotifier.notify_exception(e, data: { site: @site.name, address: address })
|
2023-03-23 18:38:46 +00:00
|
|
|
end
|
2019-07-05 23:55:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to site_usuaries_path(@site)
|
|
|
|
end
|
|
|
|
|
2019-07-08 17:55:19 +00:00
|
|
|
# Aceptar la invitación
|
|
|
|
def accept_invitation
|
|
|
|
@site = find_site
|
2021-01-11 16:22:11 +00:00
|
|
|
rol = current_usuarie.rol_for_site(@site)
|
2020-11-17 23:53:01 +00:00
|
|
|
|
|
|
|
# Le usuarie ya aceptó la invitación
|
2021-01-11 16:22:11 +00:00
|
|
|
unless rol&.temporal
|
2020-11-17 23:53:01 +00:00
|
|
|
redirect_to sites_path
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-07-08 17:55:19 +00:00
|
|
|
authorize SiteUsuarie.new(@site, current_usuarie)
|
|
|
|
|
2021-01-11 16:22:11 +00:00
|
|
|
Rol.transaction do
|
|
|
|
rol.update(temporal: false) &&
|
2021-02-25 20:53:08 +00:00
|
|
|
current_usuarie.update(invitation_token: nil) &&
|
2021-01-11 16:22:11 +00:00
|
|
|
@site.touch
|
|
|
|
end
|
2019-07-08 17:55:19 +00:00
|
|
|
|
|
|
|
redirect_to sites_path
|
|
|
|
end
|
|
|
|
|
|
|
|
# Declinar la invitación
|
|
|
|
def reject_invitation
|
|
|
|
@site = find_site
|
|
|
|
authorize SiteUsuarie.new(@site, current_usuarie)
|
|
|
|
|
2020-08-20 13:35:02 +00:00
|
|
|
@site.touch if current_usuarie.rol_for_site(@site).destroy
|
2019-07-08 17:55:19 +00:00
|
|
|
|
|
|
|
redirect_to sites_path
|
|
|
|
end
|
|
|
|
|
2019-07-05 23:55:59 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
# Traer todas las invitaciones que al menos tengan usuarie y dominio
|
2023-03-23 19:24:53 +00:00
|
|
|
#
|
|
|
|
# @return [Array]
|
2019-07-05 23:55:59 +00:00
|
|
|
def invitaciones
|
|
|
|
# XXX: Podríamos usar EmailAddress pero hace chequeos más lentos
|
2020-11-26 18:47:05 +00:00
|
|
|
params[:invitaciones]&.tr("\r", '')&.split("\n")&.map do |m|
|
2019-07-05 23:55:59 +00:00
|
|
|
Mail::Address.new m
|
2020-11-26 18:47:05 +00:00
|
|
|
rescue Mail::Field::IncompleteParseError
|
|
|
|
nil
|
|
|
|
end.compact.select do |m|
|
2019-07-05 23:55:59 +00:00
|
|
|
m.local && m.domain
|
2023-03-23 19:24:53 +00:00
|
|
|
end.map(&:address)
|
2019-07-05 23:55:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# El tipo de invitación que tenemos que enviar, si alguien mandó
|
|
|
|
# cualquier cosa, usamos el privilegio menor.
|
2023-03-23 20:21:05 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
2023-03-23 20:50:24 +00:00
|
|
|
def invited_as
|
|
|
|
Rol.role?(params[:invited_as]) ? params[:invited_as] : Rol::INVITADE
|
|
|
|
end
|
|
|
|
|
|
|
|
def invite_as
|
2023-03-23 20:53:13 +00:00
|
|
|
Rol.role?(params[:invite_as]&.singularize) ? params[:invite_as] : Rol::INVITADE.pluralize
|
2019-07-05 23:55:59 +00:00
|
|
|
end
|
2021-05-09 16:11:15 +00:00
|
|
|
|
|
|
|
def site
|
|
|
|
@site ||= find_site
|
|
|
|
end
|
2019-07-04 16:23:43 +00:00
|
|
|
end
|