Skip to content

Commit 6b68fe4

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents 3461bbf + 4a6520f commit 6b68fe4

File tree

9 files changed

+232
-77
lines changed

9 files changed

+232
-77
lines changed

app/Console/Commands/LdapSync.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public function handle()
256256
$item['telephone'] = $results[$i][$ldap_map["phone"]][0] ?? '';
257257
$item['mobile'] = $results[$i][$ldap_map["mobile"]][0] ?? '';
258258
$item['jobtitle'] = $results[$i][$ldap_map["jobtitle"]][0] ?? '';
259-
$item['address'] = $results[$i][$ldap_map["ldap_address"]][0] ?? '';
259+
$item['address'] = $results[$i][$ldap_map["address"]][0] ?? '';
260260
$item['city'] = $results[$i][$ldap_map["city"]][0] ?? '';
261261
$item['state'] = $results[$i][$ldap_map["state"]][0] ?? '';
262262
$item['country'] = $results[$i][$ldap_map["country"]][0] ?? '';

app/Console/Commands/LdapTroubleshooter.php

Lines changed: 139 additions & 52 deletions
Large diffs are not rendered by default.

app/Http/Controllers/Api/SettingsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function ldaptest() : JsonResponse
8989
}
9090
} catch (\Exception $e) {
9191
Log::debug('Connection failed but we cannot debug it any further on our end.');
92-
return response()->json(['message' => $e->getMessage()], 500);
92+
return response()->json(['message' => $e->getMessage()], 400);
9393
}
9494

9595

app/Mail/CheckoutAccessoryMail.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Mail;
44

55
use App\Models\Accessory;
6+
use App\Models\Asset;
7+
use App\Models\Location;
68
use App\Models\Setting;
79
use App\Models\User;
810
use Illuminate\Bus\Queueable;
@@ -41,7 +43,7 @@ public function envelope(): Envelope
4143

4244
return new Envelope(
4345
from: $from,
44-
subject: (trans('mail.Accessory_Checkout_Notification')),
46+
subject: trans('mail.Accessory_Checkout_Notification'),
4547
);
4648
}
4749

@@ -54,21 +56,53 @@ public function content(): Content
5456
$eula = $this->item->getEula();
5557
$req_accept = $this->item->requireAcceptance();
5658
$accept_url = is_null($this->acceptance) ? null : route('account.accept.item', $this->acceptance);
59+
$name = null;
60+
61+
if($this->target instanceof User){
62+
$name = $this->target->display_name;
63+
}
64+
else if($this->target instanceof Asset){
65+
$name = $this->target->assignedto?->display_name;
66+
}
67+
else if($this->target instanceof Location){
68+
$name = $this->target->manager->name;
69+
}
5770

5871
return new Content(
5972
markdown: 'mail.markdown.checkout-accessory',
6073
with: [
6174
'item' => $this->item,
6275
'admin' => $this->admin,
6376
'note' => $this->note,
64-
'target' => $this->target,
77+
'target' => $name,
6578
'eula' => $eula,
6679
'req_accept' => $req_accept,
6780
'accept_url' => $accept_url,
6881
'checkout_qty' => $this->checkout_qty,
82+
'introduction_line' => $this->introductionLine(),
6983
],
7084
);
7185
}
86+
private function introductionLine(): string
87+
{
88+
if ($this->target instanceof Location) {
89+
return trans('mail.new_item_checked_location', ['location' => $this->target->name ]);
90+
}
91+
if ($this->requiresAcceptance()) {
92+
return trans('mail.new_item_checked_with_acceptance');
93+
}
94+
95+
if (!$this->requiresAcceptance()) {
96+
return trans('mail.new_item_checked');
97+
}
98+
99+
// we shouldn't get here but let's send a default message just in case
100+
return trans('new_item_checked');
101+
}
102+
private function requiresAcceptance(): int|bool
103+
{
104+
return method_exists($this->item, 'requireAcceptance') ? $this->item->requireAcceptance() : 0;
105+
}
72106

73107
/**
74108
* Get the attachments for the message.

app/Mail/CheckoutAssetMail.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Helpers\Helper;
66
use App\Models\Asset;
7+
use App\Models\Location;
78
use App\Models\Setting;
89
use App\Models\User;
910
use Illuminate\Bus\Queueable;
@@ -36,14 +37,6 @@ public function __construct(Asset $asset, $checkedOutTo, User $checkedOutBy, $ac
3637
$this->settings = Setting::getSettings();
3738
$this->target = $checkedOutTo;
3839

39-
// Location is a target option, but there are no emails currently associated with locations.
40-
if($this->target instanceof User){
41-
$this->target = $this->target->display_name;
42-
}
43-
else if($this->target instanceof Asset){
44-
$this->target = $this->target->assignedto?->display_name;
45-
}
46-
4740
$this->last_checkout = '';
4841
$this->expected_checkin = '';
4942

@@ -85,6 +78,17 @@ public function content(): Content
8578
$eula = method_exists($this->item, 'getEula') ? $this->item->getEula() : '';
8679
$req_accept = $this->requiresAcceptance();
8780
$fields = [];
81+
$name = null;
82+
83+
if($this->target instanceof User){
84+
$name = $this->target->display_name;
85+
}
86+
else if($this->target instanceof Asset){
87+
$name = $this->target->assignedto?->display_name;
88+
}
89+
else if($this->target instanceof Location){
90+
$name = $this->target->manager->name;
91+
}
8892

8993
// Check if the item has custom fields associated with it
9094
if (($this->item->model) && ($this->item->model->fieldset)) {
@@ -100,7 +104,7 @@ public function content(): Content
100104
'admin' => $this->admin,
101105
'status' => $this->item->assetstatus?->name,
102106
'note' => $this->note,
103-
'target' => $this->target,
107+
'target' => $name,
104108
'fields' => $fields,
105109
'eula' => $eula,
106110
'req_accept' => $req_accept,
@@ -133,6 +137,9 @@ private function getSubject(): string
133137

134138
private function introductionLine(): string
135139
{
140+
if ($this->firstTimeSending && $this->target instanceof Location) {
141+
return trans('mail.new_item_checked_location', ['location' => $this->target->name ]);
142+
}
136143
if ($this->firstTimeSending && $this->requiresAcceptance()) {
137144
return trans('mail.new_item_checked_with_acceptance');
138145
}

app/Models/Ldap.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,35 @@
2727

2828
class Ldap extends Model
2929
{
30+
public static function ignoreCertificates(bool $ignore_cert = true)
31+
{
32+
if (defined('LDAP_OPT_X_TLS_REQUIRE_CERT') && defined('LDAP_OPT_X_TLS_NEVER')) {
33+
// TODO - we are currently, as a 'safety', doing *both* the following 'new-style' ldap_set_option calls,
34+
// as well as "falling-through" to the 'old-style' putenv() calls.
35+
//
36+
// I *suspect* we can eventually remove the putenv() calls, but I'm just a little nervous about that.
37+
// According to the PHP docs, the LDAP_OPT_X_TLS_REQUIRE_CERT constant has been available since PHP 7.0.
38+
// We're currently using PHP versions way, way later than that (v8.2-v8.4 as of this writing). So it's
39+
// unlikely that these constants wouldn't be defined - unless you didn't have LDAP support in the first
40+
// place. But if that were to happen, I would hope we would've detected that long, long ago, rather than at
41+
// this point.
42+
if ($ignore_cert) {
43+
if (ldap_set_option(null, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_NEVER)) {
44+
//return true;
45+
}
46+
} else {
47+
if (ldap_set_option(null, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_DEMAND)) {
48+
//return true;
49+
}
50+
}
51+
}
52+
if ($ignore_cert) {
53+
return putenv('LDAPTLS_REQCERT=never');
54+
} else {
55+
return putenv('LDAPTLS_REQCERT');
56+
}
57+
}
58+
3059
/**
3160
* Makes a connection to LDAP using the settings in Admin > Settings.
3261
*
@@ -43,15 +72,12 @@ public static function connectToLdap()
4372

4473
// If we are ignoring the SSL cert we need to setup the environment variable
4574
// before we create the connection
46-
if ($ldap_server_cert_ignore == '1') {
47-
putenv('LDAPTLS_REQCERT=never');
48-
}
75+
self::ignoreCertificates((bool)$ldap_server_cert_ignore);
4976

5077
// If the user specifies where CA Certs are, make sure to use them
5178
if (env('LDAPTLS_CACERT')) {
5279
putenv('LDAPTLS_CACERT='.env('LDAPTLS_CACERT'));
5380
}
54-
5581
$connection = @ldap_connect($ldap_host);
5682

5783
if (! $connection) {

resources/lang/en-US/mail.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
'name' => 'Name',
7878
'new_item_checked' => 'A new item has been checked out under your name, details are below.',
7979
'new_item_checked_with_acceptance' => 'A new item has been checked out under your name that requires acceptance, details are below.',
80+
'new_item_checked_location' => 'A new item has been checked out to :location, details are below.',
8081
'recent_item_checked' => 'An item was recently checked out under your name that requires acceptance, details are below.',
8182
'notes' => 'Notes',
8283
'password' => 'Password',

resources/views/mail/markdown/checkout-accessory.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@component('mail::message')
2-
# {{ trans('mail.hello') }}{{ $target->assignedto?->display_name ? ' ' . $target->assignedto->display_name . ',' : ',' }}
2+
# {{ trans('mail.hello').' '.$target.','}}
33

4-
{{ trans('mail.new_item_checked') }}
4+
{{ $introduction_line }}
55

66
@if (($snipeSettings->show_images_in_email =='1') && $item->getImageUrl())
77
<center><img src="{{ $item->getImageUrl() }}" alt="Accessory" style="max-width: 570px;"></center>

tests/Unit/LdapTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testConnect()
2020
$ldap_connect->expects($this->once())->willReturn('hello');
2121

2222
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
23-
$ldap_set_option->expects($this->exactly(3));
23+
$ldap_set_option->expects($this->exactly(4));
2424

2525

2626
$blah = Ldap::connectToLdap();
@@ -84,7 +84,7 @@ public function testFindAndBind()
8484
$ldap_connect->expects($this->once())->willReturn('hello');
8585

8686
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
87-
$ldap_set_option->expects($this->exactly(3));
87+
$ldap_set_option->expects($this->exactly(4));
8888

8989
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(true);
9090

@@ -114,7 +114,7 @@ public function testFindAndBindBadPassword()
114114
$ldap_connect->expects($this->once())->willReturn('hello');
115115

116116
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
117-
$ldap_set_option->expects($this->exactly(3));
117+
$ldap_set_option->expects($this->exactly(4));
118118

119119
// note - we return FALSE first, to simulate a bad-bind, then TRUE the second time to simulate a successful admin bind
120120
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->exactly(2))->willReturn(false, true);
@@ -135,7 +135,7 @@ public function testFindAndBindCannotFindSelf()
135135
$ldap_connect->expects($this->once())->willReturn('hello');
136136

137137
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
138-
$ldap_set_option->expects($this->exactly(3));
138+
$ldap_set_option->expects($this->exactly(4));
139139

140140
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(true);
141141

@@ -156,7 +156,7 @@ public function testFindLdapUsers()
156156
$ldap_connect->expects($this->once())->willReturn('hello');
157157

158158
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
159-
$ldap_set_option->expects($this->exactly(3));
159+
$ldap_set_option->expects($this->exactly(4));
160160

161161
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(true);
162162

@@ -179,7 +179,7 @@ public function testFindLdapUsersPaginated()
179179
$ldap_connect->expects($this->once())->willReturn('hello');
180180

181181
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
182-
$ldap_set_option->expects($this->exactly(3));
182+
$ldap_set_option->expects($this->exactly(4));
183183

184184
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(true);
185185

0 commit comments

Comments
 (0)