source: app/Models/User.php@ c6fe0df

develop
Last change on this file since c6fe0df was c6b84df, checked in by beratkjufliju <kufliju@…>, 3 years ago

added fileTypes controller, notifications, excel export, edited views

  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[2fc88ec]1<?php
2
3namespace App\Models;
4
[120759b]5use Illuminate\Database\Eloquent\Factories\HasFactory;
[7304c7f]6use Illuminate\Support\Str;
[2fc88ec]7use Illuminate\Notifications\Notifiable;
[7304c7f]8use Illuminate\Foundation\Auth\User as Authenticatable;
[2fc88ec]9
10class User extends Authenticatable
11{
[7304c7f]12 use Notifiable;
[120759b]13 use HasFactory;
[7304c7f]14
15 protected $table = "users";
[2fc88ec]16
17 /**
18 * The attributes that are mass assignable.
19 *
[7304c7f]20 * @var array
[2fc88ec]21 */
22 protected $fillable = [
[7304c7f]23 "name",
24 "surname",
25 "username",
[d795fa6]26 "phone_number",
[c6b84df]27 "email",
28 "password",
[24a616f]29 "avatar",
[7304c7f]30 "role_id"
[2fc88ec]31 ];
32
33 /**
[7304c7f]34 * The attributes that should be hidden for arrays.
[2fc88ec]35 *
36 * @var array
37 */
38 protected $hidden = [
[7304c7f]39 "password",
40 "remember_token",
41 "is_active"
[2fc88ec]42 ];
43
44 /**
[7304c7f]45 * The attributes that should be cast to native types.
[2fc88ec]46 *
47 * @var array
48 */
[7304c7f]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() {
[194a359]103 return $this->hasRole("Admin");
[7304c7f]104 }
105
106 public function isAdminOrEditor() {
[194a359]107 return $this->hasRole("Admin") || $this->hasRole("Referent");
[7304c7f]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 }
[d795fa6]131
132 public function getCreatedByName()
133 {
134 return User::where('id', $this->created_by)->pluck('username')->first();
135 }
[2fc88ec]136}
Note: See TracBrowser for help on using the repository browser.