1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Models;
|
---|
4 |
|
---|
5 | use Illuminate\Support\Str;
|
---|
6 | use Illuminate\Notifications\Notifiable;
|
---|
7 | use Illuminate\Foundation\Auth\User as Authenticatable;
|
---|
8 |
|
---|
9 | class 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 | return $this->belongsTo(Role::class);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public function userProfile() {
|
---|
55 | return $this->hasOne(UserProfile::class);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public function post() {
|
---|
59 | return $this->hasMany(Post::class);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public function comments()
|
---|
63 | {
|
---|
64 | return $this->hasManyThrough(Comment::class, Post::class);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public function hasPermission($permission, $id = null, $any = false) {
|
---|
68 |
|
---|
69 | $userPermissions = null;
|
---|
70 | $flag = null;
|
---|
71 |
|
---|
72 | if($id != null) $userPermissions = User::find($id)->role->permission->pluck("name");
|
---|
73 | else $userPermissions = $this->role->permission->pluck("name");
|
---|
74 |
|
---|
75 | if($any) {
|
---|
76 | foreach($permission as $p) {
|
---|
77 | if($this->hasPermission($p)) {
|
---|
78 | return true;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | return false;
|
---|
82 | }
|
---|
83 |
|
---|
84 | if(is_string($permission)) {
|
---|
85 | return $userPermissions->contains($permission);
|
---|
86 | }
|
---|
87 |
|
---|
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 | }
|
---|
97 |
|
---|
98 | return $flag;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public function hasAllPermissions(array $permissions = array(), $id = null, $any = false) {
|
---|
102 | return $this->hasPermission($permissions, $id, $any);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public function hasAnyPermission(array $permissions = array(), $id = null, $any = true) {
|
---|
106 | return $this->hasPermission($permissions, $id, $any);
|
---|
107 | }
|
---|
108 |
|
---|
109 | public function hasRole($role) {
|
---|
110 | return $role === $this->role->name;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public function isAdmin() {
|
---|
114 | return $this->hasRole("admin");
|
---|
115 | }
|
---|
116 |
|
---|
117 | public function isAdminOrEditor() {
|
---|
118 | return $this->hasRole("admin") || $this->hasRole("editor");
|
---|
119 | }
|
---|
120 |
|
---|
121 | public function getFullName($id = null) {
|
---|
122 |
|
---|
123 | if($id != null) {
|
---|
124 | $user = User::find($id);
|
---|
125 | return $user->name . " " . $user->surname;
|
---|
126 | }
|
---|
127 |
|
---|
128 | return $this->name . " " . $this->surname;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public function getPostsCount($id) {
|
---|
132 | return Post::where("user_id", $id)->count();
|
---|
133 | }
|
---|
134 |
|
---|
135 | public function generateTemporaryPassword($length = 20) {
|
---|
136 | return bcrypt(Str::random($length));
|
---|
137 | }
|
---|
138 |
|
---|
139 | public function generateSecurityCode($min = 10000, $max = 99999) {
|
---|
140 | return rand($min, $max);
|
---|
141 | }
|
---|
142 |
|
---|
143 | public function generateVerifyToken($length = 32) {
|
---|
144 | return Str::random($length);
|
---|
145 | }
|
---|
146 |
|
---|
147 | public function generateProfileLink($name, $surname) {
|
---|
148 |
|
---|
149 | $profileLinks = UserProfile::pluck("profile_link")->toArray();
|
---|
150 | $link = strtolower($name) . strtolower($surname);
|
---|
151 |
|
---|
152 | foreach ($profileLinks as $profileLink) {
|
---|
153 | if($profileLink == $link) {
|
---|
154 | $link .= "-" . strtolower(Str::random(10));
|
---|
155 | break;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | return $link;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public function generateTechnoblogEmail($name, $surname) {
|
---|
163 |
|
---|
164 | $emails = UserProfile::pluck("technoblog_email")->toArray();
|
---|
165 | $newEmail = strtolower($name) . "." . strtolower($surname) . "@technoblog.com";
|
---|
166 |
|
---|
167 | foreach ($emails as $email) {
|
---|
168 | if($email == $newEmail) {
|
---|
169 | $position = strlen($name) + strlen($surname) + 1;
|
---|
170 | $id = UserProfile::latest("id")->first()->id + 1;
|
---|
171 | $newEmail = substr_replace($newEmail, "." . $id, $position, 0);
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | return $newEmail;
|
---|
177 | }
|
---|
178 | }
|
---|