-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUser.php
115 lines (99 loc) · 3.3 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
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Input;
use jeremykenedy\LaravelRoles\Models\Role;
use jeremykenedy\LaravelRoles\Traits\HasRoleAndPermission;
class User extends Authenticatable
{
use Notifiable;
use HasRoleAndPermission;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
"name", "email", "password"
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
"password", "remember_token", "created_by"
];
public static function boot()
{
parent::boot();
self::saving(function ($model) {
if (!\Auth::guest()) $model->created_by = \Auth::user()->id;
$model->setExtraFields();
});
}
public static function paginate($size)
{
$user = \Auth::user();
if ($user->isSubAdmin())
return parent::where('id', '<>', $user->id)->where(["created_by" => $user->id])->paginate($size);
else if ($user->isAdmin()){
$usersId = [];
Role::where('slug', 'admin')->orWhere('slug', 'subadmin')->get()->each(function($role) use (&$usersId) {
return $role->users->each(function($user) use (&$usersId){
$usersId[] = $user->id;
});
});
sort($usersId);
return parent::where('id', '<>', $user->id)->whereIn('id', $usersId)->paginate($size);
}
}
private static function getSubAdminRole()
{
$roleSubAdmin = Role::where(["slug" => "subadmin"])->first();
if ($roleSubAdmin) return $roleSubAdmin;
return Role::create(["name" => "subadmin", "description" => "subadmin", "level" => 4, "slug" => "subadmin"]);
}
public function isSubAdmin()
{
return $this->hasRole("subadmin");
}
public function setExtraFields()
{
$this->setInputVar('zip_code');
$this->setInputVar('city_id');
$this->setInputVar('address');
$this->setInputVar('is_company');
$this->setInputVar('cpf_cnpj');
$this->setInputVar('accession', '\Helper','moneyToFloat');
$this->setInputVar('payment_day');
$this->setInputVar('neighborhood');
$this->setInputVar('payment_monthy', '\Helper','moneyToFloat');
$this->setInputVar('validation', '\Helper','parseDate');
}
private function setInputVar($key, $class = null, $method = null){
$value = Input::post($key);
if (!$value) return;
$this->{$key} = ($class && $method) ? call_user_func([$class, $method], $value) : $value;
}
public function city()
{
return $this->hasOne('App\City', 'id', 'city_id');
}
public function contacts()
{
return $this->hasMany('App\Contact', 'user_id', 'id');
}
public function invalid()
{
if (!$this->validation) return false;
return strtotime($this->validation) < strtotime(date("Y-m-d H:i:s"));
}
public function users(){
return $this->hasMany('App\User', 'created_by', 'id');
}
public function vehicles(){
return $this->hasMany('App\Vehicle', 'final_user_id', 'id');
}
}