Completed
Push — master ( bcdba8...408d98 )
by Jan
03:54
created

User::getUsername()   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\Base\NamedDBElement;
65
use App\Security\Interfaces\HasPermissionsInterface;
66
use Doctrine\ORM\Mapping as ORM;
67
use Symfony\Component\Security\Core\User\UserInterface;
68
use Symfony\Component\Validator\Constraints as Assert;
69
70
/**
71
 * This entity represents a user, which can log in and have permissions.
72
 * Also this entity is able to save some informations about the user, like the names, email-address and other info.
73
 *
74
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
75
 * @ORM\Table("`users`")
76
 */
77
class User extends NamedDBElement implements UserInterface, HasPermissionsInterface
78
{
79
    /** The User id of the anonymous user */
80
    public const ID_ANONYMOUS = 1;
81
82
    /**
83
     * @ORM\Id()
84
     * @ORM\GeneratedValue()
85
     * @ORM\Column(type="integer")
86
     */
87
    protected $id;
88
89
    /**
90
     * @ORM\Column(type="string", length=180, unique=true)
91
     * @Assert\NotBlank
92
     */
93
    protected $name = "";
94
95
    /**
96
     * //@ORM\Column(type="json").
97
     */
98
    //protected $roles = [];
99
100
    /**
101
     * @var string|null The hashed password
102
     * @ORM\Column(type="string", nullable=true)
103
     */
104
    protected $password;
105
106
    /**
107
     * @var bool True if the user needs to change password after log in
108
     * @ORM\Column(type="boolean")
109
     */
110
    protected $need_pw_change = true;
111
112
    /**
113
     * @var string|null The first name of the User
114
     * @ORM\Column(type="string", length=255, nullable=true)
115
     */
116
    protected $first_name = '';
117
118
    /**
119
     * @var string|null The last name of the User
120
     * @ORM\Column(type="string", length=255,  nullable=true)
121
     */
122
    protected $last_name = '';
123
124
    /**
125
     * @var string|null The department the user is working
126
     * @ORM\Column(type="string", length=255, nullable=true)
127
     */
128
    protected $department = '';
129
130
    /**
131
     * @var string|null The email address of the user
132
     * @ORM\Column(type="string", length=255, nullable=true)
133
     * @Assert\Email()
134
     */
135
    protected $email = '';
136
137
    /**
138
     * @var string|null The language/locale the user prefers
139
     * @ORM\Column(type="string", name="config_language", nullable=true)
140
     */
141
    protected $language = '';
142
143
    /**
144
     * @var string|null The timezone the user prefers
145
     * @ORM\Column(type="string", name="config_timezone", nullable=true)
146
     */
147
    protected $timezone = '';
148
149
    /**
150
     * @var string|null The theme
151
     * @ORM\Column(type="string", name="config_theme", nullable=true)
152
     */
153
    protected $theme = '';
154
155
    /**
156
     * @var Group|null the group this user belongs to
157
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="users", fetch="EAGER")
158
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
159
     */
160
    protected $group;
161
162
    /** @var PermissionsEmbed
163
     * @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
164
     */
165
    protected $permissions;
166
167
    /**
168
     * @ORM\Column(type="string", name="config_currency")
169
     */
170
    protected $currency;
171
172
    /**
173
     * @ORM\Column(type="text", name="config_image_path")
174
     */
175
    protected $image_path;
176
177
    /**
178
     * @ORM\Column(type="text", name="config_instock_comment_w")
179
     */
180
    protected $instock_comment_w;
181
182
    /**
183
     * @ORM\Column(type="text", name="config_instock_comment_a")
184
     */
185
    protected $instock_comment_a;
186
187
    /**
188
     * Checks if the current user, is the user which represents the not logged in (anonymous) users.
189
     *
190
     * @return bool true if this user is the anonymous user
191
     */
192
    public function isAnonymousUser(): bool
193
    {
194
        return $this->id === static::ID_ANONYMOUS && 'anonymous' === $this->name;
195
    }
196
197
    /**
198
     * A visual identifier that represents this user.
199
     *
200
     * @see UserInterface
201
     */
202
    public function getUsername(): string
203
    {
204
        return (string) $this->name;
205
    }
206
207
    /**
208
     * @see UserInterface
209
     */
210
    public function getRoles(): array
211
    {
212
        $roles = [];
213
        //$roles = $this->roles;
214
        // guarantee every user at least has ROLE_USER
215
        $roles[] = 'ROLE_USER';
216
217
        return array_unique($roles);
218
    }
219
220
    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

220
    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...
221
    {
222
        //$this->roles = $roles;
223
224
        return $this;
225
    }
226
227
    /**
228
     * @see UserInterface
229
     * Gets the password hash for this entity.
230
     */
231
    public function getPassword(): string
232
    {
233
        return (string) $this->password;
234
    }
235
236
    /**
237
     * Sets the password hash for this user.
238
     *
239
     * @param string $password
240
     *
241
     * @return User
242
     */
243
    public function setPassword(string $password): self
244
    {
245
        $this->password = $password;
246
247
        return $this;
248
    }
249
250
    /**
251
     * @see UserInterface
252
     */
253
    public function getSalt()
254
    {
255
        // not needed when using the "bcrypt" algorithm in security.yaml
256
    }
257
258
    /**
259
     * @see UserInterface
260
     */
261
    public function eraseCredentials()
262
    {
263
        // If you store any temporary, sensitive data on the user, clear it here
264
        // $this->plainPassword = null;
265
    }
266
267
    /**
268
     * Returns the ID as an string, defined by the element class.
269
     * This should have a form like P000014, for a part with ID 14.
270
     *
271
     * @return string The ID as a string;
272
     */
273
    public function getIDString(): string
274
    {
275
        return 'U'.sprintf('%06d', $this->getID());
276
    }
277
278
    public function getPermissions(): PermissionsEmbed
279
    {
280
        return $this->permissions;
281
    }
282
283
    /************************************************
284
     * Getters
285
     ************************************************/
286
287
    /**
288
     * Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)].
289
     * Example: Max Muster (m.muster).
290
     *
291
     * @param bool $including_username include the username in the full name
292
     *
293
     * @return string a string with the full name of this user
294
     */
295
    public function getFullName(bool $including_username = false): string
296
    {
297
        $str = $this->getFirstName().' '.$this->getLastName();
298
        if ($including_username) {
299
            $str .= ' ('.$this->getName().')';
300
        }
301
302
        return $str;
303
    }
304
305
    public function setName(string $new_name): NamedDBElement
306
    {
307
        // Anonymous user is not allowed to change its username
308
        if (!$this->isAnonymousUser()) {
309
            $this->name = $new_name;
310
        }
311
312
        return $this;
313
    }
314
315
    /**
316
     * @return string
317
     */
318
    public function getFirstName(): ?string
319
    {
320
        return $this->first_name;
321
    }
322
323
    /**
324
     * @param string $first_name
325
     *
326
     * @return User
327
     */
328
    public function setFirstName(?string $first_name): User
329
    {
330
        $this->first_name = $first_name;
331
332
        return $this;
333
    }
334
335
    /**
336
     * @return string
337
     */
338
    public function getLastName(): ?string
339
    {
340
        return $this->last_name;
341
    }
342
343
    /**
344
     * @param string $last_name
345
     *
346
     * @return User
347
     */
348
    public function setLastName(?string $last_name): User
349
    {
350
        $this->last_name = $last_name;
351
352
        return $this;
353
    }
354
355
    /**
356
     * @return string
357
     */
358
    public function getDepartment(): ?string
359
    {
360
        return $this->department;
361
    }
362
363
    /**
364
     * @param string $department
365
     *
366
     * @return User
367
     */
368
    public function setDepartment(?string $department): User
369
    {
370
        $this->department = $department;
371
372
        return $this;
373
    }
374
375
    /**
376
     * @return string
377
     */
378
    public function getEmail(): ?string
379
    {
380
        return $this->email;
381
    }
382
383
    /**
384
     * @param string $email
385
     *
386
     * @return User
387
     */
388
    public function setEmail(?string $email): User
389
    {
390
        $this->email = $email;
391
392
        return $this;
393
    }
394
395
    /**
396
     * @return string
397
     */
398
    public function getLanguage(): ?string
399
    {
400
        return $this->language;
401
    }
402
403
    /**
404
     * @param string $language
405
     *
406
     * @return User
407
     */
408
    public function setLanguage(?string $language): User
409
    {
410
        $this->language = $language;
411
412
        return $this;
413
    }
414
415
    /**
416
     * @return string
417
     */
418
    public function getTimezone(): ?string
419
    {
420
        return $this->timezone;
421
    }
422
423
    /**
424
     * @param string $timezone
425
     *
426
     * @return User
427
     */
428
    public function setTimezone(?string $timezone): User
429
    {
430
        $this->timezone = $timezone;
431
432
        return $this;
433
    }
434
435
    /**
436
     * @return string
437
     */
438
    public function getTheme(): ?string
439
    {
440
        return $this->theme;
441
    }
442
443
    /**
444
     * @param string $theme
445
     *
446
     * @return User
447
     */
448
    public function setTheme(?string $theme): User
449
    {
450
        $this->theme = $theme;
451
452
        return $this;
453
    }
454
455
    public function getGroup(): ?Group
456
    {
457
        return $this->group;
458
    }
459
460
    public function setGroup(?Group $group): self
461
    {
462
        $this->group = $group;
463
464
        return $this;
465
    }
466
467
    public function __toString()
468
    {
469
        return $this->getFullName(true);
470
    }
471
}
472