opnform/app/Models/User.php

250 lines
5.9 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
namespace App\Models;
use App\Models\Forms\Form;
use App\Notifications\ResetPassword;
use App\Notifications\VerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Cashier\Billable;
use Tymon\JWTAuth\Contracts\JWTSubject;
2023-10-12 10:06:03 +00:00
class User extends Authenticatable implements JWTSubject
2022-09-20 19:59:52 +00:00
{
2024-02-23 10:54:12 +00:00
use Billable;
use HasFactory;
use Notifiable;
2022-09-20 19:59:52 +00:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
2024-02-23 10:54:12 +00:00
'hear_about_us',
2022-09-20 19:59:52 +00:00
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
2024-02-23 10:54:12 +00:00
'hear_about_us',
2022-09-20 19:59:52 +00:00
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'photo_url',
];
public function ownsForm(Form $form)
{
2024-02-23 10:54:12 +00:00
return $this->workspaces()->where('workspaces.id', $form->workspace_id)->exists();
2023-12-03 14:02:48 +00:00
}
public function ownsWorkspace(Workspace $workspace)
{
2024-02-23 10:54:12 +00:00
return $this->workspaces()->where('workspaces.id', $workspace->id)->exists();
}
2022-09-20 19:59:52 +00:00
/**
* Get the profile photo URL attribute.
*
* @return string
*/
public function getPhotoUrlAttribute()
{
return vsprintf('https://www.gravatar.com/avatar/%s.jpg?s=200&d=%s', [
md5(strtolower($this->email)),
$this->name ? urlencode("https://ui-avatars.com/api/$this->name") : 'mp',
]);
}
public function getHasFormsAttribute()
{
return $this->workspaces()->whereHas('forms')->exists();
}
public function getIsSubscribedAttribute()
{
return $this->subscribed()
|| in_array($this->email, config('opnform.extra_pro_users_emails'))
2024-02-23 10:54:12 +00:00
|| ! is_null($this->activeLicense());
2022-09-20 19:59:52 +00:00
}
public function getHasCustomerIdAttribute()
{
2024-02-23 10:54:12 +00:00
return ! is_null($this->stripe_id);
2022-09-20 19:59:52 +00:00
}
public function getAdminAttribute()
{
2023-10-12 10:06:03 +00:00
return in_array($this->email, config('opnform.admin_emails'));
2022-09-20 19:59:52 +00:00
}
2024-01-19 13:27:04 +00:00
public function getModeratorAttribute()
{
return in_array($this->email, config('opnform.moderator_emails')) || $this->admin;
}
2022-11-16 12:12:48 +00:00
public function getTemplateEditorAttribute()
{
2023-10-12 10:06:03 +00:00
return $this->admin || in_array($this->email, config('opnform.template_editor_emails'));
2022-11-16 12:12:48 +00:00
}
2022-09-20 19:59:52 +00:00
/**
* =================================
* Helper Related
* =================================
*/
/**
* Send the password reset notification.
*
2024-02-23 10:54:12 +00:00
* @param string $token
2022-09-20 19:59:52 +00:00
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPassword($token));
}
/**
* Send the email verification notification.
*
* @return void
*/
public function sendEmailVerificationNotification()
{
2024-02-23 10:54:12 +00:00
$this->notify(new VerifyEmail());
2022-09-20 19:59:52 +00:00
}
/**
* =================================
* Relationship
* =================================
*/
public function workspaces()
{
return $this->belongsToMany(Workspace::class);
}
public function forms()
{
return $this->hasMany(Form::class, 'creator_id');
2022-09-20 19:59:52 +00:00
}
public function formTemplates()
{
return $this->hasMany(Template::class, 'creator_id');
}
public function licenses()
{
return $this->hasMany(License::class);
}
2023-11-01 17:14:01 +00:00
public function activeLicense(): ?License
{
return $this->licenses()->active()->first();
}
2022-09-20 19:59:52 +00:00
/**
* =================================
* Oauth Related
* =================================
*/
/**
* Get the oauth providers.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function oauthProviders()
{
return $this->hasMany(OAuthProvider::class);
}
/**
* @return int
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* @return array
*/
public function getJWTCustomClaims()
{
2024-01-09 09:43:07 +00:00
return [
'ip' => \Hash::make(request()->ip()),
'ua' => \Hash::make(request()->userAgent()),
];
2022-09-20 19:59:52 +00:00
}
public function getIsRiskyAttribute()
{
return $this->created_at->isAfter(now()->subDays(3)) || // created in last 3 days
$this->subscriptions()->where(function ($q) {
$q->where('stripe_status', 'trialing')
->orWhere('stripe_status', 'active');
})->first()?->onTrial();
}
2023-12-03 14:02:48 +00:00
public function flushCache()
{
$this->workspaces()->with('forms')->get()->each(function (Workspace $workspace) {
$workspace->flush();
$workspace->forms->each(function (Form $form) {
$form->flush();
});
});
}
public static function boot()
2022-09-20 19:59:52 +00:00
{
parent::boot();
static::deleting(function (User $user) {
2022-09-20 19:59:52 +00:00
// Remove user's workspace if he's the only one with this workspace
foreach ($user->workspaces as $workspace) {
if ($workspace->users()->count() == 1) {
$workspace->delete();
}
}
});
2022-09-20 19:59:52 +00:00
}
public function scopeWithActiveSubscription($query)
{
return $query->whereHas('subscriptions', function ($query) {
$query->where(function ($q) {
$q->where('stripe_status', 'trialing')
->orWhere('stripe_status', 'active');
});
2022-09-20 19:59:52 +00:00
});
}
}