%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/diaspora/api_internal/app/Models/
Upload File :
Create Path :
Current File : /var/www/html/diaspora/api_internal/app/Models/User.php

<?php

namespace App\Models;

use App\Notifications\ResetPasswordNotification;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded;
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\InvalidBase64Data;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\Permission\Traits\HasRoles;

/**
 * @property integer id
 * @property string name
 * @property string surname
 * @property string email
 * @property string password
 * @property string country_name
 * @property boolean is_confirmed
 * @property boolean is_enabled
 * @property integer country_id
 * @property Country country
 * @property string timezone
 * @property string country_code
 * @property mixed created_at
 * @property mixed updated_at
 */
class User extends Authenticatable implements HasMedia
{
    use HasApiTokens, Notifiable, HasRoles, HasMediaTrait;
    const AVATAR = 'avatars';
    const AVATAR_DISK = 'avatars';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'surname',
        'password',
        'country_name',
        'timezone',
        'country_code',
        'email',
        'is_confirmed',
        'is_enabled',
    ];

    protected $guarded = [
        'role_id',
        'country_id',
    ];

    protected $guard_name = 'api';

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'id'           => 'integer',
        'name'         => 'string',
        'surname'      => 'string',
        'email'        => 'string',
        'country_name' => 'string',
        'country_id'   => 'integer',
        'timezone'     => 'string',
        'country_code' => 'string',
        'is_confirmed' => 'boolean',
        'is_enabled'   => 'boolean',

    ];

    public function country()
    {
        return $this->belongsTo(Country::class);
    }

    public function devices()
    {
        return $this->hasMany(Device::Class);
    }

    /**
     * @param $value
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = bcrypt($value);
    }

    /**
     * @param $value
     * @return string
     */
    public function getCreatedAtAttribute($value)
    {
        return Carbon::parse($value)
                     ->toIso8601String();
    }

    /**
     * @param $value
     * @return string
     */
    public function getUpdatedAtAttribute($value)
    {
        return Carbon::parse($value)
                     ->toIso8601String();
    }

    /**
     * @return $this
     */
    public function confirm()
    {
        $this->is_confirmed = true;
        $this->save();
        return $this;
    }

    /**
     * @return $this
     */
    public function enable()
    {
        $this->is_enabled = true;
        $this->save();
        return $this;
    }

    /**
     * @return $this
     */
    public function disable()
    {
        $this->is_enabled = false;
        $this->save();
        return $this;
    }

    /**
     * @param $password
     * @return $this
     */
    public function updatePassword($password)
    {
        $this->password = $password;
        $this->save();
        return $this;
    }

    /**
     *
     */
    public function registerMediaCollections()
    {
        $this->addMediaCollection(self::AVATAR)
             ->useDisk(self::AVATAR_DISK);
    }

    /**
     * @param $baseImage
     *
     * @return User
     * @throws FileCannotBeAdded
     * @throws InvalidBase64Data
     */
    public function uploadImage($baseImage)
    {
        $name = md5(time());
        try {
            if ($media = $this->getMedia(self::AVATAR)
                              ->first()) {
                $media->delete();
            }
            $this->addMediaFromBase64($baseImage)
                 ->usingName($name)
                 ->usingFileName("$name.png")
                 ->toMediaCollection(self::AVATAR);
            return $this;
        } catch (InvalidBase64Data $e) {
            throw $e;
        } catch (FileCannotBeAdded $e) {
            throw $e;
        }
    }

    /**
     * @return string
     */
    public function getAvatar()
    {
        $media = $this->getMedia(self::AVATAR)
                      ->first();
        if ($media) {
            return asset($media->getUrl());
        }
        return asset('svg/user.png');
    }

    public function getRoleId()
    {
        if ($role = $this->roles()
                         ->first()) {
            return $role->id;
        }

        return null;
    }

    /**
     * @param $url
     *
     * @return User
     * @throws FileCannotBeAdded
     */
    public function setAvatarFromLink($url)
    {
        $name = md5(time());
        try {
            if ($media = $this->getMedia(self::AVATAR)
                              ->first()) {
                $media->delete();
            }
            $this->addMediaFromUrl($url)
                 ->usingName($name)
                 ->usingFileName("$name.png")
                 ->toMediaCollection(self::AVATAR);
            return $this;
        } catch (FileCannotBeAdded $e) {
            throw $e;
        }
    }

    public function addDevice($uuid)
    {
        /** @var Device $device */
        $device = Device::query()
                        ->where('uuid', $uuid)
                        ->first();
        if (!$device) {
            return $this;
        }

        $device->user()
               ->associate($this);
        $device->save();
        return $this;
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordNotification($token, app()->getLocale()));
    }

    public function getBadge()
    {
        return Notification::query()
                    ->where('user_id', $this->id)
                    ->where('is_read', false)
                    ->count();
    }

}



Zerion Mini Shell 1.0