1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Users;
13
14 use ICanBoogie\DateTime;
15
16 class Model extends \Icybee\ActiveRecord\Model\Constructor
17 {
18 public function save(array $properties, $key=null, array $options=[])
19 {
20 global $core;
21
22 if (!$key)
23 {
24 if (empty($properties[User::PASSWORD]))
25 {
26 $properties[User::PASSWORD] = md5(uniqid());
27 }
28
29 if (empty($properties[User::CREATED_AT]) || DateTime::from($properties[User::CREATED_AT])->is_empty)
30 {
31 $properties[User::CREATED_AT] = DateTime::now();
32 }
33 }
34
35
36
37
38
39 unset($properties[User::PASSWORD_HASH]);
40
41 if (!empty($properties[User::PASSWORD]))
42 {
43 $properties[User::PASSWORD_HASH] = User::hash_password($properties[User::PASSWORD]);
44 }
45
46 $rc = parent::save($properties, $key, $options);
47
48
49
50
51
52 if (isset($properties[User::ROLES]))
53 {
54 $has_many_roles = $core->models['users/has_many_roles'];
55
56 if ($key)
57 {
58 $has_many_roles->filter_by_uid($key)->delete();
59 }
60
61 foreach ($properties[User::ROLES] as $rid)
62 {
63 if ($rid == 2)
64 {
65 continue;
66 }
67
68 $has_many_roles->execute('INSERT {self} SET uid = ?, rid = ?', [ $rc, $rid ]);
69 }
70 }
71
72
73
74
75
76 if (isset($properties[User::RESTRICTED_SITES]))
77 {
78 $has_many_sites = $core->models['users/has_many_sites'];
79
80 if ($key)
81 {
82 $has_many_sites->filter_by_uid($key)->delete();
83 }
84
85 foreach ($properties[User::RESTRICTED_SITES] as $siteid)
86 {
87 $has_many_sites->execute('INSERT {self} SET uid = ?, siteid = ?', [ $rc, $siteid ]);
88 }
89 }
90
91 return $rc;
92 }
93 }