Completed
Branch develop-3.0 (5d2048)
by Mohamed
02:55
created

User::updater()   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 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 Illuminate\Auth\Authenticatable;
15
use Illuminate\Auth\Passwords\CanResetPassword;
16
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
17
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
18
use Illuminate\Database\Eloquent;
19
use Illuminate\Foundation\Auth\Access\Authorizable;
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
 *
47
 * @method Eloquent\Collection getProjects()
48
 * @method Eloquent\Collection getProjectsWithSettings()
49
 * @method Eloquent\Collection getActiveUsers()
50
 * @method Eloquent\Collection getProjectsWithRecentActivities()
51
 * @method Eloquent\Collection getProjectsWithRecentIssues()
52
 * @method Eloquent\Collection getIssuesGroupByTags(Eloquent\Collection $tagIds, $projectId = null)
53
 * @method Eloquent\Collection getProjectsWithOpenIssuesCount($status = Project::STATUS_OPEN)
54
 * @method int countNotDeleted()
55
 * @method int countDeleted()
56
 * @method int createdIssuesCount($projectId = 0)
57
 * @method int countProjectsByStatus($status)
58
 * @method User updateOrCreate(array $attributes, array $values = [])
59
 * @method $this private($status = false)
60
 * @method $this notPrivate()
61
 * @method $this developerOrHigher()
62
 * @method $this active()
63
 * @method $this removed()
64
 * @method $this notMemberOfProject(Project $project)
65
 */
66
class User extends ModelAbstract implements AuthenticatableContract, CanResetPasswordContract
67
{
68
    use Authenticatable,
69
        CanResetPassword,
70
        UserRelations,
71
        UserScopes,
72
        LoggedUser,
73
        Authorizable;
74
75
    /**
76
     * User name is private.
77
     *
78
     * @var int
79
     */
80
    const PRIVATE_YES = 1;
81
82
    /**
83
     * User name is public.
84
     *
85
     * @var int
86
     */
87
    const PRIVATE_NO = 0;
88
89
    /**
90
     * User status Deleted.
91
     *
92
     * @var int
93
     */
94
    const DELETED_USERS = 1;
95
96
    /**
97
     * User status not deleted.
98
     *
99
     * @var int
100
     */
101
    const NOT_DELETED_USERS = 0;
102
103
    /**
104
     * User status active. (Standard).
105
     *
106
     * @var int
107
     */
108
    const ACTIVE_USER = 1;
109
110
    /**
111
     * User status blocked. (Too many login attempts).
112
     *
113
     * @var int
114
     */
115
    const BLOCKED_USER = 2;
116
117
    /**
118
     * User status inactive. (Cannot login at the moment).
119
     *
120
     * @var int
121
     */
122
    const INACTIVE_USER = 0;
123
124
    /**
125
     * The database table used by the model.
126
     *
127
     * @var string
128
     */
129
    protected $table = 'users';
130
131
    /**
132
     * The attributes that are mass assignable.
133
     *
134
     * @var array
135
     */
136
    protected $fillable = ['deleted', 'email', 'password', 'firstname', 'lastname', 'role_id', 'private', 'language', 'status'];
137
138
    /**
139
     * @param User|null $user
140
     *
141
     * @return \Tinyissue\Repository\User\Updater
142
     */
143
    public function updater(User $user = null)
144
    {
145
        return parent::updater($user);
146
    }
147
148
    /**
149
     * The attributes excluded from the model's JSON form.
150
     *
151
     * @var array
152
     */
153
    protected $hidden = ['password', 'remember_token'];
154
155
    /**
156
     * Checks to see if $this user is current user.
157
     *
158
     * @return bool
159
     */
160
    public function me()
161
    {
162
        return $this->id == $this->getLoggedUser()->id;
163
    }
164
165
    /**
166
     * Return user full name with property "fullname".
167
     *
168
     * @return string
169
     */
170
    public function getFullNameAttribute()
171
    {
172
        if ((int) $this->private === self::PRIVATE_NO || ($this->isLoggedIn() && $this->getLoggedUser()->can('viewName', $this))) {
173
            return $this->attributes['firstname'] . ' ' . $this->attributes['lastname'];
174
        }
175
176
        return trans('tinyissue.anonymous');
177
    }
178
179
    /**
180
     * Return user image.
181
     *
182
     * @return string
183
     */
184
    public function getImageAttribute()
185
    {
186
        return app('gravatar')->src($this->email);
187
    }
188
189
    /**
190
     * Returns list of user statuses.
191
     *
192
     * @return array
193
     */
194
    public static function getStatuses()
195
    {
196
        return [
197
            static::ACTIVE_USER   => trans('tinyissue.active'),
198
            static::BLOCKED_USER  => trans('tinyissue.blocked'),
199
            static::INACTIVE_USER => trans('tinyissue.inactive'),
200
        ];
201
    }
202
203
    /**
204
     * Whether or not the user is active.
205
     *
206
     * @return bool
207
     */
208
    public function isActive()
209
    {
210
        return (int) $this->status === static::ACTIVE_USER;
211
    }
212
213
    /**
214
     * Whether or not the user is inactive.
215
     *
216
     * @return bool
217
     */
218
    public function isInactive()
219
    {
220
        return (int) $this->status === static::INACTIVE_USER;
221
    }
222
223
    /**
224
     * Whether or not the user is blocked.
225
     *
226
     * @return bool
227
     */
228
    public function isBlocked()
229
    {
230
        return (int) $this->status === static::BLOCKED_USER;
231
    }
232
233
    /**
234
     * Whether or not the user is normal user role.
235
     *
236
     * @return bool
237
     */
238
    public function isUser()
239
    {
240
        return $this->exists && $this->role->role === Role::ROLE_USER;
241
    }
242
243
    /**
244
     * Whether or not the user is administrator.
245
     *
246
     * @return bool
247
     */
248
    public function isAdmin()
249
    {
250
        return $this->exists && $this->role->role === Role::ROLE_ADMIN;
251
    }
252
253
    /**
254
     * Whether or not the user is manager.
255
     *
256
     * @return bool
257
     */
258
    public function isManager()
259
    {
260
        return $this->exists && $this->role->role === Role::ROLE_MANAGER;
261
    }
262
263
    /**
264
     * Whether or not the user is developer.
265
     *
266
     * @return bool
267
     */
268
    public function isDeveloper()
269
    {
270
        return $this->exists && $this->role->role === Role::ROLE_DEVELOPER;
271
    }
272
273
    /**
274
     * Whether or not the user is manager or admin.
275
     *
276
     * @return bool
277
     */
278
    public function isManagerOrMore()
279
    {
280
        return $this->isAdmin() || $this->isManager();
281
    }
282
283
    /**
284
     * Whether or not the user is developer or manager or admin.
285
     *
286
     * @return bool
287
     */
288
    public function isDeveloperOrMore()
289
    {
290
        return $this->isAdmin() || $this->isManager() || $this->isDeveloper();
291
    }
292
293
    /**
294
     * Get user role name.
295
     *
296
     * @return string
297
     */
298
    public function getRoleName()
299
    {
300
        return $this->role->role;
301
    }
302
}
303