Completed
Push — master ( 195b94...7a36f1 )
by Mohamed
04:50
created

User::permission()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 9.4285
c 1
b 0
f 1
cc 3
eloc 6
nc 3
nop 1
crap 3
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 3
131
    /**
132 3
     * Collection of user permissions.
133
     *
134 3
     * @var Eloquent\Collection
135 3
     */
136 3
    protected $permission;
137 3
138
    /**
139
     * Get available languages from translations folder.
140
     *
141 3
     * @return array
142
     */
143
    public static function getLanguages()
144
    {
145
        $languages = [];
146
147
        $cdir = scandir(__DIR__ . '/../../resources/lang');
148
        foreach ($cdir as $value) {
149 3
            if (!in_array($value, ['.', '..'])) {
150
                $languages[$value] = $value;
151 3
            }
152
        }
153
154
        return $languages;
155
    }
156
157
    /**
158
     * Checks to see if $this user is current user.
159
     *
160
     * @return bool
161
     */
162 39
    public function me()
163
    {
164
        return $this->id == $this->getLoggedUser()->id;
165 39
    }
166 35
167
    /**
168
     * Whether or not the user has a permission.
169 8
     *
170
     * @param string $key
171
     *
172
     * @return bool
173
     */
174
    public function permission($key)
175
    {
176 8
        $this->loadPermissions();
177
        foreach ($this->permission as $permission) {
178
            if ($permission->permission->isEqual($key)) {
179 8
                return true;
180 5
            }
181
        }
182
183 7
        return false;
184
    }
185
186
    /**
187
     * Return user full name with property "fullname".
188
     *
189
     * @return string
190
     */
191
    public function getFullNameAttribute()
192
    {
193 54
        if (!$this->private ||
194
            (!Auth::guest() && ((int) $this->getLoggedUser()->id === (int) $this->id || $this->getLoggedUser()->permission(Permission::PERM_PROJECT_ALL)))
195 54
        ) {
196 54
            return $this->attributes['firstname'] . ' ' . $this->attributes['lastname'];
197 54
        }
198 54
199
        return trans('tinyissue.anonymous');
200
    }
201
202 26
    /**
203
     * Return user image.
204
     *
205
     * @return string
206
     */
207
    public function getImageAttribute()
208
    {
209
        return app('gravatar')->src($this->email);
210 39
    }
211
212 39
    /**
213 1
     * Returns list of user statuses.
214
     *
215
     * @return array
216 39
     */
217
    public static function getStatuses()
218
    {
219
        return [
220
            static::ACTIVE_USER   => trans('tinyissue.active'),
221
            static::BLOCKED_USER  => trans('tinyissue.blocked'),
222
            static::INACTIVE_USER => trans('tinyissue.inactive'),
223
        ];
224
    }
225
226
    /**
227
     * Whether or not the user is active.
228
     *
229
     * @return bool
230
     */
231
    public function isActive()
232
    {
233
        return (int) $this->status === static::ACTIVE_USER;
234 2
    }
235
236
    /**
237 2
     * Whether or not the user is inactive.
238 2
     *
239 2
     * @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 7
     *
249
     * @return bool
250 7
     */
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
    public function isUser()
262
    {
263
        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