Skip to content

Commit 37c1301

Browse files
committed
Refine
1 parent dbddc6e commit 37c1301

File tree

13 files changed

+163
-7
lines changed

13 files changed

+163
-7
lines changed

app/Enum/ConfigType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ enum ConfigType: string
2525
case ADMIN_USER = 'admin_user';
2626
case LICENSE = 'license';
2727
case MAP_PROVIDER = 'map_provider';
28+
case CURRENCY = 'currency';
2829
}

app/Http/Controllers/Shop/CheckoutController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function process(ProcessRequest $request): CheckoutResource
9797
*/
9898
public function finalize(FinalizeRequest $request): CheckoutResource
9999
{
100+
/** @disregard P1013 */
100101
$order = $this->checkout_service->handlePaymentReturn($request->basket(), $request->all(), $request->provider_type());
101102

102103
if ($order->status !== PaymentStatusType::COMPLETED) {

app/Http/Middleware/ConfigIntegrity.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class ConfigIntegrity
6464
'watermark_shift_x_direction',
6565
'watermark_shift_y',
6666
'watermark_shift_y_direction',
67+
'webshop_enabled',
68+
'webshop_currency',
69+
'webshop_default_description',
6770
'live_metrics_enabled',
6871
'live_metrics_access',
6972
'live_metrics_max_time',

app/Http/Resources/Rights/AlbumRightsResource.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use App\Models\Configs;
1414
use App\Policies\AlbumPolicy;
1515
use Illuminate\Support\Facades\Gate;
16+
use LycheeVerify\Verify;
1617
use Spatie\LaravelData\Data;
1718
use Spatie\TypeScriptTransformer\Attributes\TypeScript;
1819

@@ -30,6 +31,7 @@ class AlbumRightsResource extends Data
3031
public bool $can_access_original = false;
3132
public bool $can_pasword_protect = false;
3233
public bool $can_import_from_server = false;
34+
public bool $can_make_purchasable = false;
3335

3436
/**
3537
* Given an album, returns the access rights associated to it.
@@ -47,5 +49,28 @@ public function __construct(?AbstractAlbum $abstract_album)
4749
$this->can_access_original = Gate::check(AlbumPolicy::CAN_ACCESS_FULL_PHOTO, [AbstractAlbum::class, $abstract_album]);
4850
$this->can_pasword_protect = !Configs::getValueAsBool('cache_enabled');
4951
$this->can_import_from_server = Gate::check(AlbumPolicy::CAN_IMPORT_FROM_SERVER, [AbstractAlbum::class]);
52+
$this->can_make_purchasable = $this->canMakePurchasable($abstract_album);
53+
}
54+
55+
/**
56+
* Check if the user can make the album purchasable.
57+
* This requires the user to be a supporter and have the relevant permission.
58+
*
59+
* @param AbstractAlbum|null $abstract_album
60+
*
61+
* @return bool
62+
*/
63+
public function canMakePurchasable(?AbstractAlbum $abstract_album): bool
64+
{
65+
if (!$abstract_album instanceof Album) {
66+
return false;
67+
}
68+
69+
$verify = resolve(Verify::class);
70+
if (!$verify->is_supporter()) {
71+
return false;
72+
}
73+
74+
return Gate::check(AlbumPolicy::CAN_MAKE_PURCHASABLE, [AbstractAlbum::class]);
5075
}
5176
}

app/Http/Resources/Rights/ModulesRightsResource.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ModulesRightsResource extends Data
3131
public bool $is_watermarker_enabled = false;
3232
public bool $is_photo_timeline_enabled = false;
3333
public bool $is_mod_renamer_enabled = false;
34+
public bool $is_mod_webshop_enabled = false;
3435

3536
public function __construct()
3637
{
@@ -43,6 +44,7 @@ public function __construct()
4344
$this->is_watermarker_enabled = $this->isWatermarkerEnabled($is_logged_in);
4445
$this->is_photo_timeline_enabled = $this->isTimelinePhotosEnabled($is_logged_in);
4546
$this->is_mod_renamer_enabled = $this->isRenamerEnabled();
47+
$this->is_mod_webshop_enabled = $this->isWebshopEnabled();
4648
}
4749

4850
/**
@@ -163,4 +165,18 @@ private function isRenamerEnabled(): bool
163165

164166
return true;
165167
}
168+
169+
/**
170+
* Check if the webshop module is enabled.
171+
*
172+
* @return bool true if the webshop is enabled, false otherwise
173+
*/
174+
private function isWebshopEnabled(): bool
175+
{
176+
if (!$this->verify->check()) {
177+
return false;
178+
}
179+
180+
return Configs::getValueAsBool('webshop_enabled');
181+
}
166182
}

app/Http/Resources/Shop/PriceResource.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@
1515
use Spatie\LaravelData\Data;
1616
use Spatie\TypeScriptTransformer\Attributes\TypeScript;
1717

18+
/**
19+
* This containes the Price information for the purchasable item.
20+
* Both price and price_cents are provided for convenience.
21+
* price is used for display purposes, while price_cents is used for calculations.
22+
*/
1823
#[TypeScript()]
1924
class PriceResource extends Data
2025
{
2126
public function __construct(
2227
public PurchasableSizeVariantType $size_variant,
2328
public PurchasableLicenseType $license_type,
2429
public string $price,
30+
public int $price_cents,
2531
) {
2632
}
2733

@@ -36,6 +42,7 @@ public static function fromModel(PurchasablePrice $price): PriceResource
3642
size_variant: $price->size_variant,
3743
license_type: $price->license_type,
3844
price: $money_service->format($price->price_cents),
45+
price_cents: intval($price->price_cents->getAmount()),
3946
);
4047
}
4148
}

app/Http/Resources/Shop/PurchasableResource.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
namespace App\Http\Resources\Shop;
1010

11+
use App\Models\Configs;
1112
use App\Models\Purchasable;
13+
use App\Models\User;
14+
use Illuminate\Support\Facades\Auth;
1215
use Spatie\LaravelData\Data;
1316
use Spatie\TypeScriptTransformer\Attributes\LiteralTypeScriptType;
1417
use Spatie\TypeScriptTransformer\Attributes\TypeScript;
@@ -33,13 +36,16 @@ public function __construct(
3336
*/
3437
public static function fromModel(Purchasable $item): PurchasableResource
3538
{
39+
/** @var User|null $user */
40+
$user = Auth::user();
41+
3642
return new self(
3743
purchasable_id: $item->id,
3844
album_id: $item->album_id,
3945
photo_id: $item->photo_id,
4046
prices: $item->prices->map(PriceResource::fromModel(...))->toArray(),
41-
owner_notes: $item->owner_notes,
42-
description: $item->description,
47+
owner_notes: $user?->may_administrate === true ? $item->owner_notes : null,
48+
description: $item->description ?? Configs::getValueAsString('webshop_default_description'),
4349
is_active: $item->is_active,
4450
);
4551
}

app/Models/Configs.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ public function sanity(?string $candidate_value, ?string $message_template = nul
152152
$message = sprintf($message_template, 'a valid license');
153153
}
154154
break;
155+
case ConfigType::CURRENCY->value:
156+
$bundle = \ResourceBundle::create('en', 'ICUDATA-curr');
157+
$currencies = $bundle->get('Currencies');
158+
$found = false;
159+
foreach ($currencies as $code => $data) {
160+
$found = ($code === $candidate_value);
161+
if ($found) {
162+
break; // we found it, stop searching
163+
}
164+
}
165+
if (!$found) {
166+
$message = sprintf($message_template, 'a valid ISO 4217 currency code');
167+
break;
168+
}
169+
break;
155170
case ConfigType::MAP_PROVIDER->value:
156171
if (MapProviders::tryFrom($candidate_value) === null) {
157172
$message = sprintf($message_template, 'a valid map provider');

app/Policies/AlbumPolicy.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class AlbumPolicy extends BasePolicy
4444
public const CAN_IMPORT_FROM_SERVER = 'canImportFromServer';
4545
public const CAN_SHARE_ID = 'canShareById';
4646
public const CAN_READ_METRICS = 'canReadMetrics';
47+
public const CAN_MAKE_PURCHASABLE = 'canMakePurchasable';
4748

4849
/**
4950
* This ensures that current album is owned by current user.
@@ -600,4 +601,18 @@ public function canReadMetrics(?User $user, AbstractAlbum $album): bool
600601
default => false,
601602
};
602603
}
604+
605+
/**
606+
* Check whether the user can make the album purchasable.
607+
* Only admins can do that, so we return false here.
608+
* Admin case is handled by the before() method in BasePolicy.
609+
*
610+
* @param User $user
611+
*
612+
* @return bool
613+
*/
614+
public function canMakePurchasable(User $user): bool
615+
{
616+
return false;
617+
}
603618
}

app/Services/MoneyService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace App\Services;
1010

11+
use App\Models\Configs;
1112
use Money\Currencies\ISOCurrencies;
1213
use Money\Currency;
1314
use Money\Formatter\DecimalMoneyFormatter;
@@ -27,7 +28,7 @@ class MoneyService
2728
*/
2829
public function getDefaultCurrencyCode(): string
2930
{
30-
return config('omnipay.currency', 'EUR');
31+
return Configs::getValueAsString('webshop_currency');
3132
}
3233

3334
/**

0 commit comments

Comments
 (0)