Completed
Push — master ( 5049fe...5fd7f0 )
by Sherif
02:43
created

UserRepository   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 231
Duplicated Lines 15.58 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 25
c 2
b 0
f 1
lcom 0
cbo 1
dl 36
loc 231
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 4 1
A can() 0 10 2
A hasGroup() 0 5 2
A assignGroups() 10 10 1
A login() 0 15 3
A register() 0 4 1
A logout() 0 4 1
A block() 13 13 2
A unblock() 13 13 2
A isBlocked() 0 10 2
A editProfile() 0 8 1
A sendReset() 0 12 2
B resetPassword() 0 28 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace App\Modules\V1\Acl\Repositories;
2
3
use App\Modules\V1\Core\AbstractRepositories\AbstractRepository;
4
5
class UserRepository extends AbstractRepository
6
{
7
    /**
8
     * Return the model full namespace.
9
     * 
10
     * @return string
11
     */
12
    protected function getModel()
13
    {
14
        return 'App\Modules\V1\Acl\AclUser';
15
    }
16
17
    /**
18
     * Check if the logged in user or the given user 
19
     * has the given permissions on the given model.
20
     * 
21
     * @param  string  $nameOfPermission
22
     * @param  string  $model            
23
     * @param  boolean $user
24
     * @return boolean
25
     */
26
    public function can($nameOfPermission, $model, $user = false )
27
    {       
28
        $user        = $user ?: \JWTAuth::parseToken()->authenticate();
29
        $permissions = [];
30
        \Core::users()->find($user->id, ['groups.permissions'])->groups->lists('permissions')->each(function ($permission) use (&$permissions, $model){
31
            $permissions = array_merge($permissions, $permission->where('model', $model)->lists('name')->toArray()); 
32
        });
33
        
34
        return in_array($nameOfPermission, $permissions);
35
    }
36
37
    /**
38
     * Check if the logged in user has the given group.
39
     * 
40
     * @param  string  $groupName
41
     * @return boolean
42
     */
43
    public function hasGroup($groupName)
44
    {
45
        $groups = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id)->groups;
46
        return $groups->lists('name')->search($groupName, true) === false ? false : true;
47
    }
48
49
    /**
50
     * Assign the given group ids to the given user.
51
     * 
52
     * @param  integer $user_id    
53
     * @param  array   $group_ids
54
     * @return object
55
     */
56 View Code Duplication
    public function assignGroups($user_id, $group_ids)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        \DB::transaction(function () use ($user_id, $group_ids) {
59
            $user = \Core::users()->find($user_id);
60
            $user->groups()->detach();
61
            $user->groups()->attach($group_ids);
62
        });
63
64
        return \Core::users()->find($user_id);
65
    }
66
67
    /**
68
     * Handle a login request to the application.
69
     * 
70
     * @param  array $credentials    
71
     * @return string
72
     */
73
    public function login($credentials)
74
    {
75
        if ($this->isBlocked($credentials['email'])) 
76
        {
77
            \ErrorHandler::userIsBlocked();
78
        }
79
        else if ($token = \JWTAuth::attempt($credentials))
80
        {
81
            return $token;
82
        }
83
        else
84
        {
85
            \ErrorHandler::loginFailed();
86
        }
87
    }
88
89
    /**
90
     * Handle a registration request.
91
     * 
92
     * @param  array $credentials
93
     * @return string
94
     */
95
    public function register($credentials)
96
    {
97
        return \JWTAuth::fromUser(\Core::users()->model->create($credentials));
98
    }
99
100
    /**
101
     * Logout the user.
102
     * 
103
     * @return boolean
104
     */
105
    public function logout()
106
    {
107
        return \JWTAuth::invalidate(\JWTAuth::getToken());
108
    }
109
110
    /**
111
     * Block the user.
112
     *
113
     * @param  integer $user_id
114
     * @return object
115
     */
116 View Code Duplication
    public function block($user_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        if ( ! $this->hasGroup('Admin'))
119
        {
120
            \ErrorHandler::noPermissions();
121
        }
122
123
        $user          = \Core::users()->find($user_id);
124
        $user->blocked = 1;
125
        $user->save();
126
        
127
        return $user;
128
    }
129
130
    /**
131
     * Unblock the user.
132
     *
133
     * @param  integer $user_id
134
     * @return object
135
     */
136 View Code Duplication
    public function unblock($user_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
    {
138
        if ( ! $this->hasGroup('Admin'))
139
        {
140
            \ErrorHandler::noPermissions();
141
        }
142
143
        $user          = \Core::users()->find($user_id);
144
        $user->blocked = 0;
145
        $user->save();
146
147
        return $user;
148
    }
149
150
    /**
151
     * Check if the user blocked or not.
152
     *
153
     * @param  string $email
154
     * @return boolean
155
     */
156
    public function isBlocked($email)
157
    {
158
        $user = \Core::users()->first(['email' => $email]);
159
        if ( ! $user) 
160
        {
161
            \ErrorHandler::notFound('email or password');
162
        }
163
164
        return $user->blocked;
165
    }
166
167
    /**
168
     * Handle the editing of the user profile.
169
     * 
170
     * @param  array $profile
171
     * @return object
172
     */
173
    public function editProfile($profile)
174
    {
175
        unset($profile['email']);
176
        unset($profile['password']);
177
        $profile['id'] = \JWTAuth::parseToken()->authenticate()->id;
178
        
179
        return $this->save($profile);
180
    }
181
182
    /**
183
     * Send a reset link to the given user.
184
     *
185
     * @param  string  $email
186
     * @return void
187
     */
188
    public function sendReset($email)
189
    {
190
        $response = \Password::sendResetLink($email, function (\Illuminate\Mail\Message $message) {
191
            $message->subject('Your Password Reset Link');
192
        });
193
194
        switch ($response) 
195
        {
196
            case \Password::INVALID_USER:
197
                \ErrorHandler::notFound('email');
198
        }
199
    }
200
201
    /**
202
     * Reset the given user's password.
203
     *
204
     * @param  array  $credentials
205
     * @return integer
206
     */
207
    public function resetPassword($credentials)
208
    {
209
        $token    = false;
210
        $response = \Password::reset($credentials, function ($user, $password) use (&$token) {
211
            $user->password = $password;
212
            $user->save();
213
214
            $token = \JWTAuth::fromUser($user);
215
        });
216
217
218
        switch ($response) {
219
            case \Password::PASSWORD_RESET:
220
                return $token;
221
                
222
            case \Password::INVALID_TOKEN:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
223
                \ErrorHandler::invalidResetToken('token');
224
225
            case \Password::INVALID_PASSWORD:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
226
                \ErrorHandler::invalidResetPassword('email');
227
228
            case \Password::INVALID_USER:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
229
                \ErrorHandler::notFound('user');
230
231
            default:
232
                \ErrorHandler::generalError();
233
        }
234
    }
235
}
236