Completed
Push — master ( 7ac7c3...b4dd2a )
by Craig
20:50
created

UserEntity::getGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
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 Zikula\UsersModule\Entity;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\ORM\Mapping as ORM;
16
use Symfony\Component\Validator\Constraints as Assert;
17
use Zikula\Core\Doctrine\EntityAccess;
18
use Zikula\GroupsModule\Entity\GroupEntity;
19
use Zikula\UsersModule\Constant as UsersConstant;
20
use Zikula\UsersModule\Validator\Constraints as ZikulaAssert;
21
22
/**
23
 * User entity class.
24
 *
25
 * @ORM\Entity(repositoryClass="Zikula\UsersModule\Entity\Repository\UserRepository")
26
 * @ORM\Table(name="users",indexes={@ORM\Index(name="uname",columns={"uname"}), @ORM\Index(name="email",columns={"email"})})
27
 *
28
 * @ZikulaAssert\ValidUserFields()
29
 *
30
 * Main Users table.
31
 * Stores core information about each user account.
32
 */
33
class UserEntity extends EntityAccess
34
{
35
    /**
36
     * User ID: Primary user identifier
37
     *
38
     * @ORM\Id
39
     * @ORM\Column(type="integer")
40
     * @ORM\GeneratedValue(strategy="AUTO")
41
     */
42
    private $uid;
43
44
    /**
45
     * User Name: Primary user display name.
46
     *
47
     * @ZikulaAssert\ValidUname()
48
     * @ORM\Column(type="string", length=25)
49
     */
50
    private $uname;
51
52
    /**
53
     * E-mail Address: For user notifications.
54
     *
55
     * @ZikulaAssert\ValidEmail()
56
     * @ORM\Column(type="string", length=60)
57
     */
58
    private $email;
59
60
    /**
61
     * Account State: The user's current state, see \Zikula\UsersModule\Constant::ACTIVATED_* for defined constants.
62
     * A state represented by a negative integer means that the user's account is in a pending state, and should not yet be considered a "real" user account.
63
     * For example, user accounts pending the completion of the registration process (because either moderation, e-mail verification, or both are in use)
64
     * will have a negative integer representing their state. If the user's registration request expires before it the process is completed, or if the administrator
65
     * denies the request for an new account, the user account record will be deleted.
66
     * When this deletion happens, it will be assumed by the system that no external module has yet interacted with the user account record,
67
     * because its state never progressed beyond its pending state, and therefore normal hooks/events may not be triggered
68
     * (although it is possible that events regarding the pending account may be triggered).
69
     *
70
     * @Assert\Choice(callback = "getActivatedValues")
71
     * @ORM\Column(type="smallint")
72
     */
73
    private $activated;
74
75
    /**
76
     * Account Approved Date/Time: The date and time the user's registration request was approved through the moderation process.
77
     * If the moderation process was not in effect at the time the user made a registration request, then this will be the date and time of the registration request.
78
     * NOTE: This is stored as an SQL datetime, using the UTC time zone. The date/time is NEITHER server local time nor user local time (unless one or the other happens to be UTC).
79
     * WARNING: The date and time related functions available in SQL on many RDBMS servers are highly dependent on the database server's timezone setting.
80
     * All parameters to these functions are treated as if the dates and times they represent are in the time zone that is set in the database server's settings.
81
     * Use of date/time functions in SQL queries should be avoided if at all possible. PHP functions using UTC as the base time zone should be used instead.
82
     * If SQL date/time functions must be used, then care should be taken to ensure that either the function is time zone neutral,
83
     * or that the function and its relationship to time zone settings is completely understood.
84
     *
85
     * @Assert\DateTime()
86
     * @ORM\Column(type="utcdatetime")
87
     */
88
    private $approved_date;
89
90
    /**
91
     * The uid of the user account that approved the request to register a new account.
92
     * If this is the same as the user account's uid, then moderation was not in use at the time the request for a new account was made.
93
     * If this is -1, the the user account that approved the request has since been deleted. If this is 0, the user account has not yet been approved.
94
     *
95
     * @Assert\Type(type="integer")
96
     * @ORM\Column(type="integer")
97
     */
98
    private $approved_by;
99
100
    /**
101
     * Registration Date/Time: Date/time the user account was registered.
102
     * For users not pending the completion of the registration process, this is the date and time the user account completed the process.
103
     * For example, if registrations are moderated, then this is the date and time the registration request was approved.
104
     * If registration e-mail addresses must be verified, then this is the date and time the user completed the verification process.
105
     * If both moderation and verification are in use, then this is the later of those two dates.
106
     * If neither is in use, then this is simply the date and time the user's registration request was made.
107
     * If the user account's activated state is "pending registration" (implying that either moderation, verification, or both are in use)
108
     * then this will be the date and time the user made the registration request UNTIL the registration process is complete, and then it is updated as above.
109
     * NOTE: This is stored as an SQL datetime, using the UTC time zone. The date/time is NEITHER server local time nor user local time. SEE WARNING under approved_date, above.
110
     *
111
     * @Assert\DateTime()
112
     * @ORM\Column(type="utcdatetime")
113
     */
114
    private $user_regdate;
115
116
    /**
117
     * Last Login Date/Time: Date/time user last successfully logged into the site.
118
     * NOTE: This is stored as an SQL datetime, using the UTC time zone. The date/time is NEITHER server local time nor user local time. SEE WARNING under approved_date, above.
119
     *
120
     * @Assert\DateTime()
121
     * @ORM\Column(type="utcdatetime")
122
     */
123
    private $lastlogin;
124
125
    /**
126
     * User's timezone, as supported by PHP (listed at http://us2.php.net/manual/en/timezones.php), and as expressed by the Olson tz database.
127
     * Optional, if blank then the system default timezone should be used. [FUTURE USE]
128
     *
129
     * @Assert\Type(type="string")
130
     * @ORM\Column(type="string", length=30)
131
     */
132
    private $tz;
133
134
    /**
135
     * The user's chosen locale for i18n purposes, as defined by gettext, POSIX, and the Common Locale Data Repository;
136
     * Optional, if blank then the system default locale should be used.
137
     *
138
     * @Assert\Type(type="string")
139
     * @ORM\Column(type="string", length=5)
140
     */
141
    private $locale;
142
143
    /**
144
     * Additional attributes of this user
145
     *
146
     * @ORM\OneToMany(targetEntity="UserAttributeEntity",
147
     *                mappedBy="user",
148
     *                cascade={"all"},
149
     *                orphanRemoval=true,
150
     *                indexBy="name")
151
     */
152
    private $attributes;
153
154
    /**
155
     * @ORM\ManyToMany(targetEntity="Zikula\GroupsModule\Entity\GroupEntity", inversedBy="users", indexBy="gid")
156
     * @ORM\JoinTable(name="group_membership",
157
     *      joinColumns={@ORM\JoinColumn(name="uid", referencedColumnName="uid")},
158
     *      inverseJoinColumns={@ORM\JoinColumn(name="gid", referencedColumnName="gid")}
159
     *      )
160
     **/
161
    private $groups;
162
163
    /**
164
     * constructor
165
     */
166
    public function __construct()
167
    {
168
        $this->uname = '';
169
        $this->email = '';
170
        $this->activated = 0;
171
        $this->approved_date = new \DateTime("1970-01-01 00:00:00");
172
        $this->approved_by = 0;
173
        $this->user_regdate = new \DateTime("1970-01-01 00:00:00");
174
        $this->lastlogin = new \DateTime("1970-01-01 00:00:00");
175
        $this->tz = '';
176
        $this->locale = '';
177
178
        $this->attributes = new ArrayCollection();
179
        $this->groups = new ArrayCollection();
180
    }
181
182
    /**
183
     * get the uid of the user
184
     *
185
     * @return integer the user's uid
186
     */
187
    public function getUid()
188
    {
189
        return $this->uid;
190
    }
191
192
    /**
193
     * set the uid for the user
194
     *
195
     * @param integer $uid the user's uid
196
     */
197
    public function setUid($uid)
198
    {
199
        $this->uid = $uid;
200
    }
201
202
    /**
203
     * get the uname of the user
204
     *
205
     * @return string the user's uname
206
     */
207
    public function getUname()
208
    {
209
        return $this->uname;
210
    }
211
212
    /**
213
     * get the username of the user
214
     *
215
     * @return string the user's name
216
     */
217
    public function getUsername()
218
    {
219
        return $this->getUname();
220
    }
221
222
    /**
223
     * set the uname for the user
224
     *
225
     * @param string $uname the user's uname
226
     */
227
    public function setUname($uname)
228
    {
229
        $this->uname = $uname;
230
    }
231
232
    /**
233
     * get the email of the user
234
     *
235
     * @return string the user's email
236
     */
237
    public function getEmail()
238
    {
239
        return $this->email;
240
    }
241
242
    /**
243
     * set the email for the user
244
     *
245
     * @param string $email the user's email
246
     */
247
    public function setEmail($email)
248
    {
249
        $this->email = $email;
250
    }
251
252
    /**
253
     * get the activation status of the user
254
     *
255
     * @return integer the user's activation status
256
     */
257
    public function getActivated()
258
    {
259
        return $this->activated;
260
    }
261
262
    /**
263
     * set the activation status for the user
264
     *
265
     * @param integer $activated the user's activation status
266
     */
267
    public function setActivated($activated)
268
    {
269
        $this->activated = $activated;
270
    }
271
272
    /**
273
     * get the approved date of the user
274
     *
275
     * @return \Datetime the user's approved date
276
     */
277
    public function getApproved_Date()
278
    {
279
        return $this->approved_date;
280
    }
281
282
    /**
283
     * set the approved date for the user
284
     *
285
     * @param \Datetime $approved_date the user's approved date
286
     */
287
    public function setApproved_Date($approved_date)
288
    {
289
        if ($approved_date instanceof \DateTime) {
290
            $this->approved_date = $approved_date;
291
        } else {
292
            // assume $approved_date is a string.
293
            $this->approved_date = new \DateTime($approved_date);
294
        }
295
    }
296
297
    /**
298
     * get the user id who approved the user
299
     *
300
     * @return integer the user's id who approved the user
301
     */
302
    public function getApproved_By()
303
    {
304
        return $this->approved_by;
305
    }
306
307
    /**
308
     * set the user id who approved the user
309
     *
310
     * @param integer $approved_by the user's id who approved the user
311
     */
312
    public function setApproved_By($approved_by)
313
    {
314
        $this->approved_by = $approved_by;
315
    }
316
317
    /**
318
     * @return bool
319
     */
320
    public function isApproved()
321
    {
322
        return $this->approved_by != 0;
323
    }
324
325
    /**
326
     * get the regdate of the user
327
     *
328
     * @return \Datetime the user's regdate
329
     */
330
    public function getUser_Regdate()
331
    {
332
        return $this->user_regdate;
333
    }
334
335
    /**
336
     * set the regdate for the user
337
     *
338
     * @param \Datetime $user_regdate the user's regdate
339
     */
340
    public function setUser_Regdate($user_regdate)
341
    {
342
        if ($user_regdate instanceof \DateTime) {
343
            $this->user_regdate = $user_regdate;
344
        } else {
345
            // assume $user_regdate is a string
346
            $this->user_regdate = new \DateTime($user_regdate);
347
        }
348
    }
349
350
    /**
351
     * get the last login of the user
352
     *
353
     * @return \Datetime the user's last login
354
     */
355
    public function getLastlogin()
356
    {
357
        return $this->lastlogin;
358
    }
359
360
    /**
361
     * set the last login for the user
362
     *
363
     * @param \Datetime $lastlogin the user's last login
364
     */
365
    public function setLastlogin($lastlogin)
366
    {
367
        if ($lastlogin instanceof \DateTime) {
368
            $this->lastlogin = $lastlogin;
369
        } else {
370
            // assume $lastlogin is a string
371
            $this->lastlogin = new \DateTime($lastlogin);
372
        }
373
    }
374
375
    /**
376
     * get the tz of the user
377
     *
378
     * @return string the user's tz
379
     */
380
    public function getTz()
381
    {
382
        return $this->tz;
383
    }
384
385
    /**
386
     * set the tz for the user
387
     *
388
     * @param string $tz the user's tz
389
     */
390
    public function setTz($tz)
391
    {
392
        $this->tz = $tz;
393
    }
394
395
    /**
396
     * get the locale of the user
397
     *
398
     * @return string the user's locale
399
     */
400
    public function getLocale()
401
    {
402
        return $this->locale;
403
    }
404
405
    /**
406
     * set the locale for the user
407
     *
408
     * @param string $locale the user's locale
409
     */
410
    public function setLocale($locale)
411
    {
412
        $this->locale = $locale;
413
    }
414
415
    /**
416
     * get the attributes of the user
417
     *
418
     * @return ArrayCollection UserAttributeEntity[] of the user's attributes
419
     */
420
    public function getAttributes()
421
    {
422
        return $this->attributes;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->attributes; of type Doctrine\Common\Collecti...ity\UserAttributeEntity adds the type Zikula\UsersModule\Entity\UserAttributeEntity to the return on line 422 which is incompatible with the return type documented by Zikula\UsersModule\Entit...erEntity::getAttributes of type Doctrine\Common\Collections\ArrayCollection.
Loading history...
423
    }
424
425
    public function getAttributeValue($name)
426
    {
427
        return $this->getAttributes()->get($name)->getValue();
428
    }
429
430
    /**
431
     * set the attributes for the user
432
     *
433
     * @param UserAttributeEntity $attributes the attributes for the user
434
     */
435
    public function setAttributes($attributes)
436
    {
437
        $this->attributes = $attributes;
438
    }
439
440
    /**
441
     * set a single attribute for the user
442
     *
443
     * @param $name string attribute name
444
     * @param $value string attribute value
445
     */
446
    public function setAttribute($name, $value)
447
    {
448
        if (isset($this->attributes[$name])) {
449
            $this->attributes[$name]->setValue($value);
450
        } else {
451
            $this->attributes[$name] = new UserAttributeEntity($this, $name, $value);
0 ignored issues
show
Documentation introduced by
$this is of type this<Zikula\UsersModule\Entity\UserEntity>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
452
        }
453
    }
454
455
    /**
456
     * delete a single attribute of the user
457
     *
458
     * @param $name string attribute name
459
     */
460
    public function delAttribute($name)
461
    {
462
        if (isset($this->attributes[$name])) {
463
            $this->attributes->remove($name);
1 ignored issue
show
Bug introduced by
The method remove() does not seem to exist on object<Zikula\UsersModul...ty\UserAttributeEntity>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
464
        }
465
    }
466
467
    /**
468
     * @param $name
469
     * @return bool
470
     */
471
    public function hasAttribute($name)
472
    {
473
        return $this->attributes->containsKey($name);
1 ignored issue
show
Bug introduced by
The method containsKey() does not seem to exist on object<Zikula\UsersModul...ty\UserAttributeEntity>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
474
    }
475
476
    /**
477
     * @return ArrayCollection
478
     */
479
    public function getGroups()
480
    {
481
        return $this->groups;
482
    }
483
484
    public function setGroups(ArrayCollection $groups)
485
    {
486
        $this->groups = $groups;
487
    }
488
489
    /**
490
     * UserEntity is the 'Owning side'
491
     * @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#owning-and-inverse-side-on-a-manytomany-association
492
     * @param GroupEntity $group
493
     */
494
    public function addGroup(GroupEntity $group)
495
    {
496
        $group->addUser($this);
497
        $this->groups[] = $group;
498
    }
499
500
    public function removeGroup(GroupEntity $group)
501
    {
502
        $group->removeUser($this);
503
        $this->groups->removeElement($group);
504
    }
505
506
    public function removeGroups()
507
    {
508
        /** @var GroupEntity $group */
509
        foreach ($this->groups as $group) {
510
            $group->removeUser($this);
511
        }
512
        $this->groups->clear();
513
    }
514
515
    /**
516
     * Callback function used to validate the activated value
517
     * @return array
518
     */
519
    public static function getActivatedValues()
520
    {
521
        return [
522
            UsersConstant::ACTIVATED_ACTIVE,
523
            UsersConstant::ACTIVATED_INACTIVE,
524
            UsersConstant::ACTIVATED_PENDING_DELETE,
525
            UsersConstant::ACTIVATED_PENDING_REG
526
        ];
527
    }
528
}
529