|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
|
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
|
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
|
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
|
8
|
|
|
* @link https://vistart.me/ |
|
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
|
10
|
|
|
* @license https://vistart.me/license/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace rhosocial\user\models\invitation\registration; |
|
14
|
|
|
|
|
15
|
|
|
use rhosocial\base\models\queries\BaseBlameableQuery; |
|
16
|
|
|
use rhosocial\user\models\invitation\Invitation; |
|
17
|
|
|
use rhosocial\user\User; |
|
18
|
|
|
use Yii; |
|
19
|
|
|
use yii\base\InvalidParamException; |
|
20
|
|
|
use yii\db\IntegrityException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Trait UserInvitationRegistrationTrait |
|
24
|
|
|
* @property-read Invitation[] invitationRegistrations |
|
25
|
|
|
* |
|
26
|
|
|
* @package rhosocial\user\models\invitation\registration |
|
27
|
|
|
* @version 1.0 |
|
28
|
|
|
* @author vistart <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
trait UserInvitationRegistrationTrait |
|
31
|
|
|
{ |
|
32
|
|
|
public $invitationRegistrationClass = Registration::class; |
|
33
|
|
|
/** |
|
34
|
|
|
* @param array $associatedModels |
|
35
|
|
|
* @param array $authRoles |
|
36
|
|
|
* @param User $inviter |
|
37
|
|
|
* @return boolean |
|
38
|
|
|
* @throws \Exception |
|
39
|
|
|
* @throws InvalidParamException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function registerAccordingToInvitation($associatedModels = [], $authRoles = [], $inviter) |
|
42
|
|
|
{ |
|
43
|
|
|
if (!$inviter) { |
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
if ($inviter instanceof User && $inviter->getIsNewRecord()) { |
|
47
|
|
|
throw new InvalidParamException("Inviter cannot be a new user."); |
|
48
|
|
|
} |
|
49
|
|
|
$transaction = Yii::$app->db->beginTransaction(); |
|
50
|
|
|
try { |
|
51
|
|
|
$result = $this->register($associatedModels, $authRoles); |
|
|
|
|
|
|
52
|
|
|
if ($result instanceof \Exception) { |
|
53
|
|
|
throw $result; |
|
54
|
|
|
} |
|
55
|
|
|
if ($result !== true) { |
|
56
|
|
|
throw new IntegrityException("Registration Failed."); |
|
57
|
|
|
} |
|
58
|
|
|
$invitation = $inviter->createInvitationRegistration($this); |
|
59
|
|
|
$result = $invitation->save(); |
|
60
|
|
|
if (!$result) { |
|
61
|
|
|
throw new IntegrityException("Record Invitation Failed."); |
|
62
|
|
|
} |
|
63
|
|
|
$transaction->commit(); |
|
64
|
|
|
} catch (\Exception $ex) { |
|
65
|
|
|
$transaction->rollBack(); |
|
66
|
|
|
throw $ex; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $invitee |
|
72
|
|
|
* @return Registration |
|
73
|
|
|
*/ |
|
74
|
|
|
public function createInvitationRegistration($invitee) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->create($this->invitationRegistrationClass, ['invitee' => $invitee]); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Check whether this user enables the invitation from registration feature or not. |
|
81
|
|
|
* @return boolean |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function hasEnabledInvitationRegistration() |
|
84
|
|
|
{ |
|
85
|
1 |
|
if ($this->invitationRegistrationClass === false || !is_string($this->invitationRegistrationClass) || !class_exists($this->invitationRegistrationClass)) { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
1 |
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return BaseBlameableQuery |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function getInvitationRegistrations() |
|
96
|
|
|
{ |
|
97
|
1 |
|
if (!$this->hasEnabledInvitationRegistration()) { |
|
98
|
|
|
return null; |
|
99
|
|
|
} |
|
100
|
1 |
|
$irClass = $this->invitationRegistrationClass; |
|
101
|
1 |
|
$noInit = $irClass::buildNoInitModel(); |
|
102
|
|
|
/* @var $noInit Registration */ |
|
103
|
1 |
|
return $this->hasMany($irClass, [$noInit->createdByAttribute => $this->guidAttribute]); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return User[] |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getInvitationRegistrationInvitees() |
|
110
|
|
|
{ |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.