Skip to content

Commit e0b4005

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents 3ef36e7 + 0be50e8 commit e0b4005

File tree

6 files changed

+73
-18
lines changed

6 files changed

+73
-18
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
110110

111111
# Get dependencies
112112
USER docker
113-
RUN composer install --no-dev --working-dir=/var/www/html
113+
RUN COMPOSER_CACHE_DIR=/dev/null composer install --no-dev --working-dir=/var/www/html && rm -rf /var/www/html/vendor/*/*/.git
114114
USER root
115115

116116
############### APPLICATION INSTALL/INIT #################

app/Http/Controllers/Api/UsersController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ public function selectlist(Request $request) : array
339339
$users = $users->where(function ($query) use ($request) {
340340
$query->SimpleNameSearch($request->get('search'))
341341
->orWhere('username', 'LIKE', '%'.$request->get('search').'%')
342+
->orWhere('email', 'LIKE', '%'.$request->get('search').'%')
342343
->orWhere('employee_num', 'LIKE', '%'.$request->get('search').'%');
343344
});
344345
}

app/Http/Controllers/ProfileController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function postIndex(ImageUploadRequest $request) : RedirectResponse
5353
$user->enable_confetti = $request->input('enable_confetti', false);
5454

5555
if (! config('app.lock_passwords')) {
56-
$user->locale = $request->input('locale', 'en-US');
56+
$user->locale = $request->input('locale');
5757
}
5858

5959
if ((Gate::allows('self.two_factor')) && ((Setting::getSettings()->two_factor_enabled == '1') && (! config('app.lock_passwords')))) {

app/Listeners/CheckoutableListener.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ public function onCheckedOut($event)
6363
}
6464
$ccEmails = array_filter($adminCcEmailsArray);
6565
$mailable = $this->getCheckoutMailType($event, $acceptance);
66-
$notifiable = $this->getNotifiables($event);
66+
$notifiable = $this->getNotifiableUsers($event);
67+
6768

68-
if ($event->checkedOutTo->locale) {
69-
$mailable->locale($event->checkedOutTo->locale);
70-
}
7169
// Send email notifications
7270
try {
7371
/**
@@ -79,7 +77,7 @@ public function onCheckedOut($event)
7977

8078
if ($event->checkoutable->requireAcceptance() || $event->checkoutable->getEula() ||
8179
$this->checkoutableShouldSendEmail($event)) {
82-
Log::info('Sending checkout email, Locale: ' . ($event->checkedOutTo->locale ?? 'default'));
80+
//Log::info('Sending checkout email, Locale: ' . ($event->checkedOutTo->locale ?? 'default'));
8381
if (!empty($notifiable)) {
8482
Mail::to($notifiable)->cc($ccEmails)->send($mailable);
8583
} elseif (!empty($ccEmails)) {
@@ -161,10 +159,8 @@ public function onCheckedIn($event)
161159
}
162160
$ccEmails = array_filter($adminCcEmailsArray);
163161
$mailable = $this->getCheckinMailType($event);
164-
$notifiable = $this->getNotifiables($event);
165-
if ($event->checkedOutTo?->locale) {
166-
$mailable->locale($event->checkedOutTo->locale);
167-
}
162+
$notifiable = $this->getNotifiableUsers($event);
163+
168164
// Send email notifications
169165
try {
170166
/**
@@ -175,7 +171,6 @@ public function onCheckedIn($event)
175171
*/
176172
if ($event->checkoutable->requireAcceptance() || $event->checkoutable->getEula() ||
177173
$this->checkoutableShouldSendEmail($event)) {
178-
Log::info('Sending checkin email, Locale: ' . ($event->checkedOutTo->locale ?? 'default'));
179174
if (!empty($notifiable)) {
180175
Mail::to($notifiable)->cc($ccEmails)->send($mailable);
181176
} elseif (!empty($ccEmails)){
@@ -324,17 +319,26 @@ private function getCheckinMailType($event){
324319
return new $mailable($event->checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note);
325320

326321
}
327-
private function getNotifiables($event){
322+
323+
/**
324+
* This gets the recipient objects based on the type of checkoutable.
325+
* The 'name' property for users is set in the boot method in the User model.
326+
*
327+
* @see \App\Models\User::boot()
328+
* @param $event
329+
* @return mixed
330+
*/
331+
private function getNotifiableUsers($event){
328332

329333
if($event->checkedOutTo instanceof Asset){
330334
$event->checkedOutTo->load('assignedTo');
331-
return $event->checkedOutTo->assignedto?->email ?? '';
335+
return $event->checkedOutTo->assignedto;
332336
}
333337
else if($event->checkedOutTo instanceof Location) {
334-
return $event->checkedOutTo->manager?->email ?? '';
338+
return $event->checkedOutTo->manager;
335339
}
336340
else{
337-
return $event->checkedOutTo?->email ?? '';
341+
return $event->checkedOutTo;
338342
}
339343
}
340344
private function webhookSelected(){

app/Models/User.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Illuminate\Support\Facades\Gate;
2121
use Laravel\Passport\HasApiTokens;
2222
use Watson\Validating\ValidatingTrait;
23+
use Illuminate\Database\Eloquent\Casts\Attribute;
2324

2425
class User extends SnipeModel implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, HasLocalePreference
2526
{
@@ -139,6 +140,29 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
139140
'manager' => ['first_name', 'last_name', 'username'],
140141
];
141142

143+
144+
/**
145+
* This sets the name property on the user. It's not a real field in the database
146+
* (since we use first_name and last_name), but the Laravel mailable method
147+
* uses this to determine the name of the user to send emails to.
148+
*
149+
* We only have to do this on the User model and no other models because other
150+
* first-class objects have a name field.
151+
* @return void
152+
*/
153+
154+
public $name;
155+
156+
protected static function boot()
157+
{
158+
parent::boot();
159+
160+
static::retrieved(function($user){
161+
$user->name = $user->getFullNameAttribute();
162+
});
163+
}
164+
165+
142166
/**
143167
* Internally check the user permission for the given section
144168
*
@@ -279,6 +303,7 @@ public function isActivated()
279303
return $this->activated == 1;
280304
}
281305

306+
282307
/**
283308
* Returns the full name attribute
284309
*
@@ -844,10 +869,22 @@ public function scopeOrderCompany($query, $order)
844869
return $query->leftJoin('companies as companies_user', 'users.company_id', '=', 'companies_user.id')->orderBy('companies_user.name', $order);
845870
}
846871

847-
public function preferredLocale()
872+
873+
874+
875+
/**
876+
* Get the preferred locale for the user.
877+
*
878+
* This uses the HasLocalePreference contract to determine the user's preferred locale,
879+
* used by Laravel's mail system to determine the locale for sending emails.
880+
* https://laravel.com/docs/11.x/mail#user-preferred-locales
881+
*
882+
*/
883+
public function preferredLocale(): string
848884
{
849-
return $this->locale;
885+
return $this->locale ?? Setting::getSettings()->locale ?? config('app.locale');
850886
}
887+
851888
public function getUserTotalCost(){
852889
$asset_cost= 0;
853890
$license_cost= 0;

tests/Feature/Users/Api/UsersForSelectListTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ public function testUsersCanBeSearchedByFirstAndLastName()
4040
$this->assertTrue($results->pluck('text')->contains(fn($text) => str_contains($text, 'Luke')));
4141
}
4242

43+
public function testUsersCanBeSearchedByEmail()
44+
{
45+
User::factory()->create(['first_name' => 'Luke', 'last_name' => 'Skywalker', 'email' => '[email protected]']);
46+
47+
Passport::actingAs(User::factory()->create());
48+
$response = $this->getJson(route('api.users.selectlist', ['search' => 'luke@jedis']))->assertOk();
49+
50+
$results = collect($response->json('results'));
51+
52+
$this->assertEquals(1, $results->count());
53+
$this->assertTrue($results->pluck('text')->contains(fn($text) => str_contains($text, 'Luke')));
54+
}
55+
4356
public function testUsersScopedToCompanyWhenMultipleFullCompanySupportEnabled()
4457
{
4558
$this->settings->enableMultipleFullCompanySupport();

0 commit comments

Comments
 (0)