1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace plunner; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\Authenticatable; |
6
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
7
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
8
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
9
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Employee |
15
|
|
|
* |
16
|
|
|
* @author Claudio Cardinale <[email protected]> |
17
|
|
|
* @copyright 2015 Claudio Cardinale |
18
|
|
|
* @version 1.0.0 |
19
|
|
|
* @package plunner |
20
|
|
|
* @property integer $id |
21
|
|
|
* @property string $name |
22
|
|
|
* @property string $email |
23
|
|
|
* @property string $password |
24
|
|
|
* @property integer $company_id |
25
|
|
|
* @property string $remember_token |
26
|
|
|
* @property \Carbon\Carbon $created_at |
27
|
|
|
* @property \Carbon\Carbon $updated_at |
28
|
|
|
* @property-read mixed $is_planner |
29
|
|
|
* @property-read \plunner\Company $company |
30
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\plunner\Group[] $groups |
31
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\plunner\Calendar[] $calendars |
32
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\plunner\Meeting[] $meetings |
33
|
|
|
*/ |
34
|
|
|
class Employee extends Model implements AuthenticatableContract, |
35
|
|
|
AuthorizableContract, |
36
|
|
|
CanResetPasswordContract, |
37
|
|
|
PolicyCheckable |
38
|
|
|
{ |
39
|
|
|
use Authenticatable, Authorizable, CanResetPassword; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The attributes that are mass assignable. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $fillable = ['name', 'email', 'password']; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The attributes excluded from the model's JSON form. |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $hidden = ['password', 'remember_token', 'pivot']; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $appends = ['is_planner']; |
59
|
|
|
|
60
|
37 |
|
public function getIsPlannerAttribute() |
61
|
|
|
{ |
62
|
37 |
|
return !($this->groupsManagedRelationship()->get()->isEmpty()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
67
|
|
|
*/ |
68
|
109 |
|
protected function groupsManagedRelationship() |
69
|
|
|
{ |
70
|
109 |
|
return $this->HasMany(Group::class, 'planner_id'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
75
|
|
|
*/ |
76
|
3 |
|
public function company() |
77
|
|
|
{ |
78
|
3 |
|
return $this->belongsTo('plunner\Company'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
83
|
|
|
*/ |
84
|
54 |
|
public function calendars() |
85
|
|
|
{ |
86
|
54 |
|
return $this->hasMany('plunner\Calendar'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the e-mail address where password reset links are sent. |
91
|
|
|
* This is needed for multiple user type login |
92
|
|
|
* |
93
|
|
|
* Make email unique |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
3 |
|
public function getEmailForPasswordReset() |
98
|
|
|
{ |
99
|
3 |
|
list(, $caller) = debug_backtrace(false); |
100
|
3 |
|
if (isset($caller['class'])) |
101
|
3 |
|
$caller = explode('\\', $caller['class']); |
102
|
|
|
else |
103
|
|
|
$caller = ''; |
104
|
|
|
|
105
|
|
|
//check if this function is called by email sender |
106
|
3 |
|
if ((count($caller) && $caller[count($caller) - 1] == 'PasswordBroker') || (defined('HHVM_VERSION') && $caller == '')) |
107
|
3 |
|
return $this->email; |
108
|
|
|
//return unique identify for token repository |
109
|
3 |
|
return $this->email . $this->company->id; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Group $group |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
6 |
|
public function verifyGroup(Group $group) |
117
|
|
|
{ |
118
|
6 |
|
return $this->belongsToGroup($group); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param Group $group |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
12 |
|
public function belongsToGroup(Group $group) |
126
|
|
|
{ |
127
|
12 |
|
$group = $this->groups()->where('id', $group->id)->first(); |
128
|
12 |
|
return (is_object($group) && $group->exists); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/* |
132
|
|
|
* for a normal employee the policyCheckable methods say if the employee can se or not the element |
133
|
|
|
*/ |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
137
|
|
|
*/ |
138
|
129 |
|
public function groups() |
139
|
|
|
{ |
140
|
129 |
|
return $this->belongsToMany('plunner\Group', 'employee_group', 'employee_id'); //needed for planner model |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Employee $employee |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function verifyEmployee(Employee $employee) |
148
|
|
|
{ |
149
|
|
|
return $employee->company_id === $this->company_id; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param Company $company |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function verifyCompany(Company $company) |
157
|
|
|
{ |
158
|
|
|
return $company->id === $this->company_id; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* the employee can modify a calendar |
163
|
|
|
* @param Calendar $calendar |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
36 |
|
public function verifyCalendar(Calendar $calendar) |
167
|
|
|
{ |
168
|
|
|
//TODO test this |
169
|
36 |
|
return $calendar->employee_id == $this->id; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param Timeslot $timeslot |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
12 |
|
public function verifyTimeslot(Timeslot $timeslot) |
177
|
|
|
{ |
178
|
|
|
//TODO test this |
179
|
12 |
|
return $timeslot->calendar->employee_id == $this->id; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param Meeting $meeting |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
6 |
|
public function verifyMeeting(Meeting $meeting) |
187
|
|
|
{ |
188
|
|
|
//TODO test this |
189
|
6 |
|
$meeting = $this->meetings()->whereId($meeting->id)->first(); |
190
|
6 |
|
return (is_object($meeting) && $meeting->exists); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* meetings where the user participates |
195
|
|
|
* to get all meetings where the user can go user groups with meetings |
196
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
197
|
|
|
*/ |
198
|
21 |
|
public function meetings() |
199
|
|
|
{ |
200
|
|
|
//TODO durign the inserting chek if the meeting is of a group of the user |
201
|
21 |
|
return $this->belongsToMany(Meeting::class); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param MeetingTimeslot $meetingTimeslot |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
public function verifyMeetingTimeslot(MeetingTimeslot $meetingTimeslot) |
209
|
|
|
{ |
210
|
|
|
//TODO implement and test this |
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|