update
This commit is contained in:
@@ -324,6 +324,64 @@ class CustomerController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
public function sendPasswordEmail(Request $request, string $uuid): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'password' => 'required|string|min:8|max:64',
|
||||
]);
|
||||
|
||||
$customer = $this->customerRepository->findBy('uuid', $uuid);
|
||||
|
||||
if (!$customer) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => "Can't find customer by UUID #" . $uuid,
|
||||
], 404);
|
||||
}
|
||||
|
||||
$password = (string) $request->input('password');
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$this->customerRepository->update($customer->id, [
|
||||
'password' => Hash::make($password),
|
||||
]);
|
||||
|
||||
\Illuminate\Support\Facades\Mail::to($customer->email)->send(
|
||||
new \App\Mail\Customer\PasswordResetByAdmin([
|
||||
'fullname' => $customer->fullname,
|
||||
'email' => $customer->email,
|
||||
'password' => $password,
|
||||
])
|
||||
);
|
||||
|
||||
Log::create([
|
||||
'agent' => 'admin',
|
||||
'client_ip' => $request->ip(),
|
||||
'description' => sprintf(
|
||||
'Admin gửi lại mật khẩu mới cho customer %s',
|
||||
$customer->email,
|
||||
),
|
||||
'customer_id' => $customer->id,
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã gửi mật khẩu mới cho khách hàng.',
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function addCredit(Request $request, string $uuid): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
@@ -562,6 +620,82 @@ class CustomerController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function getDnsDomains(string $uuid): JsonResponse
|
||||
{
|
||||
$customer = $this->customerRepository->findBy('uuid', $uuid);
|
||||
|
||||
if (!$customer) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => "Can't find customer by UUID #" . $uuid,
|
||||
], 404);
|
||||
}
|
||||
|
||||
$domains = \App\Models\DnsDomain::query()
|
||||
->with('server:id,name,ip_address')
|
||||
->withCount(['ns', 'record'])
|
||||
->whereHas('customer', fn ($q) => $q->where('customers.id', $customer->id))
|
||||
->orderByDesc('id')
|
||||
->get()
|
||||
->map(function ($d) {
|
||||
return [
|
||||
'id' => $d->id,
|
||||
'domain_name' => $d->domain_name,
|
||||
'is_created' => (int) $d->is_created,
|
||||
'plesk_id' => $d->plesk_id,
|
||||
'ns_count' => (int) ($d->ns_count ?? 0),
|
||||
'record_count' => (int) ($d->record_count ?? 0),
|
||||
'server' => $d->server ? [
|
||||
'id' => $d->server->id,
|
||||
'name' => $d->server->name,
|
||||
'ip_address' => $d->server->ip_address,
|
||||
] : null,
|
||||
'created_at' => $d->created_at,
|
||||
'updated_at' => $d->updated_at,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $domains,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getDnsDomainRecords(string $uuid, int $domainId): JsonResponse
|
||||
{
|
||||
$customer = $this->customerRepository->findBy('uuid', $uuid);
|
||||
|
||||
if (!$customer) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => "Can't find customer by UUID #" . $uuid,
|
||||
], 404);
|
||||
}
|
||||
|
||||
$owns = \Illuminate\Support\Facades\DB::table('dns_domain_user')
|
||||
->where('domain_id', $domainId)
|
||||
->where('user_id', $customer->id)
|
||||
->exists();
|
||||
|
||||
if (!$owns) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Domain không thuộc về khách hàng này',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$records = \App\Models\DnsRecord::query()
|
||||
->where('dns_manager_domain_id', $domainId)
|
||||
->orderBy('type')
|
||||
->orderBy('name')
|
||||
->get();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $records,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getServices(string $uuid, Request $request): JsonResponse
|
||||
{
|
||||
$services = $this->serviceRepository->filter(
|
||||
|
||||
@@ -4,12 +4,14 @@ namespace App\Http\Controllers\Api\Client;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\Client\ClientAuthRequest;
|
||||
use App\Http\Requests\Api\Client\ClientChangePasswordRequest;
|
||||
use App\Http\Requests\Api\Client\ClientRegisterRequest;
|
||||
use App\Http\Requests\Api\Client\ClientUpdateRequest;
|
||||
use App\Http\Resources\Api\Client\ClientResource;
|
||||
use App\Services\ActivityLogger;
|
||||
use App\Services\Api\Client\ClientAuthService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Tymon\JWTAuth\Facades\JWTAuth;
|
||||
use Tymon\JWTAuth\Exceptions\JWTException;
|
||||
|
||||
@@ -307,6 +309,68 @@ class ClientController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
public function forgotPassword(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'email' => ['required', 'email', 'max:255'],
|
||||
]);
|
||||
|
||||
$email = strtolower(trim((string) $request->input('email')));
|
||||
|
||||
$customer = \App\Models\Customer::whereRaw('LOWER(email) = ?', [$email])->first();
|
||||
|
||||
if (! $customer) {
|
||||
// Do not disclose whether the email exists.
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Nếu email tồn tại, mật khẩu mới đã được gửi đến hộp thư của bạn.',
|
||||
]);
|
||||
}
|
||||
|
||||
$alphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789!@#$%^&*';
|
||||
$newPassword = '';
|
||||
$max = strlen($alphabet) - 1;
|
||||
for ($i = 0; $i < 16; $i++) {
|
||||
$newPassword .= $alphabet[random_int(0, $max)];
|
||||
}
|
||||
|
||||
try {
|
||||
\Illuminate\Support\Facades\DB::beginTransaction();
|
||||
|
||||
$customer->password = Hash::make($newPassword);
|
||||
$customer->save();
|
||||
|
||||
\Illuminate\Support\Facades\Mail::to($customer->email)->send(
|
||||
new \App\Mail\Customer\PasswordResetByAdmin([
|
||||
'fullname' => $customer->fullname,
|
||||
'email' => $customer->email,
|
||||
'password' => $newPassword,
|
||||
])
|
||||
);
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'password_forgot',
|
||||
logName: 'Customer',
|
||||
description: "Customer {$customer->email} yêu cầu cấp lại mật khẩu",
|
||||
subject: $customer,
|
||||
);
|
||||
|
||||
\Illuminate\Support\Facades\DB::commit();
|
||||
} catch (\Throwable) {
|
||||
\Illuminate\Support\Facades\DB::rollBack();
|
||||
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Không thể gửi email, vui lòng thử lại sau.',
|
||||
], 500);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Mật khẩu mới đã được gửi đến email của bạn.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(ClientUpdateRequest $request)
|
||||
{
|
||||
$user = auth('client')->user();
|
||||
@@ -335,4 +399,31 @@ class ClientController extends Controller
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function changePassword(ClientChangePasswordRequest $request)
|
||||
{
|
||||
$user = auth('client')->user();
|
||||
|
||||
if (!Hash::check($request->input('current_password'), $user->password)) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Mật khẩu hiện tại không chính xác',
|
||||
], 422);
|
||||
}
|
||||
|
||||
$user->password = bcrypt($request->input('password'));
|
||||
$user->save();
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'password_change',
|
||||
logName: 'Customer',
|
||||
description: "Customer {$user->email} đã đổi mật khẩu",
|
||||
subject: $user,
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đổi mật khẩu thành công',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user