1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Canvas\Models; |
5
|
|
|
|
6
|
|
|
use Canvas\Traits\SubscriptionPlanLimitTrait; |
7
|
|
|
use Canvas\Exception\ModelException; |
8
|
|
|
use Phalcon\Di; |
9
|
|
|
|
10
|
|
|
class UsersInvite extends AbstractModel |
11
|
|
|
{ |
12
|
|
|
use SubscriptionPlanLimitTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
* @var integer |
17
|
|
|
*/ |
18
|
|
|
public $id; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $invite_hash; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* @var integer |
29
|
|
|
*/ |
30
|
|
|
public $users_id; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @var integer |
35
|
|
|
*/ |
36
|
|
|
public $companies_id; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @var integer |
41
|
|
|
*/ |
42
|
|
|
public $role_id; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* @var integer |
47
|
|
|
*/ |
48
|
|
|
public $app_id; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
public $email; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* |
58
|
|
|
* @var integer |
59
|
|
|
*/ |
60
|
|
|
public $is_deleted; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
public $created_at; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
public $updated_at; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Subscription plan key. |
76
|
|
|
*/ |
77
|
|
|
protected $subscriptionPlanLimitKey = 'users'; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Initialize method for model. |
81
|
|
|
*/ |
82
|
|
|
public function initialize() |
83
|
|
|
{ |
84
|
|
|
$this->setSource('users_invite'); |
85
|
|
|
|
86
|
|
|
$this->belongsTo( |
87
|
|
|
'companies_id', |
88
|
|
|
'Canvas\Models\Companies', |
89
|
|
|
'id', |
90
|
|
|
['alias' => 'company'] |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$this->belongsTo( |
94
|
|
|
'apps_id', |
95
|
|
|
'Canvas\Models\Apps', |
96
|
|
|
'id', |
97
|
|
|
['alias' => 'app'] |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns table name mapped in the model. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getSource(): string |
107
|
|
|
{ |
108
|
|
|
return 'users_invite'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* What to do after the creation of a new users |
113
|
|
|
* - Assign default role. |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function afterCreate() |
118
|
|
|
{ |
119
|
|
|
$this->updateAppActivityLimit(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Validate if the current email is valid to invite. |
124
|
|
|
* |
125
|
|
|
* @throws Exception |
126
|
|
|
* @param string $email |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public static function isValid(string $email, int $roleId = 1): bool |
130
|
|
|
{ |
131
|
|
|
$userData = Di::getDefault()->getUserData(); |
132
|
|
|
//check inviste |
133
|
|
|
$invitedUser = self::findFirst([ |
134
|
|
|
'conditions' => 'email = ?0 and companies_id = ?1 and role_id = ?2 and is_deleted = 0', |
135
|
|
|
'bind' => [$email, $userData->currentCompanyId(), $roleId] |
136
|
|
|
]); |
137
|
|
|
|
138
|
|
|
if (is_object($invitedUser)) { |
139
|
|
|
throw new ModelException('User already invited to this company app'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
//check for user if they already are in this company app |
143
|
|
|
$userExists = Users::findFirst([ |
144
|
|
|
'conditions' => 'email = ?0 and is_deleted = 0', |
145
|
|
|
'bind' => [$email] |
146
|
|
|
]); |
147
|
|
|
|
148
|
|
|
if (is_object($userExists)) { |
149
|
|
|
if ($userData->defaultCompany->userAssociatedToCompany($userExists)) { |
150
|
|
|
throw new ModelException('User already is associated with this company app'); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|