source: app/Models/User.php@ 7ed1069

Last change on this file since 7ed1069 was f457265, checked in by Berat Kjufliju <kufliju@…>, 4 years ago

ADD technoweek offer, companies

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