1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Service; |
13
|
|
|
|
14
|
|
|
use Da\User\Contracts\ServiceInterface; |
15
|
|
|
use Da\User\Event\UserEvent; |
16
|
|
|
use Da\User\Helper\SecurityHelper; |
17
|
|
|
use Da\User\Model\User; |
18
|
|
|
use Da\User\Traits\MailAwareTrait; |
19
|
|
|
use Da\User\Traits\ModuleAwareTrait; |
20
|
|
|
use Exception; |
21
|
|
|
use Yii; |
22
|
|
|
use yii\base\InvalidCallException; |
23
|
|
|
|
24
|
|
|
class UserCreateService implements ServiceInterface |
25
|
|
|
{ |
26
|
|
|
use MailAwareTrait; |
27
|
|
|
use ModuleAwareTrait; |
28
|
|
|
|
29
|
|
|
protected $model; |
30
|
|
|
protected $securityHelper; |
31
|
|
|
protected $mailService; |
32
|
|
|
|
33
|
1 |
|
public function __construct(User $model, MailService $mailService, SecurityHelper $securityHelper) |
34
|
|
|
{ |
35
|
1 |
|
$this->model = $model; |
36
|
1 |
|
$this->mailService = $mailService; |
37
|
1 |
|
$this->securityHelper = $securityHelper; |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws InvalidCallException |
42
|
|
|
* @throws \yii\db\Exception |
43
|
|
|
* @return bool |
44
|
|
|
* |
45
|
|
|
*/ |
46
|
1 |
|
public function run() |
47
|
|
|
{ |
48
|
1 |
|
$model = $this->model; |
49
|
|
|
|
50
|
1 |
|
if ($model->getIsNewRecord() === false) { |
51
|
|
|
throw new InvalidCallException('Cannot create a new user from an existing one.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$transaction = $model::getDb()->beginTransaction(); |
55
|
|
|
|
56
|
|
|
try { |
57
|
1 |
|
$model->confirmed_at = time(); |
58
|
1 |
|
$model->password = !empty($model->password) |
59
|
1 |
|
? $model->password |
60
|
|
|
: $this->securityHelper->generatePassword(8); |
61
|
|
|
|
62
|
|
|
/** @var UserEvent $event */ |
63
|
1 |
|
$event = $this->make(UserEvent::class, [$model]); |
64
|
1 |
|
$model->trigger(UserEvent::EVENT_BEFORE_CREATE, $event); |
65
|
|
|
|
66
|
1 |
|
if (!$model->save()) { |
67
|
|
|
$transaction->rollBack(); |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$model->trigger(UserEvent::EVENT_AFTER_CREATE, $event); |
72
|
1 |
|
if (!$this->sendMail($model)) { |
73
|
|
|
$error_msg = Yii::t( |
74
|
|
|
'usuario', |
75
|
|
|
'Error sending welcome message to "{email}". Please try again later.', |
76
|
|
|
['email' => $model->email] |
77
|
|
|
); |
78
|
|
|
// from web display a flash message (if enabled) |
79
|
|
|
if ($this->getModule()->enableFlashMessages === true && is_a(Yii::$app, yii\web\Application::class)) { |
80
|
|
|
Yii::$app->session->setFlash( |
81
|
|
|
'warning', |
82
|
|
|
$error_msg |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
// if we're from console add an error to the model in order to return an error message |
86
|
|
|
if (is_a(Yii::$app, yii\console\Application::class)) { |
87
|
|
|
$model->addError('username', $error_msg); |
88
|
|
|
} |
89
|
|
|
$transaction->rollBack(); |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
1 |
|
$transaction->commit(); |
93
|
|
|
return true; |
94
|
1 |
|
} catch (Exception $e) { |
95
|
1 |
|
$transaction->rollBack(); |
96
|
1 |
|
Yii::error($e->getMessage(), 'usuario'); |
97
|
|
|
|
98
|
1 |
|
return false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|