-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserService.php
More file actions
52 lines (42 loc) · 1.51 KB
/
UserService.php
File metadata and controls
52 lines (42 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace App\Service;
use App\Domain\User\BenchUserRepository;
use App\Domain\User\BenchUser;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
class UserService
{
/**
* @var BenchUserRepository
*/
private $userRepository;
/**
* @var EncoderFactoryInterface
*/
private $encoderFactory;
public function __construct(BenchUserRepository $userRepository, EncoderFactoryInterface $encoderFactory)
{
$this->userRepository = $userRepository;
$this->encoderFactory = $encoderFactory;
}
public function createLocalUser(string $username, string $password): BenchUser
{
// using bcrypt, to no salt
$password = $this->encoderFactory->getEncoder(BenchUser::class)->encodePassword($password, null);
return $this->userRepository->create($username, uniqid(), $password, [ BenchUser::ROLE_USER ]);
}
public function grantRoles(string $username, array $roles)
{
$user = $this->userRepository->findByUsername($username);
$user->setRoles($roles);
$this->userRepository->update($user);
}
public function findOrCreateForVendor(string $username, int $vendorId): BenchUser
{
$user = $this->userRepository->findByVendorId($vendorId);
if (null === $user) {
$user = $this->userRepository->create($username, $vendorId, null, [ BenchUser::ROLE_USER ]);
}
return $user;
}
}