Passed
Push — master ( 0716b8...6a0d02 )
by Jan
04:25
created

User::setLastName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
declare(strict_types=1);
23
24
/**
25
 * part-db version 0.1
26
 * Copyright (C) 2005 Christoph Lechner
27
 * http://www.cl-projects.de/.
28
 *
29
 * part-db version 0.2+
30
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
31
 * http://code.google.com/p/part-db/
32
 *
33
 * Part-DB Version 0.4+
34
 * Copyright (C) 2016 - 2019 Jan Böhmer
35
 * https://github.com/jbtronics
36
 *
37
 * This program is free software; you can redistribute it and/or
38
 * modify it under the terms of the GNU General Public License
39
 * as published by the Free Software Foundation; either version 2
40
 * of the License, or (at your option) any later version.
41
 *
42
 * This program is distributed in the hope that it will be useful,
43
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45
 * GNU General Public License for more details.
46
 *
47
 * You should have received a copy of the GNU General Public License
48
 * along with this program; if not, write to the Free Software
49
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
50
 */
51
52
namespace App\Entity\UserSystem;
53
54
use App\Entity\Attachments\AttachmentContainingDBElement;
55
use App\Entity\Attachments\UserAttachment;
56
use App\Entity\Base\NamedDBElement;
57
use App\Entity\PriceInformations\Currency;
58
use App\Security\Interfaces\HasPermissionsInterface;
59
use App\Validator\Constraints\Selectable;
60
use App\Validator\Constraints\ValidPermission;
61
use Doctrine\Common\Collections\Collection;
62
use Doctrine\ORM\Mapping as ORM;
63
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
64
use Symfony\Component\Security\Core\User\UserInterface;
65
use Symfony\Component\Validator\Constraints as Assert;
66
67
/**
68
 * This entity represents a user, which can log in and have permissions.
69
 * Also this entity is able to save some informations about the user, like the names, email-address and other info.
70
 * Also this entity is able to save some informations about the user, like the names, email-address and other info.
71
 *
72
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
73
 * @ORM\Table("`users`")
74
 * @UniqueEntity("name", message="validator.user.username_already_used")
75
 */
76
class User extends AttachmentContainingDBElement implements UserInterface, HasPermissionsInterface
77
{
78
    /** The User id of the anonymous user */
79
    public const ID_ANONYMOUS = 1;
80
81
    public const AVAILABLE_THEMES = ['bootstrap', 'cerulean', 'cosmo', 'cyborg', 'darkly', 'flatly', 'journal',
82
        'litera', 'lumen', 'lux', 'materia', 'minty', 'pulse', 'sandstone', 'simplex', 'sketchy', 'slate', 'solar',
83
        'spacelab', 'united', 'yeti', ];
84
85
    /**
86
     * @var Collection|UserAttachment[]
87
     * @ORM\OneToMany(targetEntity="App\Entity\Attachments\UserAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
88
     */
89
    protected $attachments;
90
91
    /**
92
     * @ORM\Id()
93
     * @ORM\GeneratedValue()
94
     * @ORM\Column(type="integer")
95
     */
96
    protected $id;
97
98
    /**
99
     * @ORM\Column(type="string", length=180, unique=true)
100
     * @Assert\NotBlank
101
     */
102
    protected $name = '';
103
104
    /**
105
     * //@ORM\Column(type="json").
106
     */
107
    //protected $roles = [];
108
109
    /**
110
     * @var string|null The hashed password
111
     * @ORM\Column(type="string", nullable=true)
112
     */
113
    protected $password;
114
115
    /**
116
     * @var bool True if the user needs to change password after log in
117
     * @ORM\Column(type="boolean")
118
     */
119
    protected $need_pw_change = true;
120
121
    /**
122
     * @var string|null The first name of the User
123
     * @ORM\Column(type="string", length=255, nullable=true)
124
     */
125
    protected $first_name = '';
126
127
    /**
128
     * @var string|null The last name of the User
129
     * @ORM\Column(type="string", length=255,  nullable=true)
130
     */
131
    protected $last_name = '';
132
133
    /**
134
     * @var string|null The department the user is working
135
     * @ORM\Column(type="string", length=255, nullable=true)
136
     */
137
    protected $department = '';
138
139
    /**
140
     * @var string|null The email address of the user
141
     * @ORM\Column(type="string", length=255, nullable=true)
142
     * @Assert\Email()
143
     */
144
    protected $email = '';
145
146
    /**
147
     * @var string|null The language/locale the user prefers
148
     * @ORM\Column(type="string", name="config_language", nullable=true)
149
     * @Assert\Language()
150
     */
151
    protected $language = '';
152
153
    /**
154
     * @var string|null The timezone the user prefers
155
     * @ORM\Column(type="string", name="config_timezone", nullable=true)
156
     * @Assert\Timezone()
157
     */
158
    protected $timezone = '';
159
160
    /**
161
     * @var string|null The theme
162
     * @ORM\Column(type="string", name="config_theme", nullable=true)
163
     * @Assert\Choice(choices=User::AVAILABLE_THEMES)
164
     */
165
    protected $theme = '';
166
167
    /**
168
     * @var Group|null the group this user belongs to
169
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="users", fetch="EAGER")
170
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
171
     * @Selectable()
172
     */
173
    protected $group;
174
175
    /**
176
     * @var array
177
     * @ORM\Column(type="json")
178
     */
179
    protected $settings = [];
180
181
    /**
182
     * @var Currency|null The currency the user wants to see prices in.
183
     *                    Dont use fetch=EAGER here, this will cause problems with setting the currency setting.
184
     *                    TODO: This is most likely a bug in doctrine/symfony related to the UniqueEntity constraint (it makes a db call).
185
     *                    TODO: Find a way to use fetch EAGER (this improves performance a bit)
186
     * @ORM\ManyToOne(targetEntity="App\Entity\PriceInformations\Currency")
187
     * @ORM\JoinColumn(name="currency_id", referencedColumnName="id")
188
     * @Selectable()
189
     */
190
    protected $currency = null;
191
192
    /** @var PermissionsEmbed
193
     * @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
194
     * @ValidPermission()
195
     */
196
    protected $permissions;
197
198
    /**
199
     * @ORM\Column(type="text", name="config_instock_comment_w")
200
     */
201
    protected $instock_comment_w = '';
202
203
    /**
204
     * @ORM\Column(type="text", name="config_instock_comment_a")
205
     */
206
    protected $instock_comment_a = '';
207
208
    /**
209
     * @var string|null The hash of a token the user must provide when he wants to reset his password.
210
     * @ORM\Column(type="string", nullable=true)
211
     */
212
    protected $pw_reset_token = null;
213
214
    /**
215
     * @var \DateTime The time until the password reset token is valid.
216
     * @ORM\Column(type="datetime", nullable=true)
217
     */
218
    protected $pw_reset_expires = null;
219
220
    /**
221
     * @var bool Determines if the user is disabled (user can not log in)
222
     * @ORM\Column(type="boolean")
223
     */
224
    protected $disabled = false;
225
226
    public function __construct()
227
    {
228
        parent::__construct();
229
        $this->permissions = new PermissionsEmbed();
230
    }
231
232
    /**
233
     * Checks if the current user, is the user which represents the not logged in (anonymous) users.
234
     *
235
     * @return bool true if this user is the anonymous user
236
     */
237
    public function isAnonymousUser(): bool
238
    {
239
        return $this->id === static::ID_ANONYMOUS && 'anonymous' === $this->name;
240
    }
241
242
    /**
243
     * A visual identifier that represents this user.
244
     *
245
     * @see UserInterface
246
     */
247
    public function getUsername(): string
248
    {
249
        return (string) $this->name;
250
    }
251
252
    /**
253
     * @see UserInterface
254
     */
255
    public function getRoles(): array
256
    {
257
        $roles = [];
258
        //$roles = $this->roles;
259
        // guarantee every user at least has ROLE_USER
260
        $roles[] = 'ROLE_USER';
261
262
        return array_unique($roles);
263
    }
264
265
    public function setRoles(array $roles): self
0 ignored issues
show
Unused Code introduced by
The parameter $roles is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

265
    public function setRoles(/** @scrutinizer ignore-unused */ array $roles): self

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
266
    {
267
        //$this->roles = $roles;
268
269
        return $this;
270
    }
271
272
    /**
273
     * @see UserInterface
274
     * Gets the password hash for this entity.
275
     */
276
    public function getPassword(): string
277
    {
278
        return (string) $this->password;
279
    }
280
281
    /**
282
     * Sets the password hash for this user.
283
     *
284
     * @param string $password
285
     * @return User
286
     */
287
    public function setPassword(string $password): self
288
    {
289
        $this->password = $password;
290
291
        return $this;
292
    }
293
294
    /**
295
     * @see UserInterface
296
     */
297
    public function getSalt()
298
    {
299
        // not needed when using the "bcrypt" algorithm in security.yaml
300
    }
301
302
    /**
303
     * @see UserInterface
304
     */
305
    public function eraseCredentials()
306
    {
307
        // If you store any temporary, sensitive data on the user, clear it here
308
        // $this->plainPassword = null;
309
    }
310
311
    /**
312
     * Gets the currency the user prefers when showing him prices.
313
     *
314
     * @return Currency|null The currency the user prefers, or null if the global currency should be used.
315
     */
316
    public function getCurrency(): ?Currency
317
    {
318
        return $this->currency;
319
    }
320
321
    /**
322
     * Sets the currency the users prefers to see prices in.
323
     *
324
     * @param Currency|null $currency
325
     * @return User
326
     */
327
    public function setCurrency(?Currency $currency): self
328
    {
329
        $this->currency = $currency;
330
331
        return $this;
332
    }
333
334
    /**
335
     * Checks if this user is disabled (user cannot login any more).
336
     *
337
     * @return bool True, if the user is disabled.
338
     */
339
    public function isDisabled(): bool
340
    {
341
        return $this->disabled;
342
    }
343
344
    /**
345
     * Sets the status if a user is disabled.
346
     *
347
     * @param bool $disabled True if the user should be disabled.
348
     *
349
     * @return User
350
     */
351
    public function setDisabled(bool $disabled): self
352
    {
353
        $this->disabled = $disabled;
354
355
        return $this;
356
    }
357
358
    /**
359
     * Returns the ID as an string, defined by the element class.
360
     * This should have a form like P000014, for a part with ID 14.
361
     *
362
     * @return string The ID as a string;
363
     */
364
    public function getIDString(): string
365
    {
366
        return 'U'.sprintf('%06d', $this->getID());
367
    }
368
369
    public function getPermissions(): PermissionsEmbed
370
    {
371
        return $this->permissions;
372
    }
373
374
    /**
375
     * Check if the user needs a password change.
376
     *
377
     * @return bool
378
     */
379
    public function isNeedPwChange(): bool
380
    {
381
        return $this->need_pw_change;
382
    }
383
384
    /**
385
     * Set the status, if the user needs a password change.
386
     *
387
     * @param bool $need_pw_change
388
     * @return User
389
     */
390
    public function setNeedPwChange(bool $need_pw_change): self
391
    {
392
        $this->need_pw_change = $need_pw_change;
393
394
        return $this;
395
    }
396
397
    /**
398
     * Returns the encrypted password reset token
399
     * @return string|null
400
     */
401
    public function getPwResetToken(): ?string
402
    {
403
        return $this->pw_reset_token;
404
    }
405
406
    /**
407
     * Sets the encrypted password reset token
408
     * @param string|null $pw_reset_token
409
     * @return User
410
     */
411
    public function setPwResetToken(?string $pw_reset_token): User
412
    {
413
        $this->pw_reset_token = $pw_reset_token;
414
        return $this;
415
    }
416
417
    /**
418
     * Gets the datetime when the password reset token expires
419
     * @return \DateTime
420
     */
421
    public function getPwResetExpires(): \DateTime
422
    {
423
        return $this->pw_reset_expires;
424
    }
425
426
    /**
427
     * Sets the datetime when the password reset token expires
428
     * @param \DateTime $pw_reset_expires
429
     * @return User
430
     */
431
    public function setPwResetExpires(\DateTime $pw_reset_expires): User
432
    {
433
        $this->pw_reset_expires = $pw_reset_expires;
434
        return $this;
435
    }
436
437
438
439
    /************************************************
440
     * Getters
441
     ************************************************/
442
443
    /**
444
     * Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)].
445
     * Example: Max Muster (m.muster).
446
     *
447
     * @param bool $including_username include the username in the full name
448
     *
449
     * @return string a string with the full name of this user
450
     */
451
    public function getFullName(bool $including_username = false): string
452
    {
453
        if ($including_username) {
454
            return sprintf('%s %s (%s)', $this->getFirstName(), $this->getLastName(), $this->getName());
455
        }
456
457
        return sprintf('%s %s', $this->getFirstName(), $this->getLastName());
458
    }
459
460
    public function setName(string $new_name): NamedDBElement
461
    {
462
        // Anonymous user is not allowed to change its username
463
        if (!$this->isAnonymousUser()) {
464
            $this->name = $new_name;
465
        }
466
467
        return $this;
468
    }
469
470
    /**
471
     * @return string
472
     */
473
    public function getFirstName(): ?string
474
    {
475
        return $this->first_name;
476
    }
477
478
    /**
479
     * @param string $first_name
480
     *
481
     * @return User
482
     */
483
    public function setFirstName(?string $first_name): self
484
    {
485
        $this->first_name = $first_name;
486
487
        return $this;
488
    }
489
490
    /**
491
     * @return string
492
     */
493
    public function getLastName(): ?string
494
    {
495
        return $this->last_name;
496
    }
497
498
    /**
499
     * @param string $last_name
500
     *
501
     * @return User
502
     */
503
    public function setLastName(?string $last_name): self
504
    {
505
        $this->last_name = $last_name;
506
507
        return $this;
508
    }
509
510
    /**
511
     * @return string
512
     */
513
    public function getDepartment(): ?string
514
    {
515
        return $this->department;
516
    }
517
518
    /**
519
     * @param string $department
520
     *
521
     * @return User
522
     */
523
    public function setDepartment(?string $department): self
524
    {
525
        $this->department = $department;
526
527
        return $this;
528
    }
529
530
    /**
531
     * @return string
532
     */
533
    public function getEmail(): ?string
534
    {
535
        return $this->email;
536
    }
537
538
    /**
539
     * @param string $email
540
     *
541
     * @return User
542
     */
543
    public function setEmail(?string $email): self
544
    {
545
        $this->email = $email;
546
547
        return $this;
548
    }
549
550
    /**
551
     * @return string
552
     */
553
    public function getLanguage(): ?string
554
    {
555
        return $this->language;
556
    }
557
558
    /**
559
     * @param string $language
560
     *
561
     * @return User
562
     */
563
    public function setLanguage(?string $language): self
564
    {
565
        $this->language = $language;
566
567
        return $this;
568
    }
569
570
    /**
571
     * @return string
572
     */
573
    public function getTimezone(): ?string
574
    {
575
        return $this->timezone;
576
    }
577
578
    /**
579
     * @param string $timezone
580
     *
581
     * @return User
582
     */
583
    public function setTimezone(?string $timezone): self
584
    {
585
        $this->timezone = $timezone;
586
587
        return $this;
588
    }
589
590
    /**
591
     * @return string
592
     */
593
    public function getTheme(): ?string
594
    {
595
        return $this->theme;
596
    }
597
598
    /**
599
     * @param string $theme
600
     *
601
     * @return User
602
     */
603
    public function setTheme(?string $theme): self
604
    {
605
        $this->theme = $theme;
606
607
        return $this;
608
    }
609
610
    public function getGroup(): ?Group
611
    {
612
        return $this->group;
613
    }
614
615
    public function setGroup(?Group $group): self
616
    {
617
        $this->group = $group;
618
619
        return $this;
620
    }
621
622
    public function __toString()
623
    {
624
        $tmp = $this->isDisabled() ? ' [DISABLED]' : '';
625
626
        return $this->getFullName(true).$tmp;
627
    }
628
}
629