Passed
Push — master ( 8e61b0...7390f2 )
by Jan
05:14
created

User::getSalt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

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

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