Completed
Push — master ( 0826f5...8a4d66 )
by Jan
03:23
created

User::__toString()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * part-db version 0.1
7
 * Copyright (C) 2005 Christoph Lechner
8
 * http://www.cl-projects.de/.
9
 *
10
 * part-db version 0.2+
11
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
12
 * http://code.google.com/p/part-db/
13
 *
14
 * Part-DB Version 0.4+
15
 * Copyright (C) 2016 - 2019 Jan Böhmer
16
 * https://github.com/jbtronics
17
 *
18
 * This program is free software; you can redistribute it and/or
19
 * modify it under the terms of the GNU General Public License
20
 * as published by the Free Software Foundation; either version 2
21
 * of the License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU General Public License
29
 * along with this program; if not, write to the Free Software
30
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
31
 */
32
33
namespace App\Entity;
34
35
use App\Security\Interfaces\HasPermissionsInterface;
36
use Doctrine\ORM\Mapping as ORM;
37
use Symfony\Component\Security\Core\User\UserInterface;
38
use Symfony\Component\Validator\Constraints as Assert;
39
40
/**
41
 * This entity represents a user, which can log in and have permissions.
42
 * Also this entity is able to save some informations about the user, like the names, email-address and other info.
43
 *
44
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
45
 * @ORM\Table("users")
46
 */
47
class User extends NamedDBElement implements UserInterface, HasPermissionsInterface
48
{
49
    /** The User id of the anonymous user */
50
    public const ID_ANONYMOUS = 1;
51
52
    /**
53
     * @ORM\Id()
54
     * @ORM\GeneratedValue()
55
     * @ORM\Column(type="integer")
56
     */
57
    protected $id;
58
59
    /**
60
     * @ORM\Column(type="string", length=180, unique=true)
61
     * @Assert\NotBlank
62
     */
63
    protected $name = "";
64
65
    /**
66
     * //@ORM\Column(type="json").
67
     */
68
    //protected $roles = [];
69
70
    /**
71
     * @var string|null The hashed password
72
     * @ORM\Column(type="string", nullable=true)
73
     */
74
    protected $password;
75
76
    /**
77
     * @var bool True if the user needs to change password after log in
78
     * @ORM\Column(type="boolean")
79
     */
80
    protected $need_pw_change = true;
81
82
    /**
83
     * @var string|null The first name of the User
84
     * @ORM\Column(type="string", length=255, nullable=true)
85
     */
86
    protected $first_name = '';
87
88
    /**
89
     * @var string|null The last name of the User
90
     * @ORM\Column(type="string", length=255,  nullable=true)
91
     */
92
    protected $last_name = '';
93
94
    /**
95
     * @var string|null The department the user is working
96
     * @ORM\Column(type="string", length=255, nullable=true)
97
     */
98
    protected $department = '';
99
100
    /**
101
     * @var string|null The email address of the user
102
     * @ORM\Column(type="string", length=255, nullable=true)
103
     * @Assert\Email()
104
     */
105
    protected $email = '';
106
107
    /**
108
     * @var string|null The language/locale the user prefers
109
     * @ORM\Column(type="string", name="config_language", nullable=true)
110
     */
111
    protected $language = '';
112
113
    /**
114
     * @var string|null The timezone the user prefers
115
     * @ORM\Column(type="string", name="config_timezone", nullable=true)
116
     */
117
    protected $timezone = '';
118
119
    /**
120
     * @var string|null The theme
121
     * @ORM\Column(type="string", name="config_theme", nullable=true)
122
     */
123
    protected $theme = '';
124
125
    /**
126
     * @var Group|null the group this user belongs to
127
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="users", fetch="EAGER")
128
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
129
     */
130
    protected $group;
131
132
    /** @var PermissionsEmbed
133
     * @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
134
     */
135
    protected $permissions;
136
137
    /**
138
     * Checks if the current user, is the user which represents the not logged in (anonymous) users.
139
     *
140
     * @return bool true if this user is the anonymous user
141
     */
142
    public function isAnonymousUser(): bool
143
    {
144
        return $this->id === static::ID_ANONYMOUS && 'anonymous' === $this->name;
145
    }
146
147
    /**
148
     * A visual identifier that represents this user.
149
     *
150
     * @see UserInterface
151
     */
152
    public function getUsername(): string
153
    {
154
        return (string) $this->name;
155
    }
156
157
    /**
158
     * @see UserInterface
159
     */
160
    public function getRoles(): array
161
    {
162
        $roles = [];
163
        //$roles = $this->roles;
164
        // guarantee every user at least has ROLE_USER
165
        $roles[] = 'ROLE_USER';
166
167
        return array_unique($roles);
168
    }
169
170
    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

170
    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...
171
    {
172
        //$this->roles = $roles;
173
174
        return $this;
175
    }
176
177
    /**
178
     * @see UserInterface
179
     * Gets the password hash for this entity.
180
     */
181
    public function getPassword(): string
182
    {
183
        return (string) $this->password;
184
    }
185
186
    /**
187
     * Sets the password hash for this user.
188
     *
189
     * @param string $password
190
     *
191
     * @return User
192
     */
193
    public function setPassword(string $password): self
194
    {
195
        $this->password = $password;
196
197
        return $this;
198
    }
199
200
    /**
201
     * @see UserInterface
202
     */
203
    public function getSalt()
204
    {
205
        // not needed when using the "bcrypt" algorithm in security.yaml
206
    }
207
208
    /**
209
     * @see UserInterface
210
     */
211
    public function eraseCredentials()
212
    {
213
        // If you store any temporary, sensitive data on the user, clear it here
214
        // $this->plainPassword = null;
215
    }
216
217
    /**
218
     * Returns the ID as an string, defined by the element class.
219
     * This should have a form like P000014, for a part with ID 14.
220
     *
221
     * @return string The ID as a string;
222
     */
223
    public function getIDString(): string
224
    {
225
        return 'U'.sprintf('%06d', $this->getID());
226
    }
227
228
    public function getPermissions(): PermissionsEmbed
229
    {
230
        return $this->permissions;
231
    }
232
233
    /************************************************
234
     * Getters
235
     ************************************************/
236
237
    /**
238
     * Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)].
239
     * Example: Max Muster (m.muster).
240
     *
241
     * @param bool $including_username include the username in the full name
242
     *
243
     * @return string a string with the full name of this user
244
     */
245
    public function getFullName(bool $including_username = false): string
246
    {
247
        $str = $this->getFirstName().' '.$this->getLastName();
248
        if ($including_username) {
249
            $str .= ' ('.$this->getName().')';
250
        }
251
252
        return $str;
253
    }
254
255
    public function setName(string $new_name): NamedDBElement
256
    {
257
        // Anonymous user is not allowed to change its username
258
        if (!$this->isAnonymousUser()) {
259
            $this->name = $new_name;
260
        }
261
262
        return $this;
263
    }
264
265
    /**
266
     * @return string
267
     */
268
    public function getFirstName(): ?string
269
    {
270
        return $this->first_name;
271
    }
272
273
    /**
274
     * @param string $first_name
275
     *
276
     * @return User
277
     */
278
    public function setFirstName(?string $first_name): User
279
    {
280
        $this->first_name = $first_name;
281
282
        return $this;
283
    }
284
285
    /**
286
     * @return string
287
     */
288
    public function getLastName(): ?string
289
    {
290
        return $this->last_name;
291
    }
292
293
    /**
294
     * @param string $last_name
295
     *
296
     * @return User
297
     */
298
    public function setLastName(?string $last_name): User
299
    {
300
        $this->last_name = $last_name;
301
302
        return $this;
303
    }
304
305
    /**
306
     * @return string
307
     */
308
    public function getDepartment(): ?string
309
    {
310
        return $this->department;
311
    }
312
313
    /**
314
     * @param string $department
315
     *
316
     * @return User
317
     */
318
    public function setDepartment(?string $department): User
319
    {
320
        $this->department = $department;
321
322
        return $this;
323
    }
324
325
    /**
326
     * @return string
327
     */
328
    public function getEmail(): ?string
329
    {
330
        return $this->email;
331
    }
332
333
    /**
334
     * @param string $email
335
     *
336
     * @return User
337
     */
338
    public function setEmail(?string $email): User
339
    {
340
        $this->email = $email;
341
342
        return $this;
343
    }
344
345
    /**
346
     * @return string
347
     */
348
    public function getLanguage(): ?string
349
    {
350
        return $this->language;
351
    }
352
353
    /**
354
     * @param string $language
355
     *
356
     * @return User
357
     */
358
    public function setLanguage(?string $language): User
359
    {
360
        $this->language = $language;
361
362
        return $this;
363
    }
364
365
    /**
366
     * @return string
367
     */
368
    public function getTimezone(): ?string
369
    {
370
        return $this->timezone;
371
    }
372
373
    /**
374
     * @param string $timezone
375
     *
376
     * @return User
377
     */
378
    public function setTimezone(?string $timezone): User
379
    {
380
        $this->timezone = $timezone;
381
382
        return $this;
383
    }
384
385
    /**
386
     * @return string
387
     */
388
    public function getTheme(): ?string
389
    {
390
        return $this->theme;
391
    }
392
393
    /**
394
     * @param string $theme
395
     *
396
     * @return User
397
     */
398
    public function setTheme(?string $theme): User
399
    {
400
        $this->theme = $theme;
401
402
        return $this;
403
    }
404
405
    public function getGroup(): ?Group
406
    {
407
        return $this->group;
408
    }
409
410
    public function setGroup(?Group $group): self
411
    {
412
        $this->group = $group;
413
414
        return $this;
415
    }
416
417
    public function __toString()
418
    {
419
        return $this->getFullName(true);
420
    }
421
}
422