- Added standard Laravel directory structure and configuration. - Included Svelte and Tailwind configuration for the admin interface. - Added core PHPUnit and testing scripts.
87 lines
2.1 KiB
PHP
87 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Service to handle User-related operations.
|
|
*/
|
|
class UserService
|
|
{
|
|
/**
|
|
* Store a new user.
|
|
*
|
|
* @param array $data Data to create the user.
|
|
* @return User|null The created user or null on failure.
|
|
*/
|
|
public function store(array $data): ?User
|
|
{
|
|
return DB::transaction(function () use ($data) {
|
|
$user = User::create([
|
|
'name' => $data['name'],
|
|
'email' => $data['email'],
|
|
'password' => Hash::make($data['password']),
|
|
]);
|
|
|
|
if (isset($data['roles'])) {
|
|
$user->roles()->sync($data['roles']);
|
|
}
|
|
|
|
return $user;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Update an existing user.
|
|
*
|
|
* @param User $user The user to update.
|
|
* @param array $data Data to update the user.
|
|
* @return bool True if successful.
|
|
*/
|
|
public function update(User $user, array $data): bool
|
|
{
|
|
return DB::transaction(function () use ($user, $data) {
|
|
$updateData = [
|
|
'name' => $data['name'],
|
|
];
|
|
|
|
// Only update email if not protected
|
|
if (!$user->is_protected && isset($data['email'])) {
|
|
$updateData['email'] = $data['email'];
|
|
}
|
|
|
|
if (!empty($data['password'])) {
|
|
$updateData['password'] = Hash::make($data['password']);
|
|
}
|
|
|
|
$user->update($updateData);
|
|
|
|
if (isset($data['roles'])) {
|
|
$user->roles()->sync($data['roles']);
|
|
} else {
|
|
$user->roles()->sync([]);
|
|
}
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Delete a user.
|
|
*
|
|
* @param User $user The user to delete.
|
|
* @return bool True if successful, false if protected.
|
|
*/
|
|
public function delete(User $user): bool
|
|
{
|
|
if ($user->is_protected) {
|
|
return false;
|
|
}
|
|
|
|
return $user->delete();
|
|
}
|
|
}
|