%PDF- %PDF-
Direktori : /var/www/html/geotechnics/api/app/Models/ |
Current File : /var/www/html/geotechnics/api/app/Models/User.php |
<?php namespace App\Models; use App\Notifications\AccountConfirmed; use App\Notifications\ResetPasswordNotification; use Carbon\Carbon; use Carbon\CarbonPeriod; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; 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 boolean is_confirmed * @property boolean is_enabled * @property string confirmation_code * @property CompanyProfile companyProfile * @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', 'email', 'is_confirmed', 'confirmation_code', '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', 'is_confirmed' => 'boolean', 'is_enabled' => 'boolean', ]; public static function generateRandomToken($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } public static function getUserByConfirmationCode($confirmation_code) { return self::query() ->where('confirmation_code', $confirmation_code) ->first(); } public static function getLastMonthGroups() { $date = Carbon::now() ->subMonth() ->startOfDay(); $groups = self::query() ->whereDoesntHave('roles') ->where('created_at', '>=', $date) ->groupBy('date') ->orderBy('date', 'ASC') ->get(array( DB::raw('Date(created_at) as date'), DB::raw('COUNT(*) as "count"'), )); $range = CarbonPeriod::create($date, Carbon::now()); $data = collect(); $total = 0; foreach ($range as $date) { $d = $groups->filter(function ($g) use ($date) { return $g['date'] === $date->format('Y-m-d'); }); $count = $d->first()['count']; if (!$count) { $count = 0; } $total += $count; $data->push([ 'date' => $date->format('Y-m-d'), 'count' => $count, ]); } return [ 'groups' => $data->toArray(), 'total' => $total, ]; } public static function getAdmin() { return self::query() ->whereHas('roles', function ($q) { $q->where('name', Role::ADMIN); }) ->first(); } public function companyProfile() { return $this->hasOne(CompanyProfile::class); } public function devices() { return $this->hasMany(Device::class); } public function myProjects() { return $this->hasMany(Project::class, 'client_id'); } public function managingProjects() { return $this->hasMany(Project::class, 'manager_id'); } /** * @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; } public function confirmAccount($should_notify = false) { $this->confirmation_code = null; $this->is_confirmed = true; $this->save(); if ($should_notify) { $this->notify(new AccountConfirmed()); } } public function canUseAccount() { return $this->hasCompletedProfile() && $this->is_enabled; } public function hasCompletedProfile() { return $this->roles() ->count() > 0 || ($this->companyProfile); } public function getFullName() { if ($this->companyProfile) { return $this->companyProfile->name; } return $this->name . ' ' . $this->surname; } /** * Send the password reset notification. * * @param string $token * @return void */ public function sendPasswordResetNotification($token) { $this->notify(new ResetPasswordNotification($token)); } public function generateResetPasswordToken() { $token = str_random(52); DB::table('password_resets') ->where('email', $this->email) ->delete(); DB::table('password_resets') ->insert([ 'email' => $this->email, 'token' => Hash::make($token), 'created_at' => Carbon::now(), ]); return $token; } public function getUnreadCount() { return ProjectNote::query() ->join('reciptions', function ($join) { $join->on('project_notes.project_id', '=', 'reciptions.project_id'); $join->on('project_notes.created_at', '>', 'reciptions.last_read_at'); $join->where('reciptions.user_id', $this->id); }) ->count(); } public function isAdmin() { return $this->roles() ->where('name', Role::ADMIN) ->exists(); } }