source: app/Models/User.php@ bd9e8e3

develop
Last change on this file since bd9e8e3 was d795fa6, checked in by Berat Kjufliju <kufliju@…>, 3 years ago

added validation to blades

  • Property mode set to 100644
File size: 3.1 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 "password",
27 "email",
28 "phone_number",
29 "avatar",
30 "role_id"
31 ];
32
33 /**
34 * The attributes that should be hidden for arrays.
35 *
36 * @var array
37 */
38 protected $hidden = [
39 "password",
40 "remember_token",
41 "is_active"
42 ];
43
44 /**
45 * The attributes that should be cast to native types.
46 *
47 * @var array
48 */
49 protected $casts = [];
50
51
52 public function role() {
53 return $this->belongsTo(Role::class);
54 }
55
56 public function hasPermission($permission, $id = null, $any = false) {
57
58 $userPermissions = null;
59 $flag = null;
60
61 if($id != null) $userPermissions = User::find($id)->role->permission->pluck("name");
62 else $userPermissions = $this->role->permission->pluck("name");
63
64 if($any) {
65 foreach($permission as $p) {
66 if($this->hasPermission($p)) {
67 return true;
68 }
69 }
70 return false;
71 }
72
73 if(is_string($permission)) {
74 return $userPermissions->contains($permission);
75 }
76
77 if(is_array($permission)) {
78 foreach($permission as $p) {
79 if($this->hasPermission($p)) {
80 $flag = true;
81 } else {
82 $flag = false; break;
83 }
84 }
85 }
86
87 return $flag;
88 }
89
90 public function hasAllPermissions(array $permissions = array(), $id = null, $any = false) {
91 return $this->hasPermission($permissions, $id, $any);
92 }
93
94 public function hasAnyPermission(array $permissions = array(), $id = null, $any = true) {
95 return $this->hasPermission($permissions, $id, $any);
96 }
97
98 public function hasRole($role) {
99 return $role === $this->role->name;
100 }
101
102 public function isAdmin() {
103 return $this->hasRole("Admin");
104 }
105
106 public function isAdminOrEditor() {
107 return $this->hasRole("Admin") || $this->hasRole("Referent");
108 }
109
110 public function getFullName($id = null) {
111
112 if($id != null) {
113 $user = User::find($id);
114 return $user->name . " " . $user->surname;
115 }
116
117 return $this->name . " " . $this->surname;
118 }
119
120 public function generateTemporaryPassword($length = 20) {
121 return bcrypt(Str::random($length));
122 }
123
124 public function generateSecurityCode($min = 10000, $max = 99999) {
125 return rand($min, $max);
126 }
127
128 public function generateVerifyToken($length = 32) {
129 return Str::random($length);
130 }
131
132 public function getCreatedByName()
133 {
134 return User::where('id', $this->created_by)->pluck('username')->first();
135 }
136}
Note: See TracBrowser for help on using the repository browser.