Test Failed
Push — master ( 93ef3c...0c96ba )
by Arthur
09:20
created

Auth0UserRepository::upsertUser()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 9
nop 1
dl 0
loc 30
rs 8.9777
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 04.10.18
6
 * Time: 17:25.
7
 */
8
9
namespace Foundation\Repositories;
10
11
use Foundation\Exceptions\Exception;
12
use Illuminate\Validation\UnauthorizedException;
13
use Modules\User\Contracts\UserServiceContract;
14
use Modules\User\Entities\User;
15
use MongoDB\BSON\ObjectId;
16
17
class Auth0UserRepository extends \Auth0\Login\Repository\Auth0UserRepository
18
{
19
    protected $service;
20
21
    /**
22
     * Auth0UserRepository constructor.
23
     *
24
     * @param $service
25
     */
26
    public function __construct(UserServiceContract $service)
27
    {
28
        $this->service = $service;
29
    }
30
31
    /* This class is used on api authN to fetch the user based on the jwt.*/
32
    public function getUserByDecodedJWT($jwt)
33
    {
34
        /*
35
         * The `sub` claim in the token represents the subject of the token
36
         * and it is always the `user_id`
37
         */
38
        $jwt->user_id = $jwt->sub;
39
40
        return $this->upsertUser($jwt);
41
    }
42
43
    public function getUserByUserInfo($userInfo)
44
    {
45
        return $this->upsertUser($userInfo['profile']);
46
    }
47
48
    protected function upsertUser($profile)
49
    {
50
        if (!isset($profile->user_id))
51
            throw new Exception("Missing token information: Auth0 user id is not set");
52
53
        $identifier = explode('|', $profile->user_id);
54
        $identityProvider = $identifier[0];
55
        $id = $identifier[1];
56
57
        $user = $this->service->find($id);
58
59
        if ($user === null || !$this->userEqualsProfile($user, $profile)) {
60
            try {
61
62
                if ($user === null)
63
                    $user = new User();
64
65
                $user->_id = new ObjectId($id);
0 ignored issues
show
Bug Best Practice introduced by
The property _id does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
66
                $user->provider = $identityProvider;
0 ignored issues
show
Bug Best Practice introduced by
The property provider does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
67
                $user->email = $profile->email;
0 ignored issues
show
Bug Best Practice introduced by
The property email does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
68
                $user->username = $profile->nickname;
0 ignored issues
show
Bug Best Practice introduced by
The property username does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
69
                $user->name = $profile->name;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
70
                $user->avatar = $profile->picture;
0 ignored issues
show
Bug Best Practice introduced by
The property avatar does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
71
                $user->save();
72
            } catch (\Exception $exception) {
73
                throw new UnauthorizedException('Profile data is not set in the token ');
74
            }
75
        }
76
77
        return $user;
78
    }
79
80
    private function userEqualsProfile($user, $profile)
81
    {
82
        return $user->username === $profile->nickname && $user->email === $profile->email && $user->name === $profile->name && $user->avatar === $profile->picture;
83
    }
84
85
    public function getUserByIdentifier($identifier)
86
    {
87
        //Get the user info of the user logged in (probably in session)
88
        $user = \App::make('auth0')->getUser();
89
90
        if ($user === null) {
0 ignored issues
show
introduced by
The condition $user === null is always false.
Loading history...
91
            return;
92
        }
93
94
        // build the user
95
        $user = $this->getUserByUserInfo($user);
96
97
        // it is not the same user as logged in, it is not valid
98
        if ($user && $user->auth0id == $identifier) {
0 ignored issues
show
Bug Best Practice introduced by
The property auth0id does not exist on Modules\User\Entities\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
99
            return $user;
100
        }
101
    }
102
}
103