source: app/Models/User.php@ c433da6

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

modified logo

  • Property mode set to 100644
File size: 4.4 KB
Line 
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{
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",
26 "country_code",
27 "mobile_number",
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 {
57 return $this->hasOne(UserProfile::class);
58 }
59
60 public function post()
61 {
62 return $this->hasMany(Post::class);
63 }
64
65 public function comments()
66 {
67 return $this->hasManyThrough(Comment::class, Post::class);
68 }
69
70 public function hasPermission($permission, $id = null, $any = false)
71 {
72
73 $userPermissions = null;
74 $flag = null;
75
76 if ($id != null) $userPermissions = User::find($id)->role->permission->pluck("name");
77 else $userPermissions = $this->role->permission->pluck("name");
78
79 if ($any) {
80 foreach ($permission as $p) {
81 if ($this->hasPermission($p)) {
82 return true;
83 }
84 }
85 return false;
86 }
87
88 if (is_string($permission)) {
89 return $userPermissions->contains($permission);
90 }
91
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 }
102
103 return $flag;
104 }
105
106 public function hasAllPermissions(array $permissions = array(), $id = null, $any = false)
107 {
108 return $this->hasPermission($permissions, $id, $any);
109 }
110
111 public function hasAnyPermission(array $permissions = array(), $id = null, $any = true)
112 {
113 return $this->hasPermission($permissions, $id, $any);
114 }
115
116 public function hasRole($role)
117 {
118 return $role === $this->role->name;
119 }
120
121 public function isAdmin()
122 {
123 return $this->hasRole("admin");
124 }
125
126 public function isAdminOrEditor()
127 {
128 return $this->hasRole("admin") || $this->hasRole("editor");
129 }
130
131 public function getFullName($id = null)
132 {
133
134 if ($id != null) {
135 $user = User::find($id);
136 return $user->name . " " . $user->surname;
137 }
138
139 return $this->name . " " . $this->surname;
140 }
141
142 public function getPostsCount($id)
143 {
144 return Post::where("user_id", $id)->count();
145 }
146
147 public function generateTemporaryPassword($length = 20)
148 {
149 return bcrypt(Str::random($length));
150 }
151
152 public function generateSecurityCode($min = 10000, $max = 99999)
153 {
154 return rand($min, $max);
155 }
156
157 public function generateVerifyToken($length = 32)
158 {
159 return Str::random($length);
160 }
161
162 public function generateProfileLink($name, $surname)
163 {
164
165 $profileLinks = UserProfile::pluck("profile_link")->toArray();
166 $link = strtolower($name) . strtolower($surname);
167
168 foreach ($profileLinks as $profileLink) {
169 if ($profileLink == $link) {
170 $link .= "-" . strtolower(Str::random(10));
171 break;
172 }
173 }
174
175 return $link;
176 }
177
178 public function generateTechnoblogEmail($name, $surname)
179 {
180
181 $emails = UserProfile::pluck("technoblog_email")->toArray();
182 $newEmail = strtolower($name) . "." . strtolower($surname) . "@technoblog.com";
183
184 foreach ($emails as $email) {
185 if ($email == $newEmail) {
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.