source: app/Models/User.php@ 0a1fb54

Last change on this file since 0a1fb54 was 0a1fb54, checked in by beratkjufliju <kufliju@…>, 3 years ago

bug fixes

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[2fc88ec]1<?php
2
3namespace App\Models;
4
[120759b]5use Illuminate\Database\Eloquent\Factories\HasFactory;
[7304c7f]6use Illuminate\Support\Str;
[2fc88ec]7use Illuminate\Notifications\Notifiable;
[7304c7f]8use Illuminate\Foundation\Auth\User as Authenticatable;
[2fc88ec]9
10class User extends Authenticatable
11{
[7304c7f]12 use Notifiable;
[120759b]13 use HasFactory;
[7304c7f]14
15 protected $table = "users";
[2fc88ec]16
17 /**
18 * The attributes that are mass assignable.
19 *
[7304c7f]20 * @var array
[2fc88ec]21 */
22 protected $fillable = [
[7304c7f]23 "name",
24 "surname",
25 "username",
[d795fa6]26 "phone_number",
[c6b84df]27 "email",
28 "password",
[24a616f]29 "avatar",
[1c25bcf]30 "role_id",
31 "security_code",
32 "verify_token"
[2fc88ec]33 ];
34
35 /**
[7304c7f]36 * The attributes that should be hidden for arrays.
[2fc88ec]37 *
38 * @var array
39 */
40 protected $hidden = [
[7304c7f]41 "password",
42 "remember_token",
[1c25bcf]43 "is_active",
44 "verify_token",
45 "security_code"
[2fc88ec]46 ];
47
48 /**
[7304c7f]49 * The attributes that should be cast to native types.
[2fc88ec]50 *
51 * @var array
52 */
[7304c7f]53 protected $casts = [];
54
55
56 public function role() {
57 return $this->belongsTo(Role::class);
58 }
59
60 public function hasPermission($permission, $id = null, $any = false) {
61
62 $userPermissions = null;
63 $flag = null;
64
65 if($id != null) $userPermissions = User::find($id)->role->permission->pluck("name");
66 else $userPermissions = $this->role->permission->pluck("name");
67
68 if($any) {
69 foreach($permission as $p) {
70 if($this->hasPermission($p)) {
71 return true;
72 }
73 }
74 return false;
75 }
76
77 if(is_string($permission)) {
78 return $userPermissions->contains($permission);
79 }
80
81 if(is_array($permission)) {
82 foreach($permission as $p) {
83 if($this->hasPermission($p)) {
84 $flag = true;
85 } else {
86 $flag = false; break;
87 }
88 }
89 }
90
91 return $flag;
92 }
93
94 public function hasAllPermissions(array $permissions = array(), $id = null, $any = false) {
95 return $this->hasPermission($permissions, $id, $any);
96 }
97
98 public function hasAnyPermission(array $permissions = array(), $id = null, $any = true) {
99 return $this->hasPermission($permissions, $id, $any);
100 }
101
102 public function hasRole($role) {
103 return $role === $this->role->name;
104 }
105
106 public function isAdmin() {
[194a359]107 return $this->hasRole("Admin");
[7304c7f]108 }
109
110 public function isAdminOrEditor() {
[194a359]111 return $this->hasRole("Admin") || $this->hasRole("Referent");
[7304c7f]112 }
113
114 public function getFullName($id = null) {
115
116 if($id != null) {
117 $user = User::find($id);
118 return $user->name . " " . $user->surname;
119 }
120
121 return $this->name . " " . $this->surname;
122 }
123
124 public function generateTemporaryPassword($length = 20) {
125 return bcrypt(Str::random($length));
126 }
127
128 public function generateSecurityCode($min = 10000, $max = 99999) {
129 return rand($min, $max);
130 }
131
132 public function generateVerifyToken($length = 32) {
133 return Str::random($length);
134 }
[d795fa6]135
[0a1fb54]136 public function getCreatedByUsername()
[d795fa6]137 {
138 return User::where('id', $this->created_by)->pluck('username')->first();
139 }
[0df7a93]140
[0a1fb54]141 public function getCreatedByName()
142 {
143 $name = User::where('id', $this->created_by)->pluck('name')->first();
144 $surname = User::where('id', $this->created_by)->pluck('surname')->first();
145
146 return $name . " " . $surname;
147 }
148
[0df7a93]149 public function folder() {
150 return $this->hasMany(Folder::class);
151 }
[0a1fb54]152
153 public function department() {
154 return $this->hasMany(Department::class);
155 }
[2fc88ec]156}
Note: See TracBrowser for help on using the repository browser.