| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Models; |
| 4 |
|
| 5 |
|
| 6 |
use FluentCart\App\Services\Permission\PermissionManager; |
| 7 |
|
| 8 |
class User extends Model |
| 9 |
{ |
| 10 |
protected $table = 'users'; |
| 11 |
|
| 12 |
/** |
| 13 |
* The primary key for the model. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
protected $primaryKey = 'ID'; |
| 18 |
|
| 19 |
protected $guarded = ['password']; |
| 20 |
|
| 21 |
/** |
| 22 |
* Credential columns of the WordPress `users` table that must never be |
| 23 |
* serialized into an API response. |
| 24 |
* |
| 25 |
* `$guarded` above is mass-assignment protection only (GuardsAttributes) and |
| 26 |
* has no effect on serialization — and it names `password`, which is not even |
| 27 |
* a real column. Serialization hiding lives here (HidesAttributes), and it is |
| 28 |
* applied by HasAttributes::getArrayableItems(), so it covers toArray(), |
| 29 |
* toJson() and every `with('wpUser')` eager load at once. |
| 30 |
* |
| 31 |
* This affects serialization ONLY. Direct property access ($user->user_pass) |
| 32 |
* still works, so internal reads are unaffected. |
| 33 |
* |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
protected $hidden = [ |
| 37 |
'user_pass', // bcrypt/phpass password hash |
| 38 |
'user_activation_key', // password-reset / new-user activation token |
| 39 |
]; |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Check if the user has a specific permission. |
| 44 |
* @param string|array $permission |
| 45 |
* @return bool |
| 46 |
*/ |
| 47 |
public function userCan($permission): bool |
| 48 |
{ |
| 49 |
return PermissionManager::hasPermission($permission, $this->ID); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Check if the user has a specific permission. |
| 54 |
* @param string|array $permission |
| 55 |
* @return bool |
| 56 |
*/ |
| 57 |
public function userCanAny($permission): bool |
| 58 |
{ |
| 59 |
return PermissionManager::hasAnyPermission($permission, $this->ID); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @todo: Move this to Pro Plugin's Controller |
| 64 |
*/ |
| 65 |
public function setStoreRole($role) |
| 66 |
{ |
| 67 |
$wpUser = get_user_by('ID', $this->ID); |
| 68 |
|
| 69 |
if (user_can($wpUser, 'manage_options')) { |
| 70 |
return new \WP_Error('super_admin', __('The user already have all the accesses as part of Administrator Role', 'fluent-cart')); |
| 71 |
} |
| 72 |
|
| 73 |
return update_user_meta($this->ID, '_fluent_cart_admin_role', $role); |
| 74 |
} |
| 75 |
|
| 76 |
public function customer(): \FluentCart\Framework\Database\Orm\Relations\HasOne |
| 77 |
{ |
| 78 |
return $this->hasOne(Customer::class, 'user_id'); |
| 79 |
} |
| 80 |
|
| 81 |
} |
| 82 |
|