source: app/Models/User.php@ 194a359

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

added departments, edited users and added user settings

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