* Implemented webhooks

* oAuth wip

* Implement the whole auth flow

* Implement file upload limit depending on appsumo license
This commit is contained in:
Julien Nahum
2023-11-01 16:58:10 +01:00
committed by GitHub
parent 2e52518aa7
commit e9174238e4
19 changed files with 611 additions and 102 deletions

45
app/Models/License.php Normal file
View File

@@ -0,0 +1,45 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class License extends Model
{
const STATUS_ACTIVE = 'active';
const STATUS_INACTIVE = 'inactive';
use HasFactory;
protected $fillable = [
'license_key',
'user_id',
'license_provider',
'status',
'meta'
];
protected $casts = [
'meta' => 'array',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function scopeActive($query)
{
return $query->where('status', self::STATUS_ACTIVE);
}
public function getMaxFileSizeAttribute()
{
return [
1 => 25000000, // 25 MB,
2 => 50000000, // 50 MB,
3 => 75000000, // 75 MB,
][$this->meta['tier']];
}
}

View File

@@ -13,6 +13,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Cashier\Billable;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject
{
use Notifiable, HasFactory, Billable;
@@ -80,7 +81,9 @@ class User extends Authenticatable implements JWTSubject
public function getIsSubscribedAttribute()
{
return $this->subscribed() || in_array($this->email, config('opnform.extra_pro_users_emails'));
return $this->subscribed()
|| in_array($this->email, config('opnform.extra_pro_users_emails'))
|| !is_null($this->activeLicense());
}
public function getHasCustomerIdAttribute()
@@ -138,7 +141,7 @@ class User extends Authenticatable implements JWTSubject
public function forms()
{
return $this->hasMany(Form::class,'creator_id');
return $this->hasMany(Form::class, 'creator_id');
}
public function formTemplates()
@@ -146,6 +149,16 @@ class User extends Authenticatable implements JWTSubject
return $this->hasMany(Template::class, 'creator_id');
}
public function licenses()
{
return $this->hasMany(License::class);
}
public function activeLicense(): License
{
return $this->licenses()->active()->first();
}
/**
* =================================
* Oauth Related
@@ -187,26 +200,26 @@ class User extends Authenticatable implements JWTSubject
})->first()?->onTrial();
}
public static function boot ()
public static function boot()
{
parent::boot();
static::deleting(function(User $user) {
static::deleting(function (User $user) {
// 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();
}
}
});
});
}
public function scopeWithActiveSubscription($query)
{
return $query->whereHas('subscriptions', function($query) {
$query->where(function($q){
$q->where('stripe_status', 'trialing')
->orWhere('stripe_status', 'active');
});
return $query->whereHas('subscriptions', function ($query) {
$query->where(function ($q) {
$q->where('stripe_status', 'trialing')
->orWhere('stripe_status', 'active');
});
});
}

View File

@@ -2,8 +2,8 @@
namespace App\Models;
use App\Http\Requests\AnswerFormRequest;
use App\Models\Forms\Form;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -11,6 +11,9 @@ class Workspace extends Model
{
use HasFactory;
const MAX_FILE_SIZE_FREE = 5000000; // 5 MB
const MAX_FILE_SIZE_PRO = 50000000; // 50 MB
protected $fillable = [
'name',
'icon',
@@ -37,6 +40,26 @@ class Workspace extends Model
return false;
}
public function getMaxFileSizeAttribute()
{
if(is_null(config('cashier.key'))){
return self::MAX_FILE_SIZE_PRO;
}
// Return max file size depending on subscription
foreach ($this->owners as $owner) {
if ($owner->is_subscribed) {
if ($license = $owner->activeLicense()) {
// In case of special License
return $license->max_file_size;
}
}
return self::MAX_FILE_SIZE_PRO;
}
return self::MAX_FILE_SIZE_FREE;
}
public function getIsEnterpriseAttribute()
{
if(is_null(config('cashier.key'))){