|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KI\UserBundle\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use FOS\UserBundle\Model\UserManagerInterface; |
|
6
|
|
|
use KI\UserBundle\Entity\User; |
|
7
|
|
|
use KI\UserBundle\Event\UserRegistrationEvent; |
|
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
9
|
|
|
|
|
10
|
|
|
class UserFactory |
|
11
|
|
|
{ |
|
12
|
|
|
private $userManager; |
|
13
|
|
|
private $eventDispatcher; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(UserManagerInterface $userManager, EventDispatcherInterface $eventDispatcher) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->userManager = $userManager; |
|
18
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
private function array_value($array, $key, $default_value = null) |
|
22
|
|
|
{ |
|
23
|
|
|
if (!is_array($array)) |
|
24
|
|
|
return null; |
|
25
|
|
|
|
|
26
|
|
|
return array_key_exists($key, $array) ? $array[$key] : $default_value; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Creates a new user for the given username |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $username The username |
|
33
|
|
|
* @param array $roles Roles assigned to user |
|
34
|
|
|
* @param array $attributes Attributes provided by SSO server |
|
35
|
|
|
* |
|
36
|
|
|
* @return \Symfony\Component\Security\Core\User\UserInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
public function createUser($username, array $roles, array $attributes) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
$email = $this->array_value($attributes, 'email'); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var $user User |
|
44
|
|
|
*/ |
|
45
|
|
|
$user = $this->userManager->createUser(); |
|
46
|
|
|
|
|
47
|
|
|
$user->setUsername($username); |
|
48
|
|
|
$user->setEmail($email); |
|
49
|
|
|
|
|
50
|
|
|
$password = $this->array_value($attributes, 'password'); |
|
51
|
|
|
if(!$password) { |
|
52
|
|
|
$password = substr(str_shuffle(strtolower(sha1(rand() . time() . 'salt'))), 0, 8); |
|
53
|
|
|
$attributes['password'] = $password; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$user->setLoginMethod($this->array_value($attributes, 'loginMethod', 'form')); |
|
57
|
|
|
$user->setPlainPassword($password); |
|
58
|
|
|
|
|
59
|
|
|
$user->setFirstName($this->array_value($attributes, 'firstName')); |
|
60
|
|
|
$user->setLastName($this->array_value($attributes, 'lastName')); |
|
61
|
|
|
$user->setPromo($this->array_value($attributes, 'promo')); |
|
62
|
|
|
$user->setOrigin($this->array_value($attributes, 'origin')); |
|
63
|
|
|
$user->setDepartment($this->array_value($attributes, 'department')); |
|
64
|
|
|
|
|
65
|
|
|
$user->setEnabled(true); |
|
66
|
|
|
|
|
67
|
|
|
$this->userManager->updateUser($user); |
|
68
|
|
|
|
|
69
|
|
|
$userRegistration = new UserRegistrationEvent($user, $attributes); |
|
70
|
|
|
$this->eventDispatcher->dispatch('upont.user_registration', $userRegistration); |
|
71
|
|
|
|
|
72
|
|
|
return $user; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.