Completed
Push — master ( 91f8d9...64265e )
by Mohamed
09:45 queued 06:57
created

User::isActive()   A

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\Model\Project\Issue;
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
 */
39
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
40
{
41
    use Authenticatable,
42
        CanResetPassword,
43
        Traits\User\CountTrait,
44
        Traits\User\RelationTrait,
45
        Traits\User\CrudTrait,
46
        Traits\User\QueryTrait;
47
48
    /**
49
     * User name is private.
50
     *
51
     * @var int
52
     */
53
    const PRIVATE_YES = 1;
54
55
    /**
56
     * User name is public.
57
     *
58
     * @var int
59
     */
60
    const PRIVATE_NO = 0;
61
62
    /**
63
     * User status Deleted.
64
     *
65
     * @var int
66
     */
67
    const DELETED_USERS = 1;
68
69
    /**
70
     * User status not deleted.
71
     *
72
     * @var int
73
     */
74
    const NOT_DELETED_USERS = 0;
75
76
    /**
77
     * User status active. (Standard).
78
     *
79
     * @var int
80
     */
81
    const ACTIVE_USER = 1;
82
83
    /**
84
     * User status blocked. (Too many login attempts).
85
     *
86
     * @var int
87
     */
88
    const BLOCKED_USER = 2;
89
90
    /**
91
     * User status inactive. (Cannot login at the moment).
92
     *
93
     * @var int
94
     */
95
    const INACTIVE_USER = 0;
96
97
    /**
98
     * The database table used by the model.
99
     *
100
     * @var string
101
     */
102
    protected $table = 'users';
103
104
    /**
105
     * The attributes that are mass assignable.
106
     *
107
     * @var array
108
     */
109
    protected $fillable = ['deleted', 'email', 'password', 'firstname', 'lastname', 'role_id', 'private', 'language', 'status'];
110
111
    /**
112
     * The attributes excluded from the model's JSON form.
113
     *
114
     * @var array
115
     */
116
    protected $hidden = ['password', 'remember_token'];
117
118
    /**
119
     * Collection of user permissions.
120
     *
121
     * @var Eloquent\Collection
122
     */
123
    protected $permission;
124
125
    /**
126
     * Get available languages from translations folder.
127
     *
128
     * @return array
129
     */
130 3
    public static function getLanguages()
131
    {
132 3
        $languages = [];
133
134 3
        $cdir = scandir(__DIR__ . '/../../resources/lang');
135 3
        foreach ($cdir as $value) {
136 3
            if (!in_array($value, ['.', '..'])) {
137 3
                $languages[$value] = $value;
138
            }
139
        }
140
141 3
        return $languages;
142
    }
143
144
    /**
145
     * Checks to see if $this user is current user.
146
     *
147
     * @return bool
148
     */
149 3
    public function me()
150
    {
151 3
        return $this->id == \Auth::user()->id;
152
    }
153
154
    /**
155
     * Whether or not the user has a valid permission in current context
156
     * e.g. can access the issue or the project.
157
     *
158
     * @param array $params
159
     *
160
     * @return bool
161
     */
162 39
    public function permissionInContext(array $params)
163
    {
164
        // Can access all projects
165 39
        if ($this->permission(Permission::PERM_PROJECT_ALL)) {
166 35
            return true;
167
        }
168
169 8
        $project = array_get($params, 'project', function () use ($params) {
170
            $issue = array_get($params, 'issue');
171
            if ($issue instanceof Issue) {
172
                return $issue->project;
173
            }
174
175
            return;
176 8
        });
177
178
        // Is member of the project
179 8
        if ($project && !$project->isMember($this->id)) {
180 5
            return false;
181
        }
182
183 7
        return true;
184
    }
185
186
    /**
187
     * Whether or not the user has a permission.
188
     *
189
     * @param string $key
190
     *
191
     * @return bool
192
     */
193 54
    public function permission($key)
194
    {
195 54
        $this->loadPermissions();
196 54
        foreach ($this->permission as $permission) {
197 54
            if ($permission->permission->isEqual($key)) {
198 54
                return true;
199
            }
200
        }
201
202 26
        return false;
203
    }
204
205
    /**
206
     * Return user full name with property "fullname".
207
     *
208
     * @return string
209
     */
210 39
    public function getFullNameAttribute()
211
    {
212 39
        if ($this->private && (Auth::guest() || !Auth::user()->permission('administration'))) {
0 ignored issues
show
Documentation introduced by
The property private does not exist on object<Tinyissue\Model\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
213 1
            return trans('tinyissue.anonymous');
214
        }
215
216 39
        return $this->attributes['firstname'] . ' ' . $this->attributes['lastname'];
217
    }
218
219
    /**
220
     * Return user image.
221
     *
222
     * @return string
223
     */
224
    public function getImageAttribute()
225
    {
226
        return app('gravatar')->src($this->email);
227
    }
228
229
    /**
230
     * Returns list of user statuses.
231
     *
232
     * @return array
233
     */
234 2
    public static function getStatuses()
235
    {
236
        return [
237 2
            static::ACTIVE_USER     => trans('tinyissue.active'),
238 2
            static::BLOCKED_USER    => trans('tinyissue.blocked'),
239 2
            static::INACTIVE_USER   => trans('tinyissue.inactive'),
240
        ];
241
    }
242
243
    /**
244
     * Whether or not the user is active.
245
     *
246
     * @return bool
247
     */
248 7
    public function isActive()
249
    {
250 7
        return (int) $this->status === static::ACTIVE_USER;
251
    }
252
253
    /**
254
     * Whether or not the user is inactive.
255
     *
256
     * @return bool
257
     */
258
    public function isInactive()
259
    {
260
        return (int) $this->status === static::INACTIVE_USER;
261
    }
262
263
    /**
264
     * Whether or not the user is blocked.
265
     *
266
     * @return bool
267
     */
268
    public function isBlocked()
269
    {
270
        return (int) $this->status === static::BLOCKED_USER;
271
    }
272
}
273