<?php
namespace App\Models;
use Eloquent;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\HtmlString;
use Watson\Rememberable\Rememberable;
class User extends Authenticatable
{
use Rememberable;
protected $connection = 'forum';
protected $table = 'user';
protected $primaryKey = 'userid';
public $timestamps = false;
protected $fillable = [];
protected $hidden = [
'password', 'salt'
];
public function token()
{
return $this->hasOne(Token::class);
}
public function timezone()
{
return $this->hasOne(Timezone::class);
}
public function getTimezone()
{
return $this->timezone ? $this->timezone->timezone : config('app.timezone');
}
public function fields()
{
return $this->hasOne(UserField::class, 'userid');
}
public function getDisplayGroup()
{
$display = $this->displaygroupid;
if (!$display) {
$display = $this->usergroupid;
}
return Group::find($display);
}
public function isStaff()
{
$staff = false;
foreach ($this->usergroups->get() as $group) {
if ($group->isStaff()) {
$staff = true;
}
if ($group->isBanned()) {
return false;
}
}
return $staff;
}
public function isManagement()
{
foreach ($this->usergroups->get() as $group) {
if ($group->isManagement()) {
return true;
}
}
return false;
}
public function isAdmin()
{
foreach ($this->usergroups->get() as $group) {
if ($group->isAdmin()) {
return true;
}
}
return false;
}
public function is(...$groups)
{
foreach ($this->usergroups->get() as $usergroup) {
foreach ($groups as $group) {
if ($usergroup->is($group)) {
return true;
}
}
}
return false;
}
public function getUsergroupsAttribute()
{
if (!$this->relationLoaded('usergroups')) {
$usergroups = Group::whereIn('usergroupid', $this->membergroupids);
$this->setRelation('usergroups', $usergroups);
}
return $this->getRelation('usergroups');
}
public function usergroups()
{
return Group::whereIn('usergroupid', $this->membergroupids);
}
public function getMembergroupidsAttribute($commaSeparatedIds)
{
$groups = json_decode("[{$commaSeparatedIds}]", true);
array_unshift($groups, $this->usergroupid);
return $groups;
}
public function getDisplayName()
{
$group = $this->getDisplayGroup();
return new HtmlString($group->opentag . e($this->username) . $group->closetag);
}
}