Passed
Pull Request — master (#610)
by John
05:33
created

AAuthController::user()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 15
rs 9.9332
c 1
b 0
f 1
1
<?php
2
3
namespace App\Http\Controllers\OAuth;
4
5
use App\Models\Eloquent\User;
6
use App\Models\Eloquent\UserExtra;
7
use Illuminate\Database\QueryException;
8
use Illuminate\Http\RedirectResponse;
9
use Auth;
10
use Exception;
11
use Requests;
12
use Str;
13
use Throwable;
14
15
class AAuthController extends OAuthController
16
{
17
    public function __construct()
18
    {
19
        $this->platformName="AAuth";
20
        $this->platformID="aauth";
21
    }
22
23
    public function redirectTo()
24
    {
25
        // 2 ways to access this page, you want to bind(logined), you want to login(not logined)
26
        if (Auth::check()) {
27
            // logined user, only users with non-temp email & pass access and binded can unbind
28
            if (!Auth::user()->isIndependent()) {
0 ignored issues
show
Bug introduced by
The method isIndependent() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
            if (!Auth::user()->/** @scrutinizer ignore-call */ isIndependent()) {
Loading history...
29
                return redirect()->route('account.settings');
30
            }
31
            if (Auth::user()->getExtra('aauth_id')) {
0 ignored issues
show
Bug introduced by
The method getExtra() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
            if (Auth::user()->/** @scrutinizer ignore-call */ getExtra('aauth_id')) {
Loading history...
32
                return $this->generateOperationView(Auth::user()->getExtra('aauth_nickname'));
33
            }
34
        }
35
        return new RedirectResponse('https://cn.aauth.link/#/launch/'.config('services.aauth.client_id'));
36
    }
37
38
    private function user($code)
39
    {
40
        $response=Requests::post('https://cn.api.aauth.link/auth/', [], json_encode([
41
            'code' => $code,
42
            'app' => config('services.aauth.client_id'),
43
            'secret' => config('services.aauth.client_secret')
44
        ]));
45
        if (!$response->success) {
46
            throw new Exception('Requesting Error');
47
        }
48
        $user=json_decode($response->body);
49
        if (json_last_error()!==JSON_ERROR_NONE) {
50
            throw new Exception('JSON Error');
51
        }
52
        return $user;
53
    }
54
55
    public function handleCallback()
56
    {
57
        try {
58
            $aauth_user=$this->user(request()->code);
59
        } catch (Throwable $e) {
60
            return redirect()->route('home');
61
        }
62
63
        if (Auth::check()) {
64
            $user_id=Auth::user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
65
            $ret=UserExtra::search('aauth_id', $aauth_user->id);
66
            if (!empty($ret) && $ret[0]['uid']!=$user_id) {
67
                $user=User::find($ret[0]['uid']);
68
                return $this->generateDuplicateView($user->email);
69
            }
70
            Auth::user()->setExtra('aauth_id', $aauth_user->id);
0 ignored issues
show
Bug introduced by
The method setExtra() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            Auth::user()->/** @scrutinizer ignore-call */ setExtra('aauth_id', $aauth_user->id);
Loading history...
71
            Auth::user()->setExtra('aauth_nickname', $aauth_user->name);
72
            return $this->generateSuccessView(Auth::user()->getExtra('aauth_nickname'));
73
        } else {
74
            $ret=UserExtra::search('aauth_id', $aauth_user->id);
75
            if (!empty($ret)) {
76
                Auth::loginUsingId($ret[0]['uid'], true);
77
                Auth::user()->setExtra('aauth_nickname', $aauth_user->name);
78
                return redirect()->route('account.dashboard');
79
            } else {
80
                if (config('app.allow_oauth_temp_account')) {
81
                    try {
82
                        $createdUser=User::create([
83
                            'name' => Str::random(12),
84
                            'email' => Str::random(16)."@temporary.email",
85
                            'password' => '',
86
                            'avatar' => '/static/img/avatar/default.png',
87
                        ]);
88
                    } catch (QueryException $exception) {
89
                        return $this->generateUnknownErrorView();
90
                    }
91
                    Auth::loginUsingId($createdUser->id, true);
92
                    Auth::user()->setExtra('aauth_id', $aauth_user->id);
93
                    Auth::user()->setExtra('aauth_nickname', $aauth_user->name);
94
                    return redirect()->route('account.dashboard');
95
                }
96
                return $this->generateAccountNotFoundView();
97
            }
98
        }
99
    }
100
101
    public function unbind()
102
    {
103
        if (!Auth::check()) {
104
            return redirect()->route('home');
105
        }
106
        if (Auth::user()->getExtra('aauth_id')) {
107
            return $this->generateUnbindConfirmView(Auth::user()->email, Auth::user()->getExtra('aauth_nickname'));
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
108
        } else {
109
            return $this->generateAlreadyUnbindView();
110
        }
111
    }
112
113
    public function confirmUnbind()
114
    {
115
        if (!Auth::check()) {
116
            return redirect()->route('home');
117
        }
118
        if (Auth::user()->getExtra('aauth_id')) {
119
            Auth::user()->setExtra('aauth_id', null);
120
            Auth::user()->setExtra('aauth_nickname', null);
121
            return $this->generateUnbindSuccessView();
122
        } else {
123
            return $this->generateAlreadyUnbindView();
124
        }
125
    }
126
}
127