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 | "mobile_number",
|
---|
27 | "avatar",
|
---|
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 hasPermission($permission, $id = null, $any = false) {
|
---|
55 |
|
---|
56 | $userPermissions = null;
|
---|
57 | $flag = null;
|
---|
58 |
|
---|
59 | if($id != null) $userPermissions = User::find($id)->role->permission->pluck("name");
|
---|
60 | else $userPermissions = $this->role->permission->pluck("name");
|
---|
61 |
|
---|
62 | if($any) {
|
---|
63 | foreach($permission as $p) {
|
---|
64 | if($this->hasPermission($p)) {
|
---|
65 | return true;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 |
|
---|
71 | if(is_string($permission)) {
|
---|
72 | return $userPermissions->contains($permission);
|
---|
73 | }
|
---|
74 |
|
---|
75 | if(is_array($permission)) {
|
---|
76 | foreach($permission as $p) {
|
---|
77 | if($this->hasPermission($p)) {
|
---|
78 | $flag = true;
|
---|
79 | } else {
|
---|
80 | $flag = false; break;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | return $flag;
|
---|
86 | }
|
---|
87 |
|
---|
88 | public function hasAllPermissions(array $permissions = array(), $id = null, $any = false) {
|
---|
89 | return $this->hasPermission($permissions, $id, $any);
|
---|
90 | }
|
---|
91 |
|
---|
92 | public function hasAnyPermission(array $permissions = array(), $id = null, $any = true) {
|
---|
93 | return $this->hasPermission($permissions, $id, $any);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public function hasRole($role) {
|
---|
97 | return $role === $this->role->name;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public function isAdmin() {
|
---|
101 | return $this->hasRole("Admin");
|
---|
102 | }
|
---|
103 |
|
---|
104 | public function isAdminOrEditor() {
|
---|
105 | return $this->hasRole("Admin") || $this->hasRole("Referent");
|
---|
106 | }
|
---|
107 |
|
---|
108 | public function getFullName($id = null) {
|
---|
109 |
|
---|
110 | if($id != null) {
|
---|
111 | $user = User::find($id);
|
---|
112 | return $user->name . " " . $user->surname;
|
---|
113 | }
|
---|
114 |
|
---|
115 | return $this->name . " " . $this->surname;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public function generateTemporaryPassword($length = 20) {
|
---|
119 | return bcrypt(Str::random($length));
|
---|
120 | }
|
---|
121 |
|
---|
122 | public function generateSecurityCode($min = 10000, $max = 99999) {
|
---|
123 | return rand($min, $max);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public function generateVerifyToken($length = 32) {
|
---|
127 | return Str::random($length);
|
---|
128 | }
|
---|
129 | }
|
---|