1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/patron/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\organizations\events\handlers; |
10
|
|
|
|
11
|
|
|
use craft\elements\User; |
12
|
|
|
use craft\events\ModelEvent; |
13
|
|
|
use flipbox\organizations\Organizations; |
14
|
|
|
use flipbox\organizations\records\UserAssociation; |
15
|
|
|
use yii\helpers\Json; |
16
|
|
|
use yii\web\UserEvent; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This event will transition a user from 'invited' to another ('active' by default) upon login. |
20
|
|
|
* |
21
|
|
|
* @author Flipbox Factory <[email protected]> |
22
|
|
|
* @since 3.0.0 |
23
|
|
|
*/ |
24
|
|
|
class TransitionUserState |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The state to transition to. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
public static $state = UserAssociation::STATE_ACTIVE; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ModelEvent $event |
35
|
|
|
*/ |
36
|
|
|
public static function handle(UserEvent $event) |
37
|
|
|
{ |
38
|
|
|
/** @var User $user */ |
39
|
|
|
$user = $event->identity; |
40
|
|
|
|
41
|
|
|
try { |
42
|
|
|
static::transitionStates($user); |
43
|
|
|
} catch (\Exception $e) { |
44
|
|
|
Organizations::warning( |
45
|
|
|
sprintf( |
46
|
|
|
"Exception caught while trying to transition user '%s' organization type states. " . |
47
|
|
|
"Exception: [%s].", |
48
|
|
|
$user->getId(), |
49
|
|
|
(string)Json::encode([ |
50
|
|
|
'Trace' => $e->getTraceAsString(), |
51
|
|
|
'File' => $e->getFile(), |
52
|
|
|
'Line' => $e->getLine(), |
53
|
|
|
'Code' => $e->getCode(), |
54
|
|
|
'Message' => $e->getMessage() |
55
|
|
|
]) |
56
|
|
|
), |
57
|
|
|
__METHOD__ |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param User $user |
64
|
|
|
*/ |
65
|
|
|
protected static function transitionStates(User $user) |
66
|
|
|
{ |
67
|
|
|
$userAssociations = $user->getOrganizations() |
68
|
|
|
->getRelationships() |
69
|
|
|
->where('state', UserAssociation::STATE_INVITED); |
70
|
|
|
|
71
|
|
|
/** @var UserAssociation $association */ |
72
|
|
|
foreach ($userAssociations as $association) { |
73
|
|
|
static::transitionState($association); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param UserAssociation $association |
79
|
|
|
*/ |
80
|
|
|
protected static function transitionState(UserAssociation $association) |
81
|
|
|
{ |
82
|
|
|
if ($association->state == static::$state) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$state = $association->state; |
87
|
|
|
$association->state = static::$state; |
88
|
|
|
|
89
|
|
|
Organizations::info( |
90
|
|
|
sprintf( |
91
|
|
|
"Transitioning user '%s' associated to organization '%s' from state '%s' to '%s'.", |
92
|
|
|
$association->getUserId(), |
93
|
|
|
$association->getOrganizationId(), |
94
|
|
|
$state, |
95
|
|
|
$association->state |
96
|
|
|
), |
97
|
|
|
__METHOD__ |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
try { |
101
|
|
|
if (!$association->save(true)) { |
102
|
|
|
Organizations::warning( |
103
|
|
|
sprintf( |
104
|
|
|
"Failed to transition user '%s' associated to organization '%s' " . |
105
|
|
|
"from state '%s' to '%s'. Exception: [%s].", |
106
|
|
|
$association->getUserId(), |
107
|
|
|
$association->getOrganizationId(), |
108
|
|
|
$state, |
109
|
|
|
$association->state, |
110
|
|
|
(string)Json::encode($association->getErrors()) |
111
|
|
|
), |
112
|
|
|
__METHOD__ |
113
|
|
|
); |
114
|
|
|
}; |
115
|
|
|
} catch (\Exception $e) { |
116
|
|
|
Organizations::warning( |
117
|
|
|
sprintf( |
118
|
|
|
"Exception caught while trying to transition user '%s' associated to organization '%s' " . |
119
|
|
|
"from state '%s' to '%s'. Exception: [%s].", |
120
|
|
|
$association->getUserId(), |
121
|
|
|
$association->getOrganizationId(), |
122
|
|
|
$state, |
123
|
|
|
$association->state, |
124
|
|
|
(string)Json::encode([ |
125
|
|
|
'Trace' => $e->getTraceAsString(), |
126
|
|
|
'File' => $e->getFile(), |
127
|
|
|
'Line' => $e->getLine(), |
128
|
|
|
'Code' => $e->getCode(), |
129
|
|
|
'Message' => $e->getMessage() |
130
|
|
|
]) |
131
|
|
|
), |
132
|
|
|
__METHOD__ |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|