Completed
Push — master ( 01da70...2d3464 )
by Jan
04:14
created

User   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 436
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 67
c 2
b 0
f 0
dl 0
loc 436
rs 9.6
wmc 35

32 Methods

Rating   Name   Duplication   Size   Complexity  
A setName() 0 8 2
A getLastName() 0 3 1
A getLanguage() 0 3 1
A setPassword() 0 5 1
A setEmail() 0 5 1
A getTimezone() 0 3 1
A getPassword() 0 3 1
A getPermissions() 0 3 1
A getFullName() 0 8 2
A getSalt() 0 2 1
A setRoles() 0 5 1
A getDepartment() 0 3 1
A setDepartment() 0 5 1
A setFirstName() 0 5 1
A setGroup() 0 5 1
A eraseCredentials() 0 2 1
A setTheme() 0 5 1
A getRoles() 0 8 1
A setLanguage() 0 5 1
A getFirstName() 0 3 1
A getIDString() 0 3 1
A isAnonymousUser() 0 3 2
A setLastName() 0 5 1
A setCurrency() 0 4 1
A __construct() 0 3 1
A getTheme() 0 3 1
A __toString() 0 3 1
A getUsername() 0 3 1
A getGroup() 0 3 1
A getEmail() 0 3 1
A getCurrency() 0 3 1
A setTimezone() 0 5 1
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
     * Getters
333
     ************************************************/
334
335
    /**
336
     * Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)].
337
     * Example: Max Muster (m.muster).
338
     *
339
     * @param bool $including_username include the username in the full name
340
     *
341
     * @return string a string with the full name of this user
342
     */
343
    public function getFullName(bool $including_username = false): string
344
    {
345
        $str = $this->getFirstName().' '.$this->getLastName();
346
        if ($including_username) {
347
            $str .= ' ('.$this->getName().')';
348
        }
349
350
        return $str;
351
    }
352
353
    public function setName(string $new_name): NamedDBElement
354
    {
355
        // Anonymous user is not allowed to change its username
356
        if (!$this->isAnonymousUser()) {
357
            $this->name = $new_name;
358
        }
359
360
        return $this;
361
    }
362
363
    /**
364
     * @return string
365
     */
366
    public function getFirstName(): ?string
367
    {
368
        return $this->first_name;
369
    }
370
371
    /**
372
     * @param string $first_name
373
     *
374
     * @return User
375
     */
376
    public function setFirstName(?string $first_name): User
377
    {
378
        $this->first_name = $first_name;
379
380
        return $this;
381
    }
382
383
    /**
384
     * @return string
385
     */
386
    public function getLastName(): ?string
387
    {
388
        return $this->last_name;
389
    }
390
391
    /**
392
     * @param string $last_name
393
     *
394
     * @return User
395
     */
396
    public function setLastName(?string $last_name): User
397
    {
398
        $this->last_name = $last_name;
399
400
        return $this;
401
    }
402
403
    /**
404
     * @return string
405
     */
406
    public function getDepartment(): ?string
407
    {
408
        return $this->department;
409
    }
410
411
    /**
412
     * @param string $department
413
     *
414
     * @return User
415
     */
416
    public function setDepartment(?string $department): User
417
    {
418
        $this->department = $department;
419
420
        return $this;
421
    }
422
423
    /**
424
     * @return string
425
     */
426
    public function getEmail(): ?string
427
    {
428
        return $this->email;
429
    }
430
431
    /**
432
     * @param string $email
433
     *
434
     * @return User
435
     */
436
    public function setEmail(?string $email): User
437
    {
438
        $this->email = $email;
439
440
        return $this;
441
    }
442
443
    /**
444
     * @return string
445
     */
446
    public function getLanguage(): ?string
447
    {
448
        return $this->language;
449
    }
450
451
    /**
452
     * @param string $language
453
     *
454
     * @return User
455
     */
456
    public function setLanguage(?string $language): User
457
    {
458
        $this->language = $language;
459
460
        return $this;
461
    }
462
463
    /**
464
     * @return string
465
     */
466
    public function getTimezone(): ?string
467
    {
468
        return $this->timezone;
469
    }
470
471
    /**
472
     * @param string $timezone
473
     *
474
     * @return User
475
     */
476
    public function setTimezone(?string $timezone): User
477
    {
478
        $this->timezone = $timezone;
479
480
        return $this;
481
    }
482
483
    /**
484
     * @return string
485
     */
486
    public function getTheme(): ?string
487
    {
488
        return $this->theme;
489
    }
490
491
    /**
492
     * @param string $theme
493
     *
494
     * @return User
495
     */
496
    public function setTheme(?string $theme): User
497
    {
498
        $this->theme = $theme;
499
500
        return $this;
501
    }
502
503
    public function getGroup(): ?Group
504
    {
505
        return $this->group;
506
    }
507
508
    public function setGroup(?Group $group): self
509
    {
510
        $this->group = $group;
511
512
        return $this;
513
    }
514
515
    public function __toString()
516
    {
517
        return $this->getFullName(true);
518
    }
519
}
520