FleetCart/Modules/User/Entities/User.php

247 lines
5.9 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace Modules\User\Entities;
use Modules\Order\Entities\Order;
use Modules\User\Admin\UserTable;
use Modules\Review\Entities\Review;
use Illuminate\Auth\Authenticatable;
use Modules\Address\Entities\Address;
use Modules\Product\Entities\Product;
use Modules\User\Repositories\Permission;
use Cartalyst\Sentinel\Users\EloquentUser;
use Modules\Address\Entities\DefaultAddress;
use Cartalyst\Sentinel\Laravel\Facades\Activation;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class User extends EloquentUser implements AuthenticatableContract
{
use Authenticatable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email',
'phone',
'password',
'last_name',
'first_name',
'permissions',
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['last_login'];
public static function registered($email)
{
return static::where('email', $email)->exists();
}
public static function findByEmail($email)
{
return static::where('email', $email)->first();
}
public static function totalCustomers()
{
return Role::findOrNew(setting('customer_role'))->users()->count();
}
/**
* Login the user.
*
* @return $this|bool
*/
public function login()
{
return auth()->login($this);
}
/**
* Determine if the user is a customer.
*
* @return bool
*/
public function isCustomer()
{
if ($this->hasRoleName('admin')) {
return false;
}
return $this->hasRoleId(setting('customer_role'));
}
/**
* Checks if a user belongs to the given Role ID.
*
* @param int $roleId
* @return bool
*/
public function hasRoleId($roleId)
{
return $this->roles()->whereId($roleId)->count() !== 0;
}
/**
* Checks if a user belongs to the given Role Name.
*
* @param string $name
* @return bool
*/
public function hasRoleName($name)
{
return $this->roles()->whereTranslation('name', $name)->count() !== 0;
}
/**
* Check if the current user is activated.
*
* @return bool
*/
public function isActivated()
{
return Activation::completed($this);
}
/**
* Get the recent orders of the user.
*
* @param int $take
* @return \Illuminate\Database\Eloquent\Collection
*/
public function recentOrders($take)
{
return $this->orders()->latest()->take($take)->get();
}
/**
* Get the roles of the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'user_roles')->withTimestamps();
}
/**
* Get the orders of the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function orders()
{
return $this->hasMany(Order::class, 'customer_id');
}
/**
* Get the wishlist of the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function wishlist()
{
return $this->belongsToMany(Product::class, 'wish_lists')->withTimestamps();
}
/**
* Get the default address of the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function defaultAddress()
{
return $this->hasOne(DefaultAddress::class, 'customer_id')->withDefault();
}
/**
* Get the addresses of the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function addresses()
{
return $this->hasMany(Address::class, 'customer_id');
}
/**
* Get the reviews of the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function reviews()
{
return $this->hasMany(Review::class, 'reviewer_id');
}
/**
* Get the full name of the user.
*
* @return string
*/
public function getFullNameAttribute()
{
return "{$this->first_name} {$this->last_name}";
}
/**
* Set user's permissions.
*
* @param array $permissions
* @return void
*/
public function setPermissionsAttribute(array $permissions)
{
$this->attributes['permissions'] = Permission::prepare($permissions);
}
/**
* Determine if the user has access to the given permissions.
*
* @param array|string $permissions
* @return bool
*/
public function hasAccess($permissions)
{
$permissions = is_array($permissions) ? $permissions : func_get_args();
return $this->getPermissionsInstance()->hasAccess($permissions);
}
/**
* Determine if the user has access to the any given permissions
*
* @param array|string $permissions
* @return bool
*/
public function hasAnyAccess($permissions)
{
$permissions = is_array($permissions) ? $permissions : func_get_args();
return $this->getPermissionsInstance()->hasAnyAccess($permissions);
}
public function wishlistHas($productId)
{
return self::wishlist()->where('product_id', $productId)->exists();
}
/**
* Get table data for the resource
*
* @return \Illuminate\Http\JsonResponse
*/
public function table()
{
return new UserTable($this->newQuery());
}
}