Passed
Push — master ( dd1dc5...2187f5 )
by Jan
04:27
created

User::getLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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