1 | <?php |
||
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() |
|
64 | |||
65 | /** |
||
66 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
67 | */ |
||
68 | 109 | protected function groupsManagedRelationship() |
|
72 | |||
73 | /** |
||
74 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
75 | */ |
||
76 | 3 | public function company() |
|
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) |
|
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() |
|
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) |
|
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() |
|
203 | |||
204 | /** |
||
205 | * @param MeetingTimeslot $meetingTimeslot |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function verifyMeetingTimeslot(MeetingTimeslot $meetingTimeslot) |
||
213 | } |
||
214 |