HEX
Server: Apache
System: Linux WWW 6.1.0-40-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.153-1 (2025-09-20) x86_64
User: web11 (1011)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/ivs.kaunokolegija.lt/laravel/app/Models/User.php
<?php

namespace App\Models;

use Hash;
use Carbon\Carbon;
use DateTimeInterface;
use App\Traits\Auditable;
use Illuminate\Support\Facades\Http;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Google_Client;

class User extends Authenticatable
{
    use SoftDeletes, Notifiable, Auditable, HasFactory;

    public $table = 'users';

    protected $hidden = [
        'remember_token',
        'password',
    ];

    protected $dates = [
        'email_verified_at',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $fillable = [
        'name',
        'email',
        'phone',
        'email_verified_at',
        'password',
        'remember_token',
        'created_at',
        'updated_at',
        'deleted_at',
        'google_token',
        'google_refresh_token',
        'google_token_expires_at'
    ];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function getIsAdminAttribute()
    {
        return $this->roles()->where('id', 1)->exists();
    }

    public function getEmailVerifiedAtAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('panel.date_format') . ' ' . config('panel.time_format')) : null;
    }

    public function setEmailVerifiedAtAttribute($value)
    {
        $this->attributes['email_verified_at'] = $value ? Carbon::createFromFormat(config('panel.date_format') . ' ' . config('panel.time_format'), $value)->format('Y-m-d H:i:s') : null;
    }

    public function setPasswordAttribute($input)
    {
        if ($input) {
            $this->attributes['password'] = app('hash')->needsRehash($input) ? Hash::make($input) : $input;
        }
    }

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPassword($token));
    }

    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    public function departments()
    {
        return $this->belongsToMany(Department::class);
    }

    public function departmentsubsections()
    {
        return $this->belongsToMany(DepartmentSubsection::class);
    }

    public function isGoogleTokenValid()
    {
        if (!$this->google_token || !$this->google_refresh_token) {
            return false;
        }
    
        $response = Http::get('https://www.googleapis.com/oauth2/v1/tokeninfo', [
            'access_token' => $this->google_token,
        ]);

        if ($response->failed() || $response->json()['expires_in'] < 300) {
            return $this->refreshGoogleToken();
        }

        return true;
    }

    public function refreshGoogleToken()
    {
        if (!$this->google_refresh_token) {
            \Log::error('No refresh token found for user.');
            return false;
        }

        $response = Http::asForm()->post('https://oauth2.googleapis.com/token', [
            'client_id' => config('services.google.client_id'),
            'client_secret' => config('services.google.client_secret'),
            'refresh_token' => $this->google_refresh_token,
            'grant_type' => 'refresh_token',
        ]);

        $data = $response->json();

        if ($response->successful() && isset($data['access_token'])) {
                $this->update([
                'google_token' => $data['access_token'],
            ]);

            \Log::info('Google token refreshed successfully.');
            return true;
        }

        \Log::error('Failed to refresh Google token: ' . json_encode($data));
        return false;
    }



    public function sharedFutureActivities()
    {
        return $this->belongsToMany(FutureActivityPlanning::class, 'future_activity_planning_user', 'user_id', 'future_activity_planning_id');
    }

}