You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.2 KiB
52 lines
1.2 KiB
7 years ago
|
<?php
|
||
|
|
||
|
namespace core\services\auth;
|
||
|
|
||
|
use core\access\Rbac;
|
||
|
use core\dispatchers\EventDispatcher;
|
||
|
use core\entities\user\User;
|
||
|
use core\forms\auth\SignupForm;
|
||
|
use core\repositories\user\UserRepository;
|
||
|
use core\services\RoleManager;
|
||
|
use core\services\TransactionManager;
|
||
|
|
||
|
class SignupService
|
||
|
{
|
||
|
private $users;
|
||
|
private $roles;
|
||
|
private $transaction;
|
||
|
|
||
|
public function __construct(
|
||
|
UserRepository $users,
|
||
|
RoleManager $roles,
|
||
|
TransactionManager $transaction
|
||
|
)
|
||
|
{
|
||
|
$this->users = $users;
|
||
|
$this->roles = $roles;
|
||
|
$this->transaction = $transaction;
|
||
|
}
|
||
|
|
||
|
public function signup(SignupForm $form): void
|
||
|
{
|
||
|
$user = User::requestSignup(
|
||
|
$form->username,
|
||
|
$form->email,
|
||
|
$form->password
|
||
|
);
|
||
|
$this->transaction->wrap(function () use ($user) {
|
||
|
$this->users->save($user);
|
||
|
$this->roles->assign($user->id, Rbac::ROLE_USER);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
public function confirm($token): void
|
||
|
{
|
||
|
if (empty($token)) {
|
||
|
throw new \DomainException('Empty confirm token.');
|
||
|
}
|
||
|
$user = $this->users->getByEmailConfirmToken($token);
|
||
|
$user->confirmSignup();
|
||
|
$this->users->save($user);
|
||
|
}
|
||
|
}
|