HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers47/leden.ehbocranendonck.nl/app/KommaApp/Users/Models/User.php
<?php

namespace App\KommaApp\Users\Models;

use App\KommaApp\Competences\Models\Competence;
use App\KommaApp\Courses\Models\Course;
use App\KommaApp\DisplayNameTrait;
use App\KommaApp\Documents\Kms\DocumentableInterface;
use App\KommaApp\Documents\Models\Document;
use App\KommaApp\Events\Models\Event;
use App\KommaApp\Forms\Models\Request;
use App\KommaApp\Kms\ActionLog\HasActionLogs;
use App\KommaApp\Routes\Models\Route;
use App\KommaApp\Shop\Orders\Models\Order;
use App\Notifications\KmsResetPasswordNotification;
use App\Notifications\ResetPasswordNotification;
use App\Notifications\SiteResetPasswordNotification;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Collection;


/**
 * App\KommaApp\Users\Models\User
 *
 * @property int $id
 * @property int $role_id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $gender
 * @property string $first_name
 * @property string $last_name
 * @property \Illuminate\Support\Carbon $last_login_attempt
 * @property int $failed_login_attempts
 * @property string $certificate_number
 * @property string $certificate_acquirement_date
 * @property string $certificate_valid_trough_date
 * @property string $bank_account_number
 * @property string $mobile
 * @property string $telephone
 * @property string $country
 * @property string $city
 * @property string $postal_code
 * @property string $house_number
 * @property string $street
 * @property string $birthdate
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $remember_token
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Kms\ActionLog\ActionLog[] $actions
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Images\Models\Image[] $images
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Orders\Models\Order[] $orders
 * @property-read \App\KommaApp\Users\Models\Role $role
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereBankAccountNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereBirthdate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereCertificateAcquirementDate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereCertificateNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereCertificateValidTroughDate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereCountry($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereFailedLoginAttempts($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereFirstName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereHouseNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereLastLoginAttempt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereLastName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User wherePostalCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereRoleId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereStreet($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereTelephone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereUsername($value)
 * @mixin \Eloquent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Competences\Models\Competence[] $competences
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Courses\Models\Course[] $courses
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Users\Models\Role[] $roles
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Documents\Models\Document[] $documents
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Users\Models\User whereGender($value)
 */
class User extends Authenticatable implements DocumentableInterface {
    use Notifiable;
    use HasActionLogs;
    use DisplayNameTrait;

    public const PASSWORD_COLUMN_NAME = 'password';

//    protected $admin = 1;

    protected $kmsClass = User::class;

    /*
    * Transient properties on Eloquent models
    * These are not saved to database.
    */
    public $thumbnail = false;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

    protected $fillable = [
        'username',
        'password',
        'email',
        'gender',
        'first_name',
        'last_name',
        'last_login_attempt',
        'failed_login_attempts',
        'certificate_number',
        'certificate_acquirement_date',
        'certificate_valid_trough_date',
        'bank_account_number',
        'mobile',
        'telephone',
        'country',
        'city',
        'postal_code',
        'house_number',
        'street',
        'birthdate',
    ];

    public function documents(): MorphMany
    {
        return $this->morphMany(Document::class, 'documentable');
    }

    /**
     * Send the password reset notification.
     *
     * @param  string $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        /** @var Route $currentRoute */
        if(strpos(url()->current(), 'kms') === false) {
            //Send site reset mail
            $this->notify(new SiteResetPasswordNotification($token));
        } else {
            //Send kms reset mail
            $this->notify(new KmsResetPasswordNotification($token));
        }
    }

    /**
     * Returns the role of the user
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    /**
     * Checks if the users role is at least the specified role
     * @param Role|int $atLeastRole
     * @return bool
     */
    public function isAtLeast($atLeastRole)
    {
        if(!is_a($atLeastRole, Role::class) && !is_int($atLeastRole)) throw new \RuntimeException('The role to check for must be either an instance of "'.Role::class.'" or an int representing a roles value. But was a '.gettype($atLeastRole));
        if(is_a($atLeastRole, Role::class)) $atLeastRole = $atLeastRole->value;

        $isAtLeast = false;
        $this->roles->each(function(Role $role) use(&$isAtLeast, $atLeastRole) {
            if($role->id <= $atLeastRole) {
                $isAtLeast = true;
                return false; //break out of each loop
            }
        });

        return $isAtLeast;
    }

    /**
     * Checks if the users role is at least the specified role
     * @param Role|int $role
     * @return bool
     */
    public function hasRole($role)
    {
        if(! $role) return $role;
        if(!is_a($role, Role::class) && !is_int($role)) throw new \RuntimeException('The role to check for must be either an instance of "'.Role::class.'" or an int representing a roles value. But was a '.gettype($role));
        if(is_a($role, Role::class)) $role = $role->value;

        $hasRole = false;
        $this->roles->each(function(Role $userRole) use(&$hasRole, $role) {
            if($userRole->id == $role) {
                $hasRole = true;
                return false; //break out of each loop
            }
        });

        return $hasRole;
    }

    /**
     * Gets the role with the most / best rights that the user has
     *
     * @return Role
     */
    public function mostPrivilegedRole(): ?Role
    {
        return $this->roles->sortBy('value')->first();
    }

    public function competences():BelongsToMany
    {
        return $this->BelongsToMany(Competence::class)
            ->withPivot(['acquired_at', 'expires_at', 'previous_acquired_at', 'previous_expires_at'])
            ->orderBy('competence_id');
    }

    /**
     * Gets the number of year that a user has the certificate
     *
     * @return int
     */
    public function yearsCertified()
    {
        return Carbon::parse($this->certificate_acquirement_date)->diffInYears(Carbon::now());
    }


    public function courses():BelongsToMany
    {
        return $this->belongsToMany(Course::class);
    }

    public function events():BelongsToMany
    {
        return $this->belongsToMany(Event::class);
    }

    /**
     * Shop Orders
     *
     * @return HasMany
     */
    public function orders():HasMany
    {
        return $this->hasMany(Order::class);
    }

    public function getAuthIdentifierName()
    {
        return $this->getKeyName();
    }

    public function __get($key)
    {
        if($key == "title") {
            return $this->first_name." ".$this->last_name;
        }
        return parent::__get($key);
    }
}