Completed
Push — develop ( bffb2a...35cb48 )
by
unknown
07:30
created

User::getLogin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
6
 * @license       MIT
7
 */
8
9
namespace Auth\Entity;
10
11
use Core\Entity\AbstractIdentifiableEntity;
12
use Core\Entity\Collection\ArrayCollection;
13
use Core\Entity\DraftableEntityInterface;
14
use Doctrine\Common\Collections\Collection;
15
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
16
use Organizations\Entity\OrganizationReferenceInterface;
17
use Settings\Repository\SettingsEntityResolver;
18
19
/**
20
 * Defines an user model
21
 *
22
 * @ODM\Document(collection="users", repositoryClass="Auth\Repository\User")
23
 */
24
class User extends AbstractIdentifiableEntity implements UserInterface, DraftableEntityInterface
25
{
26
    /**
27
     * Users login name
28
     *
29
     * @var string
30
     * @ODM\Field(type="string")
31
     * @ODM\Index(unique=true, sparse=true, order="asc")
32
     */
33
    protected $login;
34
35
    /**
36
     * Role of an user. Currently "user" or "recruiter"
37
     *
38
     * @ODM\Field(type="string")
39
     */
40
    protected $role;
41
42
    /**
43
     * Users contact data.
44
     *
45
     * @ODM\EmbedOne(targetDocument="Info")
46
     */
47
    protected $info;
48
49
    /**
50
     * Authentification Sessions like oAuth
51
     * After Authentification with OAuth sessions can be stored like a password/key pair
52
     *
53
     * @ODM\EmbedMany(targetDocument="AuthSession")
54
     */
55
    protected $authSessions;
56
57
    /**
58
     * Users login password
59
     *
60
     * @ODM\Field(type="string")
61
     */
62
    protected $credential;
63
64
    /**
65
     * Users primary email address
66
     *
67
     * @ODM\Field(type="string")
68
     */
69
    protected $email;
70
71
    /**
72
     * pre-shared key, which allows an external application to authenticate
73
     *
74
     * @var String
75
     * @ODM\Field(type="string")
76
     */
77
    protected $secret;
78
79
    /**
80
     * Can contain various HybridAuth profiles.
81
     * Deprecated: replaced by User::$profiles
82
     *
83
     * @var array
84
     * @deprecated
85
     * @ODM\Hash
86
     */
87
    protected $profile = array();
88
89
    /**
90
     * Can contain various HybridAuth profiles.
91
     *
92
     * @var array
93
     * @ODM\Hash
94
     */
95
    protected $profiles = [];
96
97
    /** @var array
98
     * @ODM\EmbedMany(discriminatorField="_entity")
99
     */
100
    protected $settings;
101
102
    /**
103
     * This is not a persistent property!
104
     *
105
     * @var SettingsEntityResolver
106
     */
107
    protected $settingsEntityResolver;
108
109
    /**
110
     * User groups.
111
     *
112
     * @var Collection
113
     * @ODM\ReferenceMany(targetDocument="Group", mappedBy="owner", simple=true, cascade="all")
114
     */
115
    protected $groups;
116
117
    /**
118
     * User tokens. Is generated when recovering Passwords as a short term key.
119
     *
120
     * @var Collection
121
     * @ODM\EmbedMany(targetDocument="Token")
122
     */
123
    protected $tokens;
124
125
    /**
126
     * The organization reference for the user.
127
     *
128
     * This field is not stored in the database, but injected on postLoad via
129
     * {@link \Organizations\Repository\Event\InjectOrganizationReferenceListener}
130
     *
131
     * @var OrganizationReferenceInterface
132
     *
133
     * @since 0.18
134
     */
135
    protected $organization;
136
137
    /**
138
     * Is this entity a draft or not?
139
     *
140
     * @var bool
141
     * @ODM\Boolean
142
     */
143
    protected $isDraft = false;
144
145
    /**
146
     * @see http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/best-practices.html
147
     * It is recommended best practice to initialize any business collections in documents in the constructor.
148
     * {mg: What about lazy loading? Initialize the Collection in the getter, if none is set? Reduce overload.}
149
     */
150
    public function __construct()
151
    {
152
    }
153
154
    /**
155
     * @return bool
156
     */
157
    public function isDraft()
158
    {
159
        return $this->isDraft;
160
    }
161
162
    /**
163
     * @param bool $flag
164
     * @return $this
165
     */
166
    public function setIsDraft($flag)
167
    {
168
        $this->isDraft = (bool) $flag;
169
170
        return $this;
171
    }
172
173
174
    /** {@inheritdoc} */
175
    public function setLogin($login)
176
    {
177
        $this->login = trim((String)$login);
178
        return $this;
179
    }
180
181
    /** {@inheritdoc} */
182
    public function getLogin()
183
    {
184
        return $this->login;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function setRole($role)
191
    {
192
        $this->role = $role;
193
        return $this;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function getRole()
200
    {
201
        if (!$this->role) {
202
            $this->setRole('user');
203
        }
204
        return $this->role;
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function getRoleId()
211
    {
212
        return $this->getRole();
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function setInfo(InfoInterface $info)
219
    {
220
        $this->info = $info;
221
        return $this;
222
    }
223
224
    /** {@inheritdoc} */
225
    public function getInfo()
226
    {
227
        if (null == $this->info) {
228
            $this->setInfo(new Info());
229
        }
230
        return $this->info;
231
    }
232
233
    /**
234
     * @param $key
235
     * @param $sessionParameter
236
     * @return $this
237
     */
238
    public function updateAuthSession($key, $sessionParameter)
239
    {
240
        $notExists = true;
241
242
        foreach ($this->authSessions as $authSession) {
243
            /* @var $authSession AuthSession */
244
            if ($key == $authSession->getName()) {
245
                $authSession->setSession($sessionParameter);
246
                $notExists = false;
247
            }
248
        }
249
        if ($notExists) {
250
            $authSession = new AuthSession();
251
            $authSession->setName($key);
252
            $authSession->setSession($sessionParameter);
253
            $this->authSessions[] = $authSession;
254
        }
255
        return $this;
256
    }
257
258
    /**
259
     * @param $key
260
     * @return null
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
261
     */
262
    public function getAuthSession($key)
263
    {
264
        $result = null;
265
266
        foreach ($this->authSessions as $authSession) {
267
            /* @var $authSession AuthSession */
268
            if ($key == $authSession->getName()) {
269
                $result = $authSession->getSession();
270
            }
271
        }
272
        return $result;
273
    }
274
275
    /**
276
     * removes a stored Session
277
     * @param string|null $key providerName, if null, remove all sessions
278
     * @return $this
279
     */
280
    public function removeSessionData($key = null)
281
    {
282
        $authSessionRefresh = array();
283
        foreach ($this->authSessions as $authSession) {
284
            /* @var $authSession AuthSession */
285
            if (isset($key) && $key != $authSession->getName()) {
286
                $authSessionRefresh[] = $authSession;
287
            }
288
        }
289
        $this->authSessions = $authSessionRefresh;
290
        return $this;
291
    }
292
293
    /** {@inheritdoc} */
294
    public function getCredential()
295
    {
296
        return $this->credential;
297
    }
298
299
    /** {@inheritdoc} */
300
    public function setPassword($password)
301
    {
302
        $filter = new Filter\CredentialFilter();
303
        $credential = $filter->filter($password);
304
        return $this->setCredential($credential);
305
    }
306
307
    /** {@inheritdoc} */
308
    public function setCredential($credential)
309
    {
310
        $this->credential = $credential;
311
        return $this;
312
    }
313
314
    /** {@inheritdoc} */
315
    public function getSecret()
316
    {
317
        if (isset($this->secret)) {
318
            return $this->secret;
319
        }
320
        return $this->credential;
321
    }
322
323
    /** {@inheritdoc} */
324
    public function setSecret($secret)
325
    {
326
        $this->secret = $secret;
327
        return $this;
328
    }
329
330
    /** {@inheritdoc} */
331
    public function getEmail()
332
    {
333
        return $this->email ?: $this->getInfo()->getEmail();
334
    }
335
336
    /** {@inheritdoc} */
337
    public function setEmail($email)
338
    {
339
        $this->email = $email;
340
        return $this;
341
    }
342
343
    /** {@inheritdoc} */
344
    public function setProfile(array $profile)
345
    {
346
        $this->profile = $profile;
0 ignored issues
show
Deprecated Code introduced by
The property Auth\Entity\User::$profile has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
347
        return $this;
348
    }
349
350
    /** {@inheritdoc} */
351
    public function getProfile($provider = null)
352
    {
353
        if (!isset($provider))
354
        {
355
            return $this->profiles;
356
        }
357
        
358
        return isset($this->profiles[$provider]) ? $this->profiles[$provider] : [];
359
    }
360
361
    /**
362
     * @param string $provider
363
     * @param array $data
364
     * @return \Auth\Entity\User
365
     */
366
    public function addProfile($provider, array $data)
367
    {
368
        $this->profiles[$provider] = $data;
369
        return $this;
370
    }
371
    
372
    /**
373
     * @param string $provider
374
     * @return \Auth\Entity\User
375
     */
376
    public function removeProfile($provider)
377
    {
378
        unset($this->profiles[$provider]);
379
        return $this;
380
    }
381
382
    /** {@inheritdoc} */
383
    public function setSettingsEntityResolver($resolver)
384
    {
385
        $this->settingsEntityResolver = $resolver;
386
    }
387
388
    /** {@inheritdoc} */
389
    public function getSettings($module)
390
    {
391
        if (!isset($module)) {
392
            throw new \InvalidArgumentException('$module must not be null.');
393
        }
394
395
        if (!$this->settings) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->settings of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
396
            $this->settings = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Core\Entity\Collection\ArrayCollection() of type object<Core\Entity\Collection\ArrayCollection> is incompatible with the declared type array of property $settings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
397
        }
398
399
        foreach ($this->settings as $settings) {
400
            if ($settings->moduleName == $module) {
401
                return $settings;
402
            }
403
        }
404
405
        $settings = $this->settingsEntityResolver->getNewSettingsEntity($module);
406
        $this->settings->add($settings);
407
        return $settings;
408
    }
409
410
    /** {@inheritdoc} */
411
    public function getGroups()
412
    {
413
        if (!$this->groups) {
414
            $this->groups = new ArrayCollection();
415
        }
416
        return $this->groups;
417
    }
418
419
    /** {@inheritdoc} */
420
    public function getGroup($name, $create = false)
421
    {
422
        $groups = $this->getGroups();
423
        foreach ($groups as $group) {
424
            /* @var $group GroupInterface */
425
            if ($group->getName() == $name) {
426
                return $group;
427
            }
428
        }
429
        if ($create) {
430
            $group = new Group($name, $this);
431
            $groups->add($group);
432
            return $group;
433
        }
434
        return null;
435
    }
436
437
    /**
438
     * @return Collection
439
     */
440
    public function getTokens()
441
    {
442
        if (!$this->tokens) {
443
            $this->tokens = new ArrayCollection();
444
        }
445
446
        return $this->tokens;
447
    }
448
449
    /**
450
     * @param Collection $tokens
451
     */
452
    public function setTokens($tokens)
453
    {
454
        $this->tokens = $tokens;
455
    }
456
457
    /**
458
     * @param OrganizationReferenceInterface $organization
459
     * @return $this
460
     */
461
    public function setOrganization(OrganizationReferenceInterface $organization)
462
    {
463
        $this->organization = $organization;
464
465
        return $this;
466
    }
467
468
    /**
469
     * @return bool
470
     */
471
    public function hasOrganization()
472
    {
473
        /* @var $this->organization \Organizations\Entity\OrganizationReference */
474
        return $this->organization &&
475
               $this->organization->hasAssociation();
476
    }
477
478
    /**
479
     * @return OrganizationReferenceInterface
480
     */
481
    public function getOrganization()
482
    {
483
        return $this->organization;
484
    }
485
}
486