Completed
Push — master ( a76360...3b8ebd )
by Manuel
04:51 queued 03:32
created

User::getAvatarAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Oscer\Cms\Core\Users\Models;
4
5
use Illuminate\Contracts\Auth\Access\Authorizable;
6
use Illuminate\Contracts\Auth\Authenticatable;
7
use Illuminate\Foundation\Auth\Access\Authorizable as AuthorizableTrait;
8
use Illuminate\Support\Carbon;
9
use Illuminate\Support\Facades\Mail;
10
use Illuminate\Support\Facades\Storage;
11
use Illuminate\Support\Str;
12
use Laravel\Sanctum\HasApiTokens;
13
use Oscer\Cms\Core\Mails\NewUserCreatedMail;
14
use Oscer\Cms\Core\Models\BaseModel;
15
use Spatie\Permission\Traits\HasRoles;
16
17
/**
18
 * @property int id
19
 * @property string name
20
 * @property string email
21
 * @property string bio
22
 * @property string avatar
23
 * @property Carbon updated_at
24
 * @property Carbon created_at
25
 */
26
class User extends BaseModel implements Authenticatable, Authorizable
27
{
28
    use HasApiTokens;
29
    use HasRoles;
30
    use AuthorizableTrait;
31
32
    /**
33
     * The attributes excluded from the model's JSON form.
34
     *
35
     * @var array
36
     */
37
    protected $hidden = ['password', 'remember_token'];
38
39
    protected $attributes = [
40
        'language' => 'en',
41
    ];
42
43
    protected $appends = ['assigned_permissions'];
44
45
    /**
46
     * The column name of the "remember me" token.
47
     *
48
     * @var string
49
     */
50
    protected $rememberTokenName = 'remember_token';
51
52
    protected static function boot()
53
    {
54
        parent::boot();
55
56
        static::creating(function (self $user) {
57
            if (! $user->password) {
58
                $password = Str::random();
59
                $user->password = $password;
60
61
                Mail::to($user->email)->send(new NewUserCreatedMail($password));
62
            }
63
        });
64
    }
65
66
    /**
67
     * Get the name of the unique identifier for the user.
68
     *
69
     * @return string
70
     */
71
    public function getAuthIdentifierName()
72
    {
73
        return $this->getKeyName();
74
    }
75
76
    /**
77
     * Get the unique identifier for the user.
78
     *
79
     * @return mixed
80
     */
81
    public function getAuthIdentifier()
82
    {
83
        return $this->{$this->getAuthIdentifierName()};
84
    }
85
86
    /**
87
     * Get the password for the user.
88
     *
89
     * @return string
90
     */
91
    public function getAuthPassword()
92
    {
93
        return $this->password;
94
    }
95
96
    /**
97
     * Get the token value for the "remember me" session.
98
     *
99
     * @return string|null
100
     */
101
    public function getRememberToken()
102
    {
103
        if (! empty($this->getRememberTokenName())) {
104
            return (string) $this->{$this->getRememberTokenName()};
105
        }
106
    }
107
108
    /**
109
     * Set the token value for the "remember me" session.
110
     *
111
     * @param string $value
112
     * @return void
113
     */
114
    public function setRememberToken($value)
115
    {
116
        if (! empty($this->getRememberTokenName())) {
117
            $this->{$this->getRememberTokenName()} = $value;
118
        }
119
    }
120
121
    /**
122
     * Get the column name for the "remember me" token.
123
     *
124
     * @return string
125
     */
126
    public function getRememberTokenName()
127
    {
128
        return $this->rememberTokenName;
129
    }
130
131
    /**
132
     * Get the author's avatar.
133
     *
134
     * @param string $value
135
     * @return string
136
     */
137
    public function getAvatarAttribute($value)
138
    {
139
        return $value
140
            ? Storage::url($value)
141
            : 'https://secure.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=80';
142
    }
143
144
    public function setPasswordAttribute($value)
145
    {
146
        $this->attributes['password'] = bcrypt($value);
147
    }
148
149
    /**
150
     * Get all permission names that are assigned to the current user.
151
     */
152
    public function getAssignedPermissionsAttribute()
153
    {
154
        return $this->getAllPermissions()
155
            ->reduce(function ($result, $permission) {
156
                $result[] = $permission->name;
157
158
                return $result;
159
            }, []);
160
    }
161
}
162