[2fc88ec] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Models;
|
---|
| 4 |
|
---|
[7304c7f] | 5 | use Illuminate\Support\Str;
|
---|
[2fc88ec] | 6 | use Illuminate\Notifications\Notifiable;
|
---|
[7304c7f] | 7 | use Illuminate\Foundation\Auth\User as Authenticatable;
|
---|
[2fc88ec] | 8 |
|
---|
| 9 | class User extends Authenticatable
|
---|
| 10 | {
|
---|
[7304c7f] | 11 | use Notifiable;
|
---|
| 12 |
|
---|
| 13 | protected $table = "users";
|
---|
[2fc88ec] | 14 |
|
---|
| 15 | /**
|
---|
| 16 | * The attributes that are mass assignable.
|
---|
| 17 | *
|
---|
[7304c7f] | 18 | * @var array
|
---|
[2fc88ec] | 19 | */
|
---|
| 20 | protected $fillable = [
|
---|
[7304c7f] | 21 | "name",
|
---|
| 22 | "surname",
|
---|
| 23 | "username",
|
---|
| 24 | "password",
|
---|
| 25 | "email",
|
---|
| 26 | "mobile_number",
|
---|
| 27 | "role_id"
|
---|
[2fc88ec] | 28 | ];
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
[7304c7f] | 31 | * The attributes that should be hidden for arrays.
|
---|
[2fc88ec] | 32 | *
|
---|
| 33 | * @var array
|
---|
| 34 | */
|
---|
| 35 | protected $hidden = [
|
---|
[7304c7f] | 36 | "password",
|
---|
| 37 | "remember_token",
|
---|
| 38 | "is_active"
|
---|
[2fc88ec] | 39 | ];
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
[7304c7f] | 42 | * The attributes that should be cast to native types.
|
---|
[2fc88ec] | 43 | *
|
---|
| 44 | * @var array
|
---|
| 45 | */
|
---|
[7304c7f] | 46 | protected $casts = [];
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | public function role() {
|
---|
| 50 | return $this->belongsTo(Role::class);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public function hasPermission($permission, $id = null, $any = false) {
|
---|
| 54 |
|
---|
| 55 | $userPermissions = null;
|
---|
| 56 | $flag = null;
|
---|
| 57 |
|
---|
| 58 | if($id != null) $userPermissions = User::find($id)->role->permission->pluck("name");
|
---|
| 59 | else $userPermissions = $this->role->permission->pluck("name");
|
---|
| 60 |
|
---|
| 61 | if($any) {
|
---|
| 62 | foreach($permission as $p) {
|
---|
| 63 | if($this->hasPermission($p)) {
|
---|
| 64 | return true;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | return false;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | if(is_string($permission)) {
|
---|
| 71 | return $userPermissions->contains($permission);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | if(is_array($permission)) {
|
---|
| 75 | foreach($permission as $p) {
|
---|
| 76 | if($this->hasPermission($p)) {
|
---|
| 77 | $flag = true;
|
---|
| 78 | } else {
|
---|
| 79 | $flag = false; break;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | return $flag;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public function hasAllPermissions(array $permissions = array(), $id = null, $any = false) {
|
---|
| 88 | return $this->hasPermission($permissions, $id, $any);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public function hasAnyPermission(array $permissions = array(), $id = null, $any = true) {
|
---|
| 92 | return $this->hasPermission($permissions, $id, $any);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | public function hasRole($role) {
|
---|
| 96 | return $role === $this->role->name;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public function isAdmin() {
|
---|
[194a359] | 100 | return $this->hasRole("Admin");
|
---|
[7304c7f] | 101 | }
|
---|
| 102 |
|
---|
| 103 | public function isAdminOrEditor() {
|
---|
[194a359] | 104 | return $this->hasRole("Admin") || $this->hasRole("Referent");
|
---|
[7304c7f] | 105 | }
|
---|
| 106 |
|
---|
| 107 | public function getFullName($id = null) {
|
---|
| 108 |
|
---|
| 109 | if($id != null) {
|
---|
| 110 | $user = User::find($id);
|
---|
| 111 | return $user->name . " " . $user->surname;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | return $this->name . " " . $this->surname;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | public function generateTemporaryPassword($length = 20) {
|
---|
| 118 | return bcrypt(Str::random($length));
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | public function generateSecurityCode($min = 10000, $max = 99999) {
|
---|
| 122 | return rand($min, $max);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | public function generateVerifyToken($length = 32) {
|
---|
| 126 | return Str::random($length);
|
---|
| 127 | }
|
---|
[194a359] | 128 |
|
---|
[2fc88ec] | 129 | }
|
---|