Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(place/visitor_mailer): provide a QR code for building access #516

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions drivers/place/smtp.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require "qr-code"
require "qr-code/export/png"
require "placeos-driver"
require "placeos-driver/interface/mailer"

require "goban"
require "goban/exporters/png"
require "goban/exporters/svg"
require "base64"
require "email"
require "uri"
require "placeos-driver"
require "placeos-driver/interface/mailer"

class Place::Smtp < PlaceOS::Driver
include PlaceOS::Driver::Interface::Mailer
Expand All @@ -14,6 +16,11 @@ class Place::Smtp < PlaceOS::Driver
uri_base "https://smtp.host.com"
description %(sends emails via SMTP)

enum BinaryFormatQR
Hex
Base64
end

default_settings({
sender: "[email protected]",
# host: "smtp.host",
Expand All @@ -23,6 +30,8 @@ class Place::Smtp < PlaceOS::Driver
username: "", # Username/Password for SMTP servers with basic authorization
password: "",

qr_binary_format: BinaryFormatQR::Hex,

email_templates: {visitor: {checkin: {
subject: "%{name} has arrived",
text: "for your meeting at %{time}",
Expand All @@ -43,6 +52,7 @@ class Place::Smtp < PlaceOS::Driver
@tls_mode : EMail::Client::TLSMode = EMail::Client::TLSMode::STARTTLS
@send_lock : Mutex = Mutex.new
@ssl_verify_ignore : Bool = false
@qr_binary_format : BinaryFormatQR = BinaryFormatQR::Hex

def on_load
on_update
Expand All @@ -65,6 +75,7 @@ class Place::Smtp < PlaceOS::Driver
@port = setting?(Int32, :port) || port
@tls_mode = setting?(EMail::Client::TLSMode, :tls_mode) || tls_mode
@ssl_verify_ignore = setting?(Bool, :ssl_verify_ignore) || false
@qr_binary_format = setting?(BinaryFormatQR, :qr_binary_format) || BinaryFormatQR::Hex

@smtp_client = new_smtp_client

Expand All @@ -88,11 +99,27 @@ class Place::Smtp < PlaceOS::Driver
end

def generate_svg_qrcode(text : String) : String
QRCode.new(text).as_svg
qr = Goban::QR.encode_string(text)
Goban::SVGExporter.svg_string(qr, 4)
end

def generate_png_qrcode(text : String, size : Int32 = 128) : String
Base64.strict_encode QRCode.new(text).as_png(size: size)
qr = Goban::QR.encode_string(text)
io = IO::Memory.new
Goban::PNGExporter.export(qr, io, size)
Base64.strict_encode io.to_slice
end

def generate_png_qrcode_hex(hex_text : String, size : Int32 = 128) : String
case @qr_binary_format
in .hex?
in .base64?
hex_text = Base64.strict_encode hex_text.hexbytes
end
qr = Goban::QR.encode_string(hex_text)
io = IO::Memory.new
Goban::PNGExporter.export(qr, io, size)
Base64.strict_encode io.to_slice
end

def send_mail(
Expand Down
5 changes: 5 additions & 0 deletions drivers/place/visitor_mailer.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "placeos-driver"
require "placeos-driver/interface/mailer"
require "placeos-driver/interface/guest_building_access"

require "./password_generator_helper"

Expand Down Expand Up @@ -343,6 +344,10 @@ class Place::VisitorMailer < PlaceOS::Driver

attach = if @disable_qr_code
[] of NamedTuple(file_name: String, content: String, content_id: String)
elsif (access_control = system.implementing(Interface::GuestBuildingAccess).first?) && access_control.guest_access_configured?.get.as_bool
details = access_control.grant_guest_access(visitor_name || visitor_email.sub('@', ' '), visitor_email, event_start, event_start + 1.hour.total_seconds).get
data = details["card_hex"].as_s
qr_png = mailer.generate_png_qrcode_hex(hex_text: data, size: 256).get.as_s
else
qr_png = mailer.generate_png_qrcode(text: "VISIT:#{visitor_email},#{resource_id},#{event_id},#{host_email}", size: 256).get.as_s
[
Expand Down
Loading