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
|
|
|
|
@usuarie.rol_for_site(@site).update_attribute :rol, 'invitade'
|
|
|
|
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])
|
2019-07-10 20:28:28 +00:00
|
|
|
@usuarie.rol_for_site(@site).update_attribute :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
|
|
|
|
|
|
|
|
@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
|
|
|
|
invitaciones.each do |invitacion|
|
2023-03-20 19:08:37 +00:00
|
|
|
attributes = { email: invitacion.address }
|
2019-07-05 23:55:59 +00:00
|
|
|
|
2023-03-20 20:17:53 +00:00
|
|
|
next if Usuarie.where(id: @site.roles.pluck(:usuarie_id)).find_by(attributes)
|
|
|
|
|
2023-03-20 19:54:00 +00:00
|
|
|
usuarie = Usuarie.find_by attributes
|
2023-03-20 19:59:27 +00:00
|
|
|
usuarie ||= Usuarie.invite! attributes
|
2023-03-20 19:54:00 +00:00
|
|
|
|
2023-03-20 19:08:37 +00:00
|
|
|
@site.roles.create(usuarie: usuarie, temporal: true, rol: invited_as)
|
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
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# El tipo de invitación que tenemos que enviar, si alguien mandó
|
|
|
|
# cualquier cosa, usamos el privilegio menor.
|
|
|
|
def invited_as
|
2019-07-06 00:21:49 +00:00
|
|
|
if Rol::ROLES.include?(params[:invited_as])
|
|
|
|
params[:invited_as]
|
|
|
|
else
|
|
|
|
'invitade'
|
|
|
|
end
|
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
|