source: app/Models/User.php@ 3536fe9

Last change on this file since 3536fe9 was 1c25bcf, checked in by Berat Kjufliju <kufliju@…>, 3 years ago

added 2fa, bug fixes, edited blades

  • Property mode set to 100644
File size: 3.3 KB
Line 
1<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Factories\HasFactory;
6use Illuminate\Support\Str;
7use Illuminate\Notifications\Notifiable;
8use Illuminate\Foundation\Auth\User as Authenticatable;
9
10class User extends Authenticatable
11{
12 use Notifiable;
13 use HasFactory;
14
15 protected $table = "users";
16
17 /**
18 * The attributes that are mass assignable.
19 *
20 * @var array
21 */
22 protected $fillable = [
23 "name",
24 "surname",
25 "username",
26 "phone_number",
27 "email",
28 "password",
29 "avatar",
30 "role_id",
31 "security_code",
32 "verify_token"
33 ];
34
35 /**
36 * The attributes that should be hidden for arrays.
37 *
38 * @var array
39 */
40 protected $hidden = [
41 "password",
42 "remember_token",
43 "is_active",
44 "verify_token",
45 "security_code"
46 ];
47
48 /**
49 * The attributes that should be cast to native types.
50 *
51 * @var array
52 */
53 protected $casts = [];
54
55
56 public function role() {
57 return $this->belongsTo(Role::class);
58 }
59
60 public function hasPermission($permission, $id = null, $any = false) {
61
62 $userPermissions = null;
63 $flag = null;
64
65 if($id != null) $userPermissions = User::find($id)->role->permission->pluck("name");
66 else $userPermissions = $this->role->permission->pluck("name");
67
68 if($any) {
69 foreach($permission as $p) {
70 if($this->hasPermission($p)) {
71 return true;
72 }
73 }
74 return false;
75 }
76
77 if(is_string($permission)) {
78 return $userPermissions->contains($permission);
79 }
80
81 if(is_array($permission)) {
82 foreach($permission as $p) {
83 if($this->hasPermission($p)) {
84 $flag = true;
85 } else {
86 $flag = false; break;
87 }
88 }
89 }
90
91 return $flag;
92 }
93
94 public function hasAllPermissions(array $permissions = array(), $id = null, $any = false) {
95 return $this->hasPermission($permissions, $id, $any);
96 }
97
98 public function hasAnyPermission(array $permissions = array(), $id = null, $any = true) {
99 return $this->hasPermission($permissions, $id, $any);
100 }
101
102 public function hasRole($role) {
103 return $role === $this->role->name;
104 }
105
106 public function isAdmin() {
107 return $this->hasRole("Admin");
108 }
109
110 public function isAdminOrEditor() {
111 return $this->hasRole("Admin") || $this->hasRole("Referent");
112 }
113
114 public function getFullName($id = null) {
115
116 if($id != null) {
117 $user = User::find($id);
118 return $user->name . " " . $user->surname;
119 }
120
121 return $this->name . " " . $this->surname;
122 }
123
124 public function generateTemporaryPassword($length = 20) {
125 return bcrypt(Str::random($length));
126 }
127
128 public function generateSecurityCode($min = 10000, $max = 99999) {
129 return rand($min, $max);
130 }
131
132 public function generateVerifyToken($length = 32) {
133 return Str::random($length);
134 }
135
136 public function getCreatedByName()
137 {
138 return User::where('id', $this->created_by)->pluck('username')->first();
139 }
140
141 public function folder() {
142 return $this->hasMany(Folder::class);
143 }
144}
Note: See TracBrowser for help on using the repository browser.