summaryrefslogtreecommitdiff
path: root/app/Model/InviteModel.php
blob: 13d75f69bbd43374f81b24048305b3e7f4db00dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php

namespace Kanboard\Model;

use Kanboard\Core\Base;
use Kanboard\Core\Security\Token;

/**
 * Class InviteModel
 *
 * @package Kanboard\Model
 * @author  Frederic Guillot
 */
class InviteModel extends Base
{
    const TABLE = 'invites';

    public function createInvites(array $emails, $projectId)
    {
        $emails = array_unique($emails);
        $nb = 0;

        foreach ($emails as $email) {
            $email = trim($email);

            if (! empty($email) && $this->createInvite($email, $projectId)) {
                $nb++;
            }
        }

        return $nb;
    }

    protected function createInvite($email, $projectId)
    {
        $values = array(
            'email'      => $email,
            'project_id' => $projectId,
            'token'      => Token::getToken(),
        );

        if ($this->db->table(self::TABLE)->insert($values)) {
            $this->sendInvite($values);
            return true;
        }

        return false;
    }

    protected function sendInvite(array $values)
    {
        $this->emailClient->send(
            $values['email'],
            $values['email'],
            e('Kanboard Invitation'),
            $this->template->render('user_invite/email', array('token' => $values['token']))
        );
    }

    public function getByToken($token)
    {
        return $this->db->table(self::TABLE)
            ->eq('token', $token)
            ->findOne();
    }

    public function remove($email)
    {
        return $this->db->table(self::TABLE)
            ->eq('email', $email)
            ->remove();
    }
}