Passed
Push — master ( 10f39b...f31336 )
by Jan
03:26
created

User::getEmail()   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 declare(strict_types=1);
2
3
/**
4
 *
5
 * part-db version 0.1
6
 * Copyright (C) 2005 Christoph Lechner
7
 * http://www.cl-projects.de/
8
 *
9
 * part-db version 0.2+
10
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
11
 * http://code.google.com/p/part-db/
12
 *
13
 * Part-DB Version 0.4+
14
 * Copyright (C) 2016 - 2019 Jan Böhmer
15
 * https://github.com/jbtronics
16
 *
17
 * This program is free software; you can redistribute it and/or
18
 * modify it under the terms of the GNU General Public License
19
 * as published by the Free Software Foundation; either version 2
20
 * of the License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
30
 *
31
 */
32
33
namespace App\Entity;
34
35
use App\Entity\Embeddables\PermissionEntity;
0 ignored issues
show
Bug introduced by
The type App\Entity\Embeddables\PermissionEntity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
use App\Security\Interfaces\HasPermissionsInterface;
37
use Doctrine\ORM\Mapping as ORM;
38
use Symfony\Component\Security\Core\User\UserInterface;
39
use Symfony\Component\Validator\Constraints as Assert;
40
41
/**
42
 * This entity represents a user, which can log in and have permissions.
43
 * Also this entity is able to save some informations about the user, like the names, email-address and other info.
44
 *
45
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
46
 * @ORM\Table("users")
47
 */
48
class User extends NamedDBElement implements UserInterface, HasPermissionsInterface
49
{
50
    /** The User id of the anonymous user */
51
    const ID_ANONYMOUS      = 1;
52
53
    /**
54
     * @ORM\Id()
55
     * @ORM\GeneratedValue()
56
     * @ORM\Column(type="integer")
57
     */
58
    protected $id;
59
60
    /**
61
     * @ORM\Column(type="string", length=180, unique=true)
62
     * @Assert\NotBlank
63
     */
64
    protected $name;
65
66
    /**
67
     * //@ORM\Column(type="json")
68
     */
69
    //protected $roles = [];
70
71
    /**
72
     * @var string|null The hashed password
73
     * @ORM\Column(type="string", nullable=true)
74
     */
75
    protected $password;
76
77
    /**
78
     * @var bool True if the user needs to change password after log in
79
     * @ORM\Column(type="boolean")
80
     */
81
    protected $need_pw_change;
82
83
    /**
84
     * @var string|null The first name of the User
85
     * @ORM\Column(type="string", length=255, nullable=true)
86
     */
87
    protected $first_name = "";
88
89
    /**
90
     * @var string|null The last name of the User
91
     * @ORM\Column(type="string", length=255,  nullable=true)
92
     */
93
    protected $last_name = "";
94
95
    /**
96
     * @var string|null The department the user is working
97
     * @ORM\Column(type="string", length=255, nullable=true)
98
     */
99
    protected $department = "";
100
101
102
    /**
103
     * @var string|null The email address of the user
104
     * @ORM\Column(type="string", length=255, nullable=true)
105
     * @Assert\Email()
106
     */
107
    protected $email = "";
108
109
    /**
110
     * @var string|null The language/locale the user prefers
111
     * @ORM\Column(type="string", name="config_language", nullable=true)
112
     */
113
    protected $language = "";
114
115
    /**
116
     * @var string|null The timezone the user prefers
117
     * @ORM\Column(type="string", name="config_timezone", nullable=true)
118
     */
119
    protected $timezone = "";
120
121
    /**
122
     * @var string|null The theme
123
     * @ORM\Column(type="string", name="config_theme", nullable=true)
124
     */
125
    protected $theme = "";
126
127
    /**
128
     * @var Group|null The group this user belongs to.
129
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="users", fetch="EAGER")
130
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
131
     */
132
    protected $group;
133
134
    /** @var PermissionsEmbed
135
     * @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
136
     */
137
    protected $permissions;
138
139
140
    /**
141
     * Checks if the current user, is the user which represents the not logged in (anonymous) users.
142
     * @return bool True if this user is the anonymous user.
143
     */
144
    public function isAnonymousUser() : bool
145
    {
146
        return $this->id === static::ID_ANONYMOUS && $this->name === 'anonymous';
147
    }
148
149
    /**
150
     * A visual identifier that represents this user.
151
     *
152
     * @see UserInterface
153
     */
154
    public function getUsername(): string
155
    {
156
        return (string) $this->name;
157
    }
158
159
    /**
160
     * @see UserInterface
161
     */
162
    public function getRoles(): array
163
    {
164
        $roles = [];
165
        //$roles = $this->roles;
166
        // guarantee every user at least has ROLE_USER
167
        $roles[] = 'ROLE_USER';
168
169
        return array_unique($roles);
170
    }
171
172
    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

172
    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...
173
    {
174
        //$this->roles = $roles;
175
176
        return $this;
177
    }
178
179
    /**
180
     * @see UserInterface
181
     * Gets the password hash for this entity.
182
     */
183
    public function getPassword(): string
184
    {
185
        return (string) $this->password;
186
    }
187
188
    /**
189
     * Sets the password hash for this user.
190
     * @param string $password
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
     * @return string The ID as a string;
221
     */
222
    public function getIDString(): string
223
    {
224
        return 'U' . sprintf('%06d', $this->getID());
225
    }
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
     * @param bool $including_username Include the username in the full name.
241
     * @return string A string with the full name of this user.
242
     */
243
    public function getFullName(bool $including_username = false)
244
    {
245
        $str = $this->getFirstName() . ' ' . $this->getLastName();
246
        if ($including_username) {
247
            $str .= ' (' . $this->getName() . ')';
248
        }
249
250
        return $str;
251
    }
252
253
254
    public function setName(string $new_name) : NamedDBElement
255
    {
256
        // Anonymous user is not allowed to change its username
257
        if(!$this->isAnonymousUser()) {
258
            $this->name = $new_name;
259
        }
260
261
        return $this;
262
    }
263
264
    /**
265
     * @return string
266
     */
267
    public function getFirstName(): ?string
268
    {
269
        return $this->first_name;
270
    }
271
272
    /**
273
     * @param string $first_name
274
     * @return User
275
     */
276
    public function setFirstName(?string $first_name): User
277
    {
278
        $this->first_name = $first_name;
279
        return $this;
280
    }
281
282
    /**
283
     * @return string
284
     */
285
    public function getLastName(): ?string
286
    {
287
        return $this->last_name;
288
    }
289
290
    /**
291
     * @param string $last_name
292
     * @return User
293
     */
294
    public function setLastName(?string $last_name): User
295
    {
296
        $this->last_name = $last_name;
297
        return $this;
298
    }
299
300
    /**
301
     * @return string
302
     */
303
    public function getDepartment(): ?string
304
    {
305
        return $this->department;
306
    }
307
308
    /**
309
     * @param string $department
310
     * @return User
311
     */
312
    public function setDepartment(?string $department): User
313
    {
314
        $this->department = $department;
315
        return $this;
316
    }
317
318
    /**
319
     * @return string
320
     */
321
    public function getEmail(): ?string
322
    {
323
        return $this->email;
324
    }
325
326
    /**
327
     * @param string $email
328
     * @return User
329
     */
330
    public function setEmail(?string $email): User
331
    {
332
        $this->email = $email;
333
        return $this;
334
    }
335
336
    /**
337
     * @return string
338
     */
339
    public function getLanguage(): ?string
340
    {
341
        return $this->language;
342
    }
343
344
    /**
345
     * @param string $language
346
     * @return User
347
     */
348
    public function setLanguage(?string $language): User
349
    {
350
        $this->language = $language;
351
        return $this;
352
    }
353
354
    /**
355
     * @return string
356
     */
357
    public function getTimezone(): ?string
358
    {
359
        return $this->timezone;
360
    }
361
362
    /**
363
     * @param string $timezone
364
     * @return User
365
     */
366
    public function setTimezone(?string $timezone): User
367
    {
368
        $this->timezone = $timezone;
369
        return $this;
370
    }
371
372
    /**
373
     * @return string
374
     */
375
    public function getTheme(): ?string
376
    {
377
        return $this->theme;
378
    }
379
380
    /**
381
     * @param string $theme
382
     * @return User
383
     */
384
    public function setTheme(?string $theme): User
385
    {
386
        $this->theme = $theme;
387
        return $this;
388
    }
389
390
    public function getGroup(): ?Group
391
    {
392
        return $this->group;
393
    }
394
395
    public function setGroup(?Group $group): self
396
    {
397
        $this->group = $group;
398
        return $this;
399
    }
400
401
}
402