1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Icybee package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Icybee\Modules\Users; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\DateTime; |
15
|
|
|
use ICanBoogie\ActiveRecord; |
16
|
|
|
|
17
|
|
|
use Icybee\ConstructorModel; |
18
|
|
|
|
19
|
|
|
class UserModel extends ConstructorModel |
20
|
|
|
{ |
21
|
|
|
public function save(array $properties, $key = null, array $options = []) |
22
|
|
|
{ |
23
|
|
|
if (!$key) |
24
|
|
|
{ |
25
|
|
View Code Duplication |
if (empty($properties[User::PASSWORD_HASH])) |
26
|
|
|
{ |
27
|
|
|
$properties[User::PASSWORD_HASH] = User::hash_password(uniqid(true)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (empty($properties[User::CREATED_AT]) || DateTime::from($properties[User::CREATED_AT])->is_empty) |
31
|
|
|
{ |
32
|
|
|
$properties[User::CREATED_AT] = DateTime::now(); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
# |
37
|
|
|
# If defined, the password is encrypted before we pass it to our super class. |
38
|
|
|
# |
39
|
|
|
|
40
|
|
View Code Duplication |
if (!empty($properties[User::PASSWORD])) |
41
|
|
|
{ |
42
|
|
|
$properties[User::PASSWORD_HASH] = User::hash_password($properties[User::PASSWORD]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$rc = parent::save($properties, $key, $options); |
46
|
|
|
|
47
|
|
|
if (array_key_exists(User::ROLES, $properties)) |
48
|
|
|
{ |
49
|
|
|
$this->save_roles($key, $rc, $properties[User::ROLES]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (array_key_exists(User::RESTRICTED_SITES, $properties)) |
53
|
|
|
{ |
54
|
|
|
$this->save_restricted_sites($key, $rc, $properties[User::RESTRICTED_SITES]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $rc; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
protected function save_roles($key, $rc, $roles) |
61
|
|
|
{ |
62
|
|
|
$has_many_roles = $this->models['users/has_many_roles']; |
63
|
|
|
|
64
|
|
|
if ($key) |
65
|
|
|
{ |
66
|
|
|
$has_many_roles->filter_by_uid($key)->delete(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($roles as $rid) |
70
|
|
|
{ |
71
|
|
|
if ($rid == 2) |
72
|
|
|
{ |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$has_many_roles->execute('INSERT {self} SET uid = ?, rid = ?', [ $rc, $rid ]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
protected function save_restricted_sites($key, $rc, $restricted_sites) |
81
|
|
|
{ |
82
|
|
|
$has_many_sites = $this->models['users/has_many_sites']; |
83
|
|
|
|
84
|
|
|
if ($key) |
85
|
|
|
{ |
86
|
|
|
$has_many_sites->filter_by_uid($key)->delete(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
foreach ($restricted_sites as $site_id) |
90
|
|
|
{ |
91
|
|
|
$has_many_sites->execute('INSERT {self} SET uid = ?, site_id = ?', [ $rc, $site_id ]); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|