Completed
Pull Request — dev (#235)
by Alies
13:26 queued 06:27
created

EloquentAccountRepository::hasPermission()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repositories;
4
5
use Exception;
6
use Carbon\Carbon;
7
use App\Models\User;
8
use App\Models\SocialLogin;
9
use Illuminate\Support\Arr;
10
use Laravel\Socialite\AbstractUser;
11
use App\Exceptions\GeneralException;
12
use Illuminate\Support\Facades\Hash;
13
use App\Repositories\Contracts\UserRepository;
14
use Illuminate\Contracts\Auth\Authenticatable;
15
use App\Repositories\Contracts\AccountRepository;
16
17
/**
18
 * Class EloquentAccountRepository.
19
 */
20
class EloquentAccountRepository extends EloquentBaseRepository implements AccountRepository
21
{
22
    /**
23
     * @var UserRepository
24
     */
25
    protected $users;
26
27
    /**
28
     * EloquentUserRepository constructor.
29
     *
30
     * @param User                                       $user
31
     * @param \App\Repositories\Contracts\UserRepository $users
32
     *
33
     * @internal param \Mcamara\LaravelLocalization\LaravelLocalization
34
     *           $localization
35
     * @internal param \Illuminate\Contracts\Config\Repository $config
36
     */
37
    public function __construct(User $user, UserRepository $users)
38
    {
39
        parent::__construct($user);
40
        $this->users = $users;
41
    }
42
43
    /**
44
     * @param array $input
45
     *
46
     * @throws \Throwable
47
     * @throws \Exception
48
     *
49
     * @return \App\Models\User
50
     */
51
    public function register(array $input)
52
    {
53
        $user = $this->users->store(Arr::only($input, ['name', 'email', 'password']));
54
55
        return $user;
56
    }
57
58
    /**
59
     * @param Authenticatable $user
60
     *
61
     * @throws \App\Exceptions\GeneralException
62
     *
63
     * @return \App\Models\User
64
     */
65
    public function login(Authenticatable $user)
66
    {
67
        /* @var User $user */
68
        $user->last_access_at = Carbon::now();
69
70
        if (! $user->save()) {
71
            throw new GeneralException(__('exceptions.backend.users.update'));
72
        }
73
74
        return $user;
75
    }
76
77
    /**
78
     * @param              $provider
79
     * @param AbstractUser $data
80
     *
81
     * @throws \App\Exceptions\GeneralException
82
     *
83
     * @return User
84
     */
85
    public function findOrCreateSocial($provider, AbstractUser $data)
86
    {
87
        // Email can be not provided, so set default provider email.
88
        $user_email = $data->getEmail() ?: "{$data->getId()}@{$provider}.com";
89
90
        // Get user with this email or create new one.
91
        /** @var User $user */
92
        $user = $this->users->query()->whereEmail($user_email)->first();
93
94
        if (! $user) {
0 ignored issues
show
introduced by
$user is of type App\Models\User, thus it always evaluated to true.
Loading history...
95
            // Registration is not enabled
96
            if (! config('account.can_register')) {
97
                throw new GeneralException(__('exceptions.frontend.auth.registration_disabled'));
98
            }
99
100
            $user = $this->users->store([
101
                'name'   => $data->getName(),
102
                'email'  => $user_email,
103
                'active' => true,
104
            ], true);
105
        }
106
107
        // Save new provider if needed
108
        if (! $user->getProvider($provider)) {
109
            $user->providers()->save(new SocialLogin([
110
                'provider'    => $provider,
111
                'provider_id' => $data->getId(),
112
            ]));
113
        }
114
115
        return $user;
116
    }
117
118
    /**
119
     * @param $input
120
     *
121
     * @throws \Illuminate\Database\Eloquent\MassAssignmentException
122
     * @throws \App\Exceptions\GeneralException
123
     *
124
     * @return mixed
125
     */
126
    public function update(array $input)
127
    {
128
        if (! config('account.updating_enabled')) {
129
            throw new GeneralException(__('exceptions.frontend.user.updating_disabled'));
130
        }
131
132
        /** @var User $user */
133
        $user = auth()->user();
134
135
        $user->fill(Arr::only($input, ['name', 'email', 'locale', 'timezone']));
136
137
        return $user->save();
138
    }
139
140
    /**
141
     * @param $oldPassword
142
     * @param $newPassword
143
     *
144
     * @throws \App\Exceptions\GeneralException
145
     *
146
     * @return mixed
147
     */
148
    public function changePassword($oldPassword, $newPassword)
149
    {
150
        if (! config('account.updating_enabled')) {
151
            throw new GeneralException(__('exceptions.frontend.user.updating_disabled'));
152
        }
153
154
        /** @var User $user */
155
        $user = auth()->user();
156
157
        if (empty($user->password) || Hash::check($oldPassword, $user->password)) {
158
            $user->password = Hash::make($newPassword);
159
160
            return $user->save();
161
        }
162
163
        throw new GeneralException(__('exceptions.frontend.user.password_mismatch'));
164
    }
165
166
    /**
167
     * @throws \App\Exceptions\GeneralException|Exception
168
     *
169
     * @return mixed
170
     */
171
    public function delete()
172
    {
173
        /** @var User $user */
174
        $user = auth()->user();
175
176
        if ($user->is_super_admin) {
177
            throw new GeneralException(__('exceptions.backend.users.first_user_cannot_be_destroyed'));
178
        }
179
180
        if (! $user->delete()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user->delete() of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
181
            throw new GeneralException(__('exceptions.frontend.user.delete_account'));
182
        }
183
184
        return true;
185
    }
186
}
187