Admin.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Webkul\User\Models;
  3. use Illuminate\Database\Eloquent\Factories\Factory;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Foundation\Auth\User as Authenticatable;
  6. use Illuminate\Notifications\Notifiable;
  7. use Illuminate\Support\Facades\Storage;
  8. use Laravel\Sanctum\HasApiTokens;
  9. use Webkul\Admin\Mail\Admin\ResetPasswordNotification;
  10. use Webkul\User\Contracts\Admin as AdminContract;
  11. use Webkul\User\Database\Factories\AdminFactory;
  12. class Admin extends Authenticatable implements AdminContract
  13. {
  14. use HasApiTokens, HasFactory, Notifiable;
  15. /**
  16. * The attributes that are mass assignable.
  17. *
  18. * @var array
  19. */
  20. protected $fillable = [
  21. 'name',
  22. 'email',
  23. 'password',
  24. 'image',
  25. 'api_token',
  26. 'role_id',
  27. 'status',
  28. ];
  29. /**
  30. * The attributes that should be hidden for arrays.
  31. *
  32. * @var array
  33. */
  34. protected $hidden = [
  35. 'password',
  36. 'api_token',
  37. 'remember_token',
  38. ];
  39. /**
  40. * Get image url for the product image.
  41. */
  42. public function image_url()
  43. {
  44. if (! $this->image) {
  45. return;
  46. }
  47. return Storage::url($this->image);
  48. }
  49. /**
  50. * Get image url for the product image.
  51. */
  52. public function getImageUrlAttribute()
  53. {
  54. return $this->image_url();
  55. }
  56. /**
  57. * @return array
  58. */
  59. public function toArray()
  60. {
  61. $array = parent::toArray();
  62. $array['image_url'] = $this->image_url;
  63. return $array;
  64. }
  65. /**
  66. * Get the role that owns the admin.
  67. *
  68. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  69. */
  70. public function role()
  71. {
  72. return $this->belongsTo(RoleProxy::modelClass());
  73. }
  74. /**
  75. * Checks if admin has permission to perform certain action.
  76. *
  77. * @param string $permission
  78. * @return bool
  79. */
  80. public function hasPermission($permission)
  81. {
  82. if (
  83. $this->role->permission_type == 'custom'
  84. && ! $this->role->permissions
  85. ) {
  86. return false;
  87. }
  88. return in_array($permission, $this->role->permissions);
  89. }
  90. /**
  91. * Send the password reset notification.
  92. *
  93. * @param string $token
  94. * @return void
  95. */
  96. public function sendPasswordResetNotification($token)
  97. {
  98. $this->notify(new ResetPasswordNotification($token));
  99. }
  100. /**
  101. * Create a new factory instance for the model.
  102. */
  103. protected static function newFactory(): Factory
  104. {
  105. return AdminFactory::new();
  106. }
  107. }