Skip to content

Commit

Permalink
Generate a PIN when inviting an email address
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Oct 27, 2020
1 parent 0bbd70f commit 1d92a6c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/Controller/RoomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,9 @@ public function addParticipantToRoom(string $newParticipant, string $source = 'u
$data = ['type' => $this->room->getType()];
}

$this->guestManager->inviteByEmail($this->room, $newParticipant);
$participant = $this->participantService->inviteEmailAddress($this->room, $newParticipant);

$this->guestManager->sendEmailInvitation($this->room, $participant);

return new DataResponse($data);
} else {
Expand Down
12 changes: 11 additions & 1 deletion lib/GuestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ public function getNamesBySessionHashes(array $sessionHashes): array {
return $map;
}

public function inviteByEmail(Room $room, string $email): void {
public function sendEmailInvitation(Room $room, Participant $participant): void {
$email = $participant->getAttendee()->getActorId();
$pin = $participant->getAttendee()->getPin();

$event = new AddEmailEvent($room, $email);
$this->dispatcher->dispatch(self::EVENT_BEFORE_EMAIL_INVITE, $event);

Expand All @@ -182,6 +185,8 @@ public function inviteByEmail(Room $room, string $email): void {
'invitee' => $invitee,
'roomName' => $room->getDisplayName(''),
'roomLink' => $link,
'email' => $email,
'pin' => $pin,
]);

if ($user instanceof IUser) {
Expand All @@ -200,6 +205,11 @@ public function inviteByEmail(Room $room, string $email): void {
$subject
);

if ($pin) {
// FIXME wrap in text
$template->addBodyText($pin);
}

$template->addBodyButton(
$this->l->t('Join »%s«', [$room->getDisplayName('')]),
$link
Expand Down
47 changes: 47 additions & 0 deletions lib/Service/ParticipantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Talk\Service;

use OCA\Talk\Events\AddEmailEvent;
use OCA\Talk\Events\AddParticipantsEvent;
use OCA\Talk\Events\JoinRoomGuestEvent;
use OCA\Talk\Events\JoinRoomUserEvent;
Expand All @@ -34,6 +35,7 @@
use OCA\Talk\Exceptions\InvalidPasswordException;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\UnauthorizedException;
use OCA\Talk\GuestManager;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\AttendeeMapper;
use OCA\Talk\Model\Session;
Expand All @@ -49,6 +51,7 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\Security\ISecureRandom;
use OCP\Util;

class ParticipantService {
/** @var AttendeeMapper */
Expand Down Expand Up @@ -83,6 +86,7 @@ public function __construct(AttendeeMapper $attendeeMapper,
$this->connection = $connection;
$this->dispatcher = $dispatcher;
$this->userManager = $userManager;
$this->userManager = $userManager;
$this->timeFactory = $timeFactory;
}

Expand Down Expand Up @@ -213,6 +217,33 @@ public function addUsers(Room $room, array $participants): void {
$this->dispatcher->dispatch(Room::EVENT_AFTER_USERS_ADD, $event);
}

/**
* @param Room $room
* @param string $email
* @return Participant
*/
public function inviteEmailAddress(Room $room, string $email): Participant {
$lastMessage = 0;
if ($room->getLastMessage() instanceof IComment) {
$lastMessage = (int) $room->getLastMessage()->getId();
}

$attendee = new Attendee();
$attendee->setRoomId($room->getId());
$attendee->setActorType('emails');
$attendee->setActorId($email);

// FIXME Only do this when SIP is enabled?
$attendee->setPin($this->generatePin());

$attendee->setParticipantType(Participant::GUEST);
$attendee->setLastReadMessage($lastMessage);
$this->attendeeMapper->insert($attendee);
// FIXME handle duplicate invites gracefully

return new Participant($room, $attendee, null);
}

public function ensureOneToOneRoomIsFilled(Room $room): void {
if ($room->getType() !== Room::ONE_TO_ONE_CALL) {
return;
Expand Down Expand Up @@ -603,4 +634,20 @@ public function hasActiveSessionsInCall(Room $room): bool {

return (bool) $row;
}

protected function generatePin(int $entropy = 7): string {
$pin = '';
// Do not allow to start with a '0' as that is a special mode on the phone server
// Also there are issues with some providers when you enter the same number twice
// consecutive too fast, so we avoid this as well.
$lastDigit = '0';
for ($i = 0; $i < $entropy; $i++) {
$lastDigit = $this->secureRandom->generate(1,
str_replace($lastDigit, '', ISecureRandom::CHAR_DIGITS)
);
$pin .= $lastDigit;
}

return $pin;
}
}

0 comments on commit 1d92a6c

Please sign in to comment.