Completed
Branch develop-3.0 (4fe777)
by Mohamed
11:06
created

User::getRoleName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model;
13
14
use Auth as Auth;
15
use Illuminate\Auth\Authenticatable;
16
use Illuminate\Auth\Passwords\CanResetPassword;
17
use Illuminate\Database\Eloquent;
18
use Illuminate\Database\Eloquent\Model;
19
use Tinyissue\Contracts\Model\UserInterface;
20
use Tinyissue\Extensions\Auth\LoggedUser;
21
22
/**
23
 * User is model class for users.
24
 *
25
 * @author Mohamed Alsharaf <[email protected]>
26
 *
27
 * @property int $id
28
 * @property int $deleted
29
 * @property int $role_id
30
 * @property string $language
31
 * @property string $email
32
 * @property string $password
33
 * @property string $firstname
34
 * @property string $lastname
35
 * @property string $fullname
36
 * @property int $status
37
 * @property int $private
38
 * @property Role $role
39
 * @property Eloquent\Collection $comments
40
 * @property Eloquent\Collection $issuesCreatedBy
41
 * @property Eloquent\Collection $issuesClosedBy
42
 * @property Eloquent\Collection $issuesUpdatedBy
43
 * @property Eloquent\Collection $attachments
44
 * @property Eloquent\Collection $projects
45
 * @property Eloquent\Collection $issues
46
 * @property Eloquent\Collection $permissions
47
 *
48
 * @method User updateOrCreate(array $where, array $input)
49
 */
50
class User extends Model implements UserInterface
51
{
52
    use Authenticatable,
53
        CanResetPassword,
54
        UserRelations,
55
        UserScopes,
56
        LoggedUser;
57
58
    /**
59
     * User name is private.
60
     *
61
     * @var int
62
     */
63
    const PRIVATE_YES = 1;
64
65
    /**
66
     * User name is public.
67
     *
68
     * @var int
69
     */
70
    const PRIVATE_NO = 0;
71
72
    /**
73
     * User status Deleted.
74
     *
75
     * @var int
76
     */
77
    const DELETED_USERS = 1;
78
79
    /**
80
     * User status not deleted.
81
     *
82
     * @var int
83
     */
84
    const NOT_DELETED_USERS = 0;
85
86
    /**
87
     * User status active. (Standard).
88
     *
89
     * @var int
90
     */
91
    const ACTIVE_USER = 1;
92
93
    /**
94
     * User status blocked. (Too many login attempts).
95
     *
96
     * @var int
97
     */
98
    const BLOCKED_USER = 2;
99
100
    /**
101
     * User status inactive. (Cannot login at the moment).
102
     *
103
     * @var int
104
     */
105
    const INACTIVE_USER = 0;
106
107
    /**
108
     * The database table used by the model.
109
     *
110
     * @var string
111
     */
112
    protected $table = 'users';
113
114
    /**
115
     * The attributes that are mass assignable.
116
     *
117
     * @var array
118
     */
119
    protected $fillable = ['deleted', 'email', 'password', 'firstname', 'lastname', 'role_id', 'private', 'language', 'status'];
120
121
    /**
122
     * The attributes excluded from the model's JSON form.
123
     *
124
     * @var array
125
     */
126
    protected $hidden = ['password', 'remember_token'];
127
128
    /**
129
     * Collection of user permissions.
130
     *
131
     * @var Eloquent\Collection
132
     */
133
    protected $permission;
134
135
    /**
136
     * Checks to see if $this user is current user.
137
     *
138
     * @return bool
139
     */
140
    public function me()
141
    {
142
        return $this->id == $this->getLoggedUser()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Tinyissue\Contracts\Model\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
143
    }
144
145
    /**
146
     * Whether or not the user has a permission.
147
     *
148
     * @param string $key
149
     *
150
     * @return bool
151
     */
152
    public function permission($key)
153
    {
154
        $this->loadPermissions();
155
        foreach ($this->permission as $permission) {
156
            if ($permission->permission->isEqual($key)) {
157
                return true;
158
            }
159
        }
160
161
        return false;
162
    }
163
164
    /**
165
     * Load user permissions.
166
     *
167
     * @return Eloquent\Collection
168
     */
169 View Code Duplication
    protected function loadPermissions()
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...
170
    {
171
        if (null === $this->permission) {
172
            $this->permission = $this->permissions()->with('permission')->get();
173
        }
174
175
        return $this->permission;
176
    }
177
178
    /**
179
     * Return user full name with property "fullname".
180
     *
181
     * @return string
182
     */
183
    public function getFullNameAttribute()
184
    {
185
        if (!$this->private ||
186
            (!Auth::guest() && ((int) $this->getLoggedUser()->id === (int) $this->id || $this->getLoggedUser()->permission(Permission::PERM_PROJECT_ALL)))
0 ignored issues
show
Bug introduced by
Accessing id on the interface Tinyissue\Contracts\Model\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
187
        ) {
188
            return $this->attributes['firstname'] . ' ' . $this->attributes['lastname'];
189
        }
190
191
        return trans('tinyissue.anonymous');
192
    }
193
194
    /**
195
     * Return user image.
196
     *
197
     * @return string
198
     */
199
    public function getImageAttribute()
200
    {
201
        return app('gravatar')->src($this->email);
202
    }
203
204
    /**
205
     * Returns list of user statuses.
206
     *
207
     * @return array
208
     */
209
    public static function getStatuses()
210
    {
211
        return [
212
            static::ACTIVE_USER   => trans('tinyissue.active'),
213
            static::BLOCKED_USER  => trans('tinyissue.blocked'),
214
            static::INACTIVE_USER => trans('tinyissue.inactive'),
215
        ];
216
    }
217
218
    /**
219
     * Whether or not the user is active.
220
     *
221
     * @return bool
222
     */
223
    public function isActive()
224
    {
225
        return (int) $this->status === static::ACTIVE_USER;
226
    }
227
228
    /**
229
     * Whether or not the user is inactive.
230
     *
231
     * @return bool
232
     */
233
    public function isInactive()
234
    {
235
        return (int) $this->status === static::INACTIVE_USER;
236
    }
237
238
    /**
239
     * Whether or not the user is blocked.
240
     *
241
     * @return bool
242
     */
243
    public function isBlocked()
244
    {
245
        return (int) $this->status === static::BLOCKED_USER;
246
    }
247
248
    /**
249
     * Whether or not the user is normal user role.
250
     *
251
     * @return bool
252
     */
253
    public function isUser()
254
    {
255
        return $this->exists && $this->role->role === Role::ROLE_USER;
256
    }
257
258
    /**
259
     * Whether or not the user is administrator.
260
     *
261
     * @return bool
262
     */
263
    public function isAdmin()
264
    {
265
        return $this->exists && $this->role->role === Role::ROLE_ADMIN;
266
    }
267
268
    public function getRoleName()
269
    {
270
        return $this->role->role;
271
    }
272
}
273