Changeset c433da6 for app/Models
- Timestamp:
- 02/12/21 00:20:26 (4 years ago)
- Branches:
- master
- Children:
- 1f059b0
- Parents:
- 0924b6c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
app/Models/User.php
r0924b6c rc433da6 9 9 class User extends Authenticatable 10 10 { 11 11 use Notifiable; 12 12 13 13 protected $table = "users"; 14 14 15 16 17 18 19 20 21 22 23 24 25 15 /** 16 * The attributes that are mass assignable. 17 * 18 * @var array 19 */ 20 protected $fillable = [ 21 "name", 22 "surname", 23 "username", 24 "password", 25 "email", 26 26 "country_code", 27 27 "mobile_number", 28 29 28 "role_id" 29 ]; 30 30 31 32 33 34 35 36 37 38 39 40 31 /** 32 * The attributes that should be hidden for arrays. 33 * 34 * @var array 35 */ 36 protected $hidden = [ 37 "password", 38 "remember_token", 39 "is_active" 40 ]; 41 41 42 43 44 45 46 47 42 /** 43 * The attributes that should be cast to native types. 44 * 45 * @var array 46 */ 47 protected $casts = []; 48 48 49 49 50 public function role() { 51 return $this->belongsTo(Role::class); 52 } 50 public function role() 51 { 52 return $this->belongsTo(Role::class); 53 } 53 54 54 public function userProfile() { 55 public function userProfile() 56 { 55 57 return $this->hasOne(UserProfile::class); 56 58 } 57 59 58 public function post() { 59 return $this->hasMany(Post::class); 60 } 60 public function post() 61 { 62 return $this->hasMany(Post::class); 63 } 61 64 62 63 64 65 65 public function comments() 66 { 67 return $this->hasManyThrough(Comment::class, Post::class); 68 } 66 69 67 public function hasPermission($permission, $id = null, $any = false) { 70 public function hasPermission($permission, $id = null, $any = false) 71 { 68 72 69 70 73 $userPermissions = null; 74 $flag = null; 71 75 72 if($id != null) $userPermissions = User::find($id)->role->permission->pluck("name");73 76 if ($id != null) $userPermissions = User::find($id)->role->permission->pluck("name"); 77 else $userPermissions = $this->role->permission->pluck("name"); 74 78 75 if($any) {76 foreach($permission as $p) {77 if($this->hasPermission($p)) {78 79 80 81 82 79 if ($any) { 80 foreach ($permission as $p) { 81 if ($this->hasPermission($p)) { 82 return true; 83 } 84 } 85 return false; 86 } 83 87 84 if(is_string($permission)) {85 86 88 if (is_string($permission)) { 89 return $userPermissions->contains($permission); 90 } 87 91 88 if(is_array($permission)) { 89 foreach($permission as $p) { 90 if($this->hasPermission($p)) { 91 $flag = true; 92 } else { 93 $flag = false; break; 94 } 95 } 96 } 92 if (is_array($permission)) { 93 foreach ($permission as $p) { 94 if ($this->hasPermission($p)) { 95 $flag = true; 96 } else { 97 $flag = false; 98 break; 99 } 100 } 101 } 97 102 98 99 103 return $flag; 104 } 100 105 101 public function hasAllPermissions(array $permissions = array(), $id = null, $any = false) { 102 return $this->hasPermission($permissions, $id, $any); 103 } 106 public function hasAllPermissions(array $permissions = array(), $id = null, $any = false) 107 { 108 return $this->hasPermission($permissions, $id, $any); 109 } 104 110 105 public function hasAnyPermission(array $permissions = array(), $id = null, $any = true) { 106 return $this->hasPermission($permissions, $id, $any); 107 } 111 public function hasAnyPermission(array $permissions = array(), $id = null, $any = true) 112 { 113 return $this->hasPermission($permissions, $id, $any); 114 } 108 115 109 public function hasRole($role) { 110 return $role === $this->role->name; 111 } 116 public function hasRole($role) 117 { 118 return $role === $this->role->name; 119 } 112 120 113 public function isAdmin() { 114 return $this->hasRole("admin"); 115 } 121 public function isAdmin() 122 { 123 return $this->hasRole("admin"); 124 } 116 125 117 public function isAdminOrEditor() { 118 return $this->hasRole("admin") || $this->hasRole("editor"); 119 } 126 public function isAdminOrEditor() 127 { 128 return $this->hasRole("admin") || $this->hasRole("editor"); 129 } 120 130 121 public function getFullName($id = null) { 131 public function getFullName($id = null) 132 { 122 133 123 if($id != null) {124 125 126 134 if ($id != null) { 135 $user = User::find($id); 136 return $user->name . " " . $user->surname; 137 } 127 138 128 129 139 return $this->name . " " . $this->surname; 140 } 130 141 131 public function getPostsCount($id) { 132 return Post::where("user_id", $id)->count(); 133 } 142 public function getPostsCount($id) 143 { 144 return Post::where("user_id", $id)->count(); 145 } 134 146 135 public function generateTemporaryPassword($length = 20) { 136 return bcrypt(Str::random($length)); 137 } 147 public function generateTemporaryPassword($length = 20) 148 { 149 return bcrypt(Str::random($length)); 150 } 138 151 139 public function generateSecurityCode($min = 10000, $max = 99999) { 140 return rand($min, $max); 141 } 152 public function generateSecurityCode($min = 10000, $max = 99999) 153 { 154 return rand($min, $max); 155 } 142 156 143 public function generateVerifyToken($length = 32) { 144 return Str::random($length); 145 } 157 public function generateVerifyToken($length = 32) 158 { 159 return Str::random($length); 160 } 146 161 147 public function generateProfileLink($name, $surname) { 162 public function generateProfileLink($name, $surname) 163 { 148 164 149 165 $profileLinks = UserProfile::pluck("profile_link")->toArray(); … … 151 167 152 168 foreach ($profileLinks as $profileLink) { 153 if ($profileLink == $link) {169 if ($profileLink == $link) { 154 170 $link .= "-" . strtolower(Str::random(10)); 155 171 break; … … 160 176 } 161 177 162 public function generateTechnoblogEmail($name, $surname) { 178 public function generateTechnoblogEmail($name, $surname) 179 { 163 180 164 181 $emails = UserProfile::pluck("technoblog_email")->toArray(); … … 166 183 167 184 foreach ($emails as $email) { 168 if ($email == $newEmail) {185 if ($email == $newEmail) { 169 186 $position = strlen($name) + strlen($surname) + 1; 170 187 $id = UserProfile::latest("id")->first()->id + 1;
Note:
See TracChangeset
for help on using the changeset viewer.