-
Notifications
You must be signed in to change notification settings - Fork 97
/
User.php
171 lines (149 loc) · 4.31 KB
/
User.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
163
164
165
166
167
168
169
170
171
<?php namespace October\Test\Models;
use Model;
/**
* User Model
*/
class User extends Model
{
use \October\Rain\Database\Traits\Validation;
/**
* implement the TranslatableModel behavior for attachments
*/
public $implement = ['@RainLab.Translate.Behaviors.TranslatableModel'];
/**
* @var string The database table used by the model.
*/
public $table = 'october_test_users';
/**
* @var array Guarded fields
*/
protected $guarded = [];
/**
* @var array fillable fields
*/
protected $fillable = [];
/**
* @var array translatable attributes
*/
public $translatable = [];
/**
* @var array jsonable attribute names that are json encoded and decoded from the database
*/
protected $jsonable = [
'media_files',
'media_images',
];
/**
* @var array Rules
*/
public $rules = [
'username' => 'required',
'photo' => 'required',
'portfolio' => 'required',
'roles' => 'required',
];
/**
* @var array customMessages
*/
public $customMessages = [
'required' => 'The :attribute field is WOW required.',
'photo.required' => 'The :attribute field is required PHOTO.',
// @see method beforeValidate
// 'username.required' => 'Say hello (Username field required)',
];
/**
* beforeValidate uses hacky validation messages
*/
public function beforeValidate()
{
\Lang::set('validation.custom.username.required', 'Say hello (Username field required)');
}
/**
* @var array Relations
*/
public $belongsTo = [
'country' => Country::class
];
public $hasMany = [
'posts' => Post::class
];
public $belongsToMany = [
'roles' => [
Role::class,
'table' => 'october_test_users_roles',
'timestamps' => true,
'order' => 'name'
],
// @deprecated
// 'roles_count' => [
// Role::class,
// 'table' => 'october_test_users_roles',
// 'count' => true
// ],
'roles_pivot' => [
Role::class,
'table' => 'october_test_users_roles',
'pivot' => ['clearance_level', 'is_executive'],
'timestamps' => true
],
'roles_pivot_model' => [
Role::class,
'table' => 'october_test_users_roles',
'pivot' => ['clearance_level', 'is_executive', 'country_id', 'evolution', 'salary'],
'pivotKey' => 'id',
'pivotModel' => UserRolePivot::class,
'timestamps' => true,
],
];
public $attachOne = [
'photo' => \System\Models\File::class,
'photo_secure' => [\System\Models\File::class, 'public' => false],
'certificate' => [\System\Models\File::class],
'certificate_secure' => [\System\Models\File::class, 'public' => false],
'custom_file' => CustomFile::class
];
public $attachMany = [
'portfolio' => \System\Models\File::class,
'portfolio_secure' => [\System\Models\File::class, 'public' => false],
'files' => [\System\Models\File::class],
'files_secure' => [\System\Models\File::class, 'public' => false],
];
/**
* __construct
*/
public function __construct(...$args)
{
parent::__construct(...$args);
$this->bindEvent('model.relation.detach', function ($relationName, $ids) {
traceLog("Relation {$relationName} was removed", (array) $ids);
});
$this->bindEvent('model.relation.attach', function ($relationName, $ids, $attributes) {
traceLog("New relation {$relationName} was created", $ids, $attributes);
});
}
/**
* filterScopes
*/
public function filterScopes($scopes)
{
if ($scopes->disable_roles->value) {
$scopes->roles->hidden(true);
}
}
/**
* scopeApplyRoleFilter
*/
public function scopeApplyRoleFilter($query, $scope)
{
return $query->whereHas('roles', function($q) use ($scope) {
$q->whereIn('id', $scope->value);
});
}
/**
* getUserAndCode
*/
public function getUserAndCode()
{
return "{$this->username} ({$this->security_code})";
}
}