/
CrudModel.php
162 lines (154 loc) · 5.42 KB
/
CrudModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
use Kwaadpepper\CrudPolicies\Enums\CrudType;
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
class CrudModel extends Authenticatable
{
use HasFactory;
use IsCrudModel;
protected $crudLabelColumn = 'email';
protected $crudValueColumn = 'id';
protected $fillable = [
'registration',
'firstname',
'lastname',
'email',
'site_id'
];
public function __construct($attributes = [])
{
self::$editableProperties = [
'registration' => [
'label' => trans('Registration number'),
'type' => CrudType::string(),
'readonly' => true,
'validate' => [
'digits_between:7,8',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
$q = CrudModel::query();
if ($this->id) {
$q = $q->where('id', '!=', $this->id);
}
if ($q->where('registration', $value)->exists()) {
$fail(__(':attribute is already used', [
'attribute' => $attribute
]));
}
}
],
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'firstname' => [
'label' => trans('First name'),
'type' => CrudType::string(),
'validate' => [
'string',
'max:60'
],
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'lastname' => [
'label' => trans('Last name'),
'type' => CrudType::string(),
'nullable' => false,
'validate' => [
'string',
'max:60'
],
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'email' => [
'label' => trans('Email'),
'type' => CrudType::email(),
'nullable' => false,
'validate' => [
'max:150',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
$q = CrudModel::query();
if ($this->id) {
$q = $q->where('id', '!=', $this->id);
}
if ($q->where('email', $value)->exists()) {
$fail(__(':attribute is already used', [
'attribute' => $attribute
]));
}
}
],
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
],
'password' => [
'label' => trans('Password'),
'placeholder' => '#M1s3Cure.P4ss0rd!',
'type' => CrudType::password(),
'nullable' => true,
'validate' => [
'max:150'
],
'actions' => [
CrudAction::create(),
CrudAction::delete()
]
],
'site_id' => [
'label' => trans('Site'),
'type' => CrudType::belongsTo(),
'belongsTo' => Site::class,
'actions' => [
CrudAction::create(),
CrudAction::update()
]
],
'blocked' => [
'label' => trans('Blocked'),
'type' => CrudType::boolean(),
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::update(),
],
'getAttribute' => function (CrudModel $model) {
return $model->getAttribute('password_attempts') >= 3;
},
'setAttribute' => function (CrudModel $model, $value) {
$model->setAttribute('password_attempts', $value ? 3 : 0);
return $model->getAttribute('password_attempts');
},
]
];
parent::__construct($attributes);
}
/**
* Has one Site
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function site()
{
return $this->belongsTo(Site::class);
}
}