datatable.directory codebase
https://datatable.directory/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.3 KiB
129 lines
3.3 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use AdamWathan\EloquentOAuth\OAuthIdentity;
|
|
use App\Models\Concerns\Reportable;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
/**
|
|
* A user in the application
|
|
*
|
|
* @property Carbon $created_at
|
|
* @property Carbon $updated_at
|
|
* @property string $name - unique, for vanity URL
|
|
* @property string $email - unique, for login and social auth chaining
|
|
* @property string $password - hashed pw
|
|
* @property Proposal[]|Collection $proposals
|
|
* @property Table[]|Collection $tables
|
|
* @property OAuthIdentity[]|Collection $socialIdentities
|
|
* @property TableComment[]|Collection $tableComments
|
|
* @property Table[]|Collection $favouriteTables
|
|
* @property Table[]|Collection $followedDiscussions
|
|
* @property ContentReport[]|Collection $authoredReports
|
|
* @property Notification[]|Collection $notifications
|
|
* @property Notification[]|Collection $readNotifications
|
|
* @property Notification[]|Collection $unreadNotifications
|
|
*/
|
|
class User extends Authenticatable
|
|
{
|
|
use Reportable;
|
|
use Notifiable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'name', 'email', 'password',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for arrays.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $hidden = [
|
|
'password', 'remember_token',
|
|
];
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::deleting(function(User $self) {
|
|
// manually delete proposals to ensure row refcounts are updated
|
|
foreach ($self->proposals as $proposal) {
|
|
$proposal->delete();
|
|
}
|
|
});
|
|
}
|
|
|
|
/** Owned tables */
|
|
public function tables()
|
|
{
|
|
return $this->hasMany(Table::class, 'owner_id');
|
|
}
|
|
|
|
/** Assigned OAuth identities */
|
|
public function socialIdentities()
|
|
{
|
|
return $this->hasMany(OAuthIdentity::class);
|
|
}
|
|
|
|
/** Authored proposals */
|
|
public function proposals()
|
|
{
|
|
return $this->hasMany(Proposal::class, 'author_id');
|
|
}
|
|
|
|
/** Authored comments */
|
|
public function tableComments()
|
|
{
|
|
return $this->hasMany(TableComment::class, 'author_id');
|
|
}
|
|
|
|
/** User's favourite tables (personal collection) */
|
|
public function favouriteTables()
|
|
{
|
|
return $this->belongsToMany(Table::class, 'table_favourites');
|
|
}
|
|
|
|
/** Tables whose discussions user follows (this is similar to favourites) */
|
|
public function followedDiscussions()
|
|
{
|
|
return $this->belongsToMany(Table::class, 'discussion_follows');
|
|
}
|
|
|
|
/** Reports sent by this user */
|
|
public function authoredReports()
|
|
{
|
|
return $this->hasMany(ContentReport::class, 'author_id');
|
|
}
|
|
|
|
// --------- Relation Helpers ---------
|
|
|
|
public function addFavourite(Table $table)
|
|
{
|
|
$this->favouriteTables()->attach($table);
|
|
}
|
|
|
|
public function removeFavourite(Table $table)
|
|
{
|
|
$this->favouriteTables()->detach($table);
|
|
}
|
|
|
|
public function followDiscussion(Table $table)
|
|
{
|
|
$this->followedDiscussions()->attach($table);
|
|
}
|
|
|
|
public function unfollowDiscussion(Table $table)
|
|
{
|
|
$this->followedDiscussions()->detach($table);
|
|
}
|
|
}
|
|
|