User::getImageAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Contracts\Auth\Authenticatable as AuthenticatableContract;
18
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
19
use Illuminate\Database\Eloquent;
20
use Illuminate\Database\Eloquent\Model;
21
use Tinyissue\Extensions\Auth\LoggedUser;
22
23
/**
24
 * User is model class for users.
25
 *
26
 * @author Mohamed Alsharaf <[email protected]>
27
 *
28
 * @property int $id
29
 * @property int $deleted
30
 * @property int $role_id
31
 * @property string $language
32
 * @property string $email
33
 * @property string $password
34
 * @property string $firstname
35
 * @property string $lastname
36
 * @property string $fullname
37
 * @property int $status
38
 * @property int $private
39
 * @property Role $role
40
 * @property Eloquent\Collection $comments
41
 * @property Eloquent\Collection $issuesCreatedBy
42
 * @property Eloquent\Collection $issuesClosedBy
43
 * @property Eloquent\Collection $issuesUpdatedBy
44
 * @property Eloquent\Collection $attachments
45
 * @property Eloquent\Collection $projects
46
 * @property Eloquent\Collection $issues
47
 * @property Eloquent\Collection $permissions
48
 *
49
 * @method User updateOrCreate(array $where, array $input)
50
 */
51
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
52
{
53
    use Authenticatable,
54
        CanResetPassword,
55
        Traits\User\CountTrait,
56
        Traits\User\RelationTrait,
57
        Traits\User\CrudTrait,
58
        Traits\User\QueryTrait,
59
        LoggedUser;
60
61
    /**
62
     * User name is private.
63
     *
64
     * @var int
65
     */
66
    const PRIVATE_YES = 1;
67
68
    /**
69
     * User name is public.
70
     *
71
     * @var int
72
     */
73
    const PRIVATE_NO = 0;
74
75
    /**
76
     * User status Deleted.
77
     *
78
     * @var int
79
     */
80
    const DELETED_USERS = 1;
81
82
    /**
83
     * User status not deleted.
84
     *
85
     * @var int
86
     */
87
    const NOT_DELETED_USERS = 0;
88
89
    /**
90
     * User status active. (Standard).
91
     *
92
     * @var int
93
     */
94
    const ACTIVE_USER = 1;
95
96
    /**
97
     * User status blocked. (Too many login attempts).
98
     *
99
     * @var int
100
     */
101
    const BLOCKED_USER = 2;
102
103
    /**
104
     * User status inactive. (Cannot login at the moment).
105
     *
106
     * @var int
107
     */
108
    const INACTIVE_USER = 0;
109
110
    /**
111
     * The database table used by the model.
112
     *
113
     * @var string
114
     */
115
    protected $table = 'users';
116
117
    /**
118
     * The attributes that are mass assignable.
119
     *
120
     * @var array
121
     */
122
    protected $fillable = ['deleted', 'email', 'password', 'firstname', 'lastname', 'role_id', 'private', 'language', 'status'];
123
124
    /**
125
     * The attributes excluded from the model's JSON form.
126
     *
127
     * @var array
128
     */
129
    protected $hidden = ['password', 'remember_token'];
130
131
    /**
132
     * Collection of user permissions.
133
     *
134
     * @var Eloquent\Collection
135
     */
136
    protected $permission;
137
138
    /**
139
     * Get available languages from translations folder.
140
     *
141
     * @return array
142
     */
143 5
    public static function getLanguages()
144
    {
145 5
        $languages = [];
146
147 5
        $cdir = scandir(__DIR__ . '/../../resources/lang');
148 5
        foreach ($cdir as $value) {
149 5
            if (!in_array($value, ['.', '..'])) {
150 5
                $languages[$value] = $value;
151
            }
152
        }
153
154 5
        return $languages;
155
    }
156
157
    /**
158
     * Checks to see if $this user is current user.
159
     *
160
     * @return bool
161
     */
162 3
    public function me()
163
    {
164 3
        return $this->id == $this->getLoggedUser()->id;
165
    }
166
167
    /**
168
     * Whether or not the user has a permission.
169
     *
170
     * @param string $key
171
     *
172
     * @return bool
173
     */
174 65
    public function permission($key)
175
    {
176 65
        $this->loadPermissions();
177 65
        foreach ($this->permission as $permission) {
178 65
            if ($permission->permission->isEqual($key)) {
179 65
                return true;
180
            }
181
        }
182
183 31
        return false;
184
    }
185
186
    /**
187
     * Return user full name with property "fullname".
188
     *
189
     * @return string
190
     */
191 48
    public function getFullNameAttribute()
192
    {
193 48
        if (!$this->private ||
194 48
            (!Auth::guest() && ((int) $this->getLoggedUser()->id === (int) $this->id || $this->getLoggedUser()->permission(Permission::PERM_PROJECT_ALL)))
195
        ) {
196 48
            return $this->attributes['firstname'] . ' ' . $this->attributes['lastname'];
197
        }
198
199 2
        return trans('tinyissue.anonymous');
200
    }
201
202
    /**
203
     * Return user image.
204
     *
205
     * @return string
206
     */
207 6
    public function getImageAttribute()
208
    {
209 6
        return app('gravatar')->src($this->email);
210
    }
211
212
    /**
213
     * Returns list of user statuses.
214
     *
215
     * @return array
216
     */
217 2
    public static function getStatuses()
218
    {
219
        return [
220 2
            static::ACTIVE_USER   => trans('tinyissue.active'),
221 2
            static::BLOCKED_USER  => trans('tinyissue.blocked'),
222 2
            static::INACTIVE_USER => trans('tinyissue.inactive'),
223
        ];
224
    }
225
226
    /**
227
     * Whether or not the user is active.
228
     *
229
     * @return bool
230
     */
231 7
    public function isActive()
232
    {
233 7
        return (int) $this->status === static::ACTIVE_USER;
234
    }
235
236
    /**
237
     * Whether or not the user is inactive.
238
     *
239
     * @return bool
240
     */
241
    public function isInactive()
242
    {
243
        return (int) $this->status === static::INACTIVE_USER;
244
    }
245
246
    /**
247
     * Whether or not the user is blocked.
248
     *
249
     * @return bool
250
     */
251
    public function isBlocked()
252
    {
253
        return (int) $this->status === static::BLOCKED_USER;
254
    }
255
256
    /**
257
     * Whether or not the user is normal user role.
258
     *
259
     * @return bool
260
     */
261 40
    public function isUser()
262
    {
263 40
        return $this->exists && $this->role->role === Role::ROLE_USER;
264
    }
265
266
    /**
267
     * Whether or not the user is administrator.
268
     *
269
     * @return bool
270
     */
271
    public function isAdmin()
272
    {
273
        return $this->exists && $this->role->role === Role::ROLE_ADMIN;
274
    }
275
}
276