diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb index d687f8e..378ba56 100644 --- a/app/controllers/invitations_controller.rb +++ b/app/controllers/invitations_controller.rb @@ -1,41 +1,42 @@ class InvitationsController < ApplicationController before_action :fetch_invitation, only: [:accept] # POST /invitations def create invitation = Invitation.new(fetch_params) if invitation.save + invitation.notify_user flash[:success] = "User #{invitation.user.username} has been invited to the client" else flash[:alert] = 'Invitation not created' end redirect_to :back end # GET /invitations/:host_id/:verification_code def accept return redirect_to root_path unless @invitation host = @invitation.host if !@invitation.host.users.include? @invitation.user if @invitation.host.users << @invitation.user @invitation.destroy end else @invitation.destroy end redirect_to client_path(host.client) end private def fetch_params params.require(:invitation).permit(:user_id, :host_id) end def fetch_invitation @invitation = Invitation.find_by(host_id: params[:host_id], verification_code: params[:verification_code]) end end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index bd51c31..37575fe 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,24 +1,35 @@ class UserMailer < ActionMailer::Base default from: Archiving.settings[:default_sender] # Notifies the host's owners that the host has been verified by an admin # and is now ready to be configured # # @param user_emails[Array] the owners' emails # @param host[String] the host's FQDN def notify_for_verification(user_emails, host) @host = host s = "[Archiving] Host #{host} verification" mail(to: user_emails, subject: s) end + # Notifies the user that another user has requested that he should be able to + # manage the given client's backups + # + # @param email[String] the user's email + # @param invitation[Invitation] the target host + def notify_for_invitation(email, invitation) + @invitation = invitation + subject = "[Archiving] Invitation to manage #{@invitation.host.name}'s backups" + mail(to: email, subject: subject) + end + # Notifies admin about a new host that needs verification # # @param user[User] the user that created the host # @param host[String] the host's FQDN def notify_admin(user, host) @user = user @host = host mail(to: Archiving.settings[:admin_email], subject: 'New host pending verification') end end diff --git a/app/models/invitation.rb b/app/models/invitation.rb index 344cb78..f281f6b 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -1,17 +1,34 @@ # `Invitation` describes the pending invitation of a user to join a host. class Invitation < ActiveRecord::Base belongs_to :user belongs_to :host validates :user, :host, :verification_code, presence: true before_validation :calculate_verification_code + # Sends an email to the user to inform that there is an invitation to + # handle the client's backups. + def notify_user + UserMailer.notify_for_invitation(user.email, self).deliver + end + + # Fetches the parameters that can be passed to accept_invitation_url in + # order to generate the correct url + # + # @return [Hash] + def accept_hash + { + host_id: host_id, + verification_code: verification_code + } + end + private def calculate_verification_code self.verification_code = Digest::SHA256.hexdigest( [host.name, Time.now.to_s, Rails.application.secrets.salt].join ) end end diff --git a/app/views/user_mailer/notify_for_invitation.text.erb b/app/views/user_mailer/notify_for_invitation.text.erb new file mode 100644 index 0000000..65690b9 --- /dev/null +++ b/app/views/user_mailer/notify_for_invitation.text.erb @@ -0,0 +1,8 @@ +You are invited to manage <%= @invitation.host.fqdn %> 's Bacula policy + +To accept the invitation please follow the following link + +<%= accept_invitation_url(@invitation.accept_hash) %> + +--------- +Archiving