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