source: app/Models/User.php@ 1f059b0

Last change on this file since 1f059b0 was c433da6, checked in by Özkan İliyaz <iliyaz_96@…>, 4 years ago

modified logo

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[0924b6c]1<?php
2
3namespace App\Models;
4
5use Illuminate\Support\Str;
6use Illuminate\Notifications\Notifiable;
7use Illuminate\Foundation\Auth\User as Authenticatable;
8
9class User extends Authenticatable
10{
[c433da6]11 use Notifiable;
12
13 protected $table = "users";
14
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",
[0924b6c]26 "country_code",
27 "mobile_number",
[c433da6]28 "role_id"
29 ];
30
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
42 /**
43 * The attributes that should be cast to native types.
44 *
45 * @var array
46 */
47 protected $casts = [];
48
49
50 public function role()
51 {
52 return $this->belongsTo(Role::class);
53 }
54
55 public function userProfile()
56 {
[0924b6c]57 return $this->hasOne(UserProfile::class);
58 }
59
[c433da6]60 public function post()
61 {
62 return $this->hasMany(Post::class);
63 }
[0924b6c]64
[c433da6]65 public function comments()
66 {
67 return $this->hasManyThrough(Comment::class, Post::class);
68 }
[0924b6c]69
[c433da6]70 public function hasPermission($permission, $id = null, $any = false)
71 {
[0924b6c]72
[c433da6]73 $userPermissions = null;
74 $flag = null;
[0924b6c]75
[c433da6]76 if ($id != null) $userPermissions = User::find($id)->role->permission->pluck("name");
77 else $userPermissions = $this->role->permission->pluck("name");
[0924b6c]78
[c433da6]79 if ($any) {
80 foreach ($permission as $p) {
81 if ($this->hasPermission($p)) {
82 return true;
83 }
84 }
85 return false;
86 }
[0924b6c]87
[c433da6]88 if (is_string($permission)) {
89 return $userPermissions->contains($permission);
90 }
[0924b6c]91
[c433da6]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 }
[0924b6c]102
[c433da6]103 return $flag;
104 }
[0924b6c]105
[c433da6]106 public function hasAllPermissions(array $permissions = array(), $id = null, $any = false)
107 {
108 return $this->hasPermission($permissions, $id, $any);
109 }
[0924b6c]110
[c433da6]111 public function hasAnyPermission(array $permissions = array(), $id = null, $any = true)
112 {
113 return $this->hasPermission($permissions, $id, $any);
114 }
[0924b6c]115
[c433da6]116 public function hasRole($role)
117 {
118 return $role === $this->role->name;
119 }
[0924b6c]120
[c433da6]121 public function isAdmin()
122 {
123 return $this->hasRole("admin");
124 }
[0924b6c]125
[c433da6]126 public function isAdminOrEditor()
127 {
128 return $this->hasRole("admin") || $this->hasRole("editor");
129 }
[0924b6c]130
[c433da6]131 public function getFullName($id = null)
132 {
[0924b6c]133
[c433da6]134 if ($id != null) {
135 $user = User::find($id);
136 return $user->name . " " . $user->surname;
137 }
[0924b6c]138
[c433da6]139 return $this->name . " " . $this->surname;
140 }
[0924b6c]141
[c433da6]142 public function getPostsCount($id)
143 {
144 return Post::where("user_id", $id)->count();
145 }
[0924b6c]146
[c433da6]147 public function generateTemporaryPassword($length = 20)
148 {
149 return bcrypt(Str::random($length));
150 }
[0924b6c]151
[c433da6]152 public function generateSecurityCode($min = 10000, $max = 99999)
153 {
154 return rand($min, $max);
155 }
[0924b6c]156
[c433da6]157 public function generateVerifyToken($length = 32)
158 {
159 return Str::random($length);
160 }
[0924b6c]161
[c433da6]162 public function generateProfileLink($name, $surname)
163 {
[0924b6c]164
165 $profileLinks = UserProfile::pluck("profile_link")->toArray();
166 $link = strtolower($name) . strtolower($surname);
167
168 foreach ($profileLinks as $profileLink) {
[c433da6]169 if ($profileLink == $link) {
[0924b6c]170 $link .= "-" . strtolower(Str::random(10));
171 break;
172 }
173 }
174
175 return $link;
176 }
177
[c433da6]178 public function generateTechnoblogEmail($name, $surname)
179 {
[0924b6c]180
181 $emails = UserProfile::pluck("technoblog_email")->toArray();
182 $newEmail = strtolower($name) . "." . strtolower($surname) . "@technoblog.com";
183
184 foreach ($emails as $email) {
[c433da6]185 if ($email == $newEmail) {
[0924b6c]186 $position = strlen($name) + strlen($surname) + 1;
187 $id = UserProfile::latest("id")->first()->id + 1;
188 $newEmail = substr_replace($newEmail, "." . $id, $position, 0);
189 break;
190 }
191 }
192
193 return $newEmail;
194 }
195}
Note: See TracBrowser for help on using the repository browser.