¨4.0.1¨

This commit is contained in:
¨NW¨
2023-12-03 14:07:47 +00:00
parent c08b36d1b6
commit f35052522d
1112 changed files with 43019 additions and 24987 deletions

View File

@@ -4,6 +4,7 @@ namespace Modules\User\Entities;
use Modules\Order\Entities\Order;
use Modules\User\Admin\UserTable;
use Illuminate\Http\JsonResponse;
use Modules\Review\Entities\Review;
use Illuminate\Auth\Authenticatable;
use Modules\Address\Entities\Address;
@@ -11,7 +12,9 @@ use Modules\Product\Entities\Product;
use Modules\User\Repositories\Permission;
use Cartalyst\Sentinel\Users\EloquentUser;
use Modules\Address\Entities\DefaultAddress;
use Illuminate\Database\Eloquent\Collection;
use Cartalyst\Sentinel\Laravel\Facades\Activation;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
@@ -40,21 +43,25 @@ class User extends EloquentUser implements AuthenticatableContract
*/
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.
*
@@ -65,6 +72,7 @@ class User extends EloquentUser implements AuthenticatableContract
return auth()->login($this);
}
/**
* Determine if the user is a customer.
*
@@ -79,10 +87,36 @@ class User extends EloquentUser implements AuthenticatableContract
return $this->hasRoleId(setting('customer_role'));
}
/**
* 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;
}
/**
* Get the roles of the user.
*
* @return BelongsToMany
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'user_roles')->withTimestamps();
}
/**
* Checks if a user belongs to the given Role ID.
*
* @param int $roleId
*
* @return bool
*/
public function hasRoleId($roleId)
@@ -90,16 +124,6 @@ class User extends EloquentUser implements AuthenticatableContract
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.
@@ -111,77 +135,64 @@ class User extends EloquentUser implements AuthenticatableContract
return Activation::completed($this);
}
/**
* Get the recent orders of the user.
*
* @param int $take
* @return \Illuminate\Database\Eloquent\Collection
*
* @return 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
* @return 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
* @return HasMany
*/
public function defaultAddress()
{
return $this->hasOne(DefaultAddress::class, 'customer_id')->withDefault();
}
/**
* Get the addresses of the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* @return HasMany
*/
public function addresses()
{
return $this->hasMany(Address::class, 'customer_id');
}
/**
* Get the reviews of the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* @return HasMany
*/
public function reviews()
{
return $this->hasMany(Review::class, 'reviewer_id');
}
/**
* Get the full name of the user.
*
@@ -192,10 +203,12 @@ class User extends EloquentUser implements AuthenticatableContract
return "{$this->first_name} {$this->last_name}";
}
/**
* Set user's permissions.
*
* @param array $permissions
*
* @return void
*/
public function setPermissionsAttribute(array $permissions)
@@ -203,10 +216,12 @@ class User extends EloquentUser implements AuthenticatableContract
$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)
@@ -216,10 +231,12 @@ class User extends EloquentUser implements AuthenticatableContract
return $this->getPermissionsInstance()->hasAccess($permissions);
}
/**
* Determine if the user has access to the any given permissions
* Determine if the user has access to any given permissions
*
* @param array|string $permissions
*
* @return bool
*/
public function hasAnyAccess($permissions)
@@ -229,15 +246,28 @@ class User extends EloquentUser implements AuthenticatableContract
return $this->getPermissionsInstance()->hasAnyAccess($permissions);
}
public function wishlistHas($productId)
{
return self::wishlist()->where('product_id', $productId)->exists();
}
/**
* Get the wishlist of the user.
*
* @return BelongsToMany
*/
public function wishlist()
{
return $this->belongsToMany(Product::class, 'wish_lists')->withTimestamps();
}
/**
* Get table data for the resource
*
* @return \Illuminate\Http\JsonResponse
* @return JsonResponse
*/
public function table()
{