Completed
Push — master ( 694bec...006cd9 )
by Jan
04:25
created

User::setNeedPwChange()   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 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Base\NamedDBElement;
65
use App\Entity\PriceInformations\Currency;
66
use App\Security\Interfaces\HasPermissionsInterface;
67
use App\Validator\Constraints\Selectable;
68
use App\Validator\Constraints\ValidPermission;
69
use Doctrine\ORM\Mapping as ORM;
70
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
71
use Symfony\Component\Security\Core\User\UserInterface;
72
use Symfony\Component\Validator\Constraints as Assert;
73
74
/**
75
 * This entity represents a user, which can log in and have permissions.
76
 * Also this entity is able to save some informations about the user, like the names, email-address and other info.
77
 *
78
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
79
 * @ORM\Table("`users`")
80
 * @UniqueEntity("name", message="validator.user.username_already_used")
81
 */
82
class User extends NamedDBElement implements UserInterface, HasPermissionsInterface
83
{
84
    /** The User id of the anonymous user */
85
    public const ID_ANONYMOUS = 1;
86
87
    /**
88
     * @ORM\Id()
89
     * @ORM\GeneratedValue()
90
     * @ORM\Column(type="integer")
91
     */
92
    protected $id;
93
94
    /**
95
     * @ORM\Column(type="string", length=180, unique=true)
96
     * @Assert\NotBlank
97
     */
98
    protected $name = '';
99
100
    /**
101
     * //@ORM\Column(type="json").
102
     */
103
    //protected $roles = [];
104
105
    /**
106
     * @var string|null The hashed password
107
     * @ORM\Column(type="string", nullable=true)
108
     */
109
    protected $password;
110
111
    /**
112
     * @var bool True if the user needs to change password after log in
113
     * @ORM\Column(type="boolean")
114
     */
115
    protected $need_pw_change = true;
116
117
    /**
118
     * @var string|null The first name of the User
119
     * @ORM\Column(type="string", length=255, nullable=true)
120
     */
121
    protected $first_name = '';
122
123
    /**
124
     * @var string|null The last name of the User
125
     * @ORM\Column(type="string", length=255,  nullable=true)
126
     */
127
    protected $last_name = '';
128
129
    /**
130
     * @var string|null The department the user is working
131
     * @ORM\Column(type="string", length=255, nullable=true)
132
     */
133
    protected $department = '';
134
135
    /**
136
     * @var string|null The email address of the user
137
     * @ORM\Column(type="string", length=255, nullable=true)
138
     * @Assert\Email()
139
     */
140
    protected $email = '';
141
142
    /**
143
     * @var string|null The language/locale the user prefers
144
     * @ORM\Column(type="string", name="config_language", nullable=true)
145
     * @Assert\Language()
146
     */
147
    protected $language = '';
148
149
    /**
150
     * @var string|null The timezone the user prefers
151
     * @ORM\Column(type="string", name="config_timezone", nullable=true)
152
     * @Assert\Timezone()
153
     */
154
    protected $timezone = '';
155
156
    /**
157
     * @var string|null The theme
158
     * @ORM\Column(type="string", name="config_theme", nullable=true)
159
     */
160
    protected $theme = '';
161
162
    /**
163
     * @var Group|null the group this user belongs to
164
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="users", fetch="EAGER")
165
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
166
     * @Selectable()
167
     */
168
    protected $group;
169
170
    /**
171
     * @var array
172
     * @ORM\Column(type="json")
173
     */
174
    protected $settings = [];
175
176
    /**
177
     * @var Currency|null The currency the user wants to see prices in.
178
     * Dont use fetch=EAGER here, this will cause problems with setting the currency setting.
179
     * TODO: This is most likely a bug in doctrine/symfony related to the UniqueEntity constraint (it makes a db call).
180
     * TODO: Find a way to use fetch EAGER (this improves performance a bit)
181
     * @ORM\ManyToOne(targetEntity="App\Entity\PriceInformations\Currency")
182
     * @ORM\JoinColumn(name="currency_id", referencedColumnName="id")
183
     * @Selectable()
184
     */
185
    protected $currency = null;
186
187
    /** @var PermissionsEmbed
188
     * @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
189
     * @ValidPermission()
190
     */
191
    protected $permissions;
192
193
    /**
194
     * @ORM\Column(type="text", name="config_image_path")
195
     */
196
    protected $image_path = '';
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
    public function __construct()
209
    {
210
        $this->permissions = new PermissionsEmbed();
211
    }
212
213
    /**
214
     * Checks if the current user, is the user which represents the not logged in (anonymous) users.
215
     *
216
     * @return bool true if this user is the anonymous user
217
     */
218
    public function isAnonymousUser(): bool
219
    {
220
        return $this->id === static::ID_ANONYMOUS && 'anonymous' === $this->name;
221
    }
222
223
    /**
224
     * A visual identifier that represents this user.
225
     *
226
     * @see UserInterface
227
     */
228
    public function getUsername(): string
229
    {
230
        return (string) $this->name;
231
    }
232
233
    /**
234
     * @see UserInterface
235
     */
236
    public function getRoles(): array
237
    {
238
        $roles = [];
239
        //$roles = $this->roles;
240
        // guarantee every user at least has ROLE_USER
241
        $roles[] = 'ROLE_USER';
242
243
        return array_unique($roles);
244
    }
245
246
    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

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