Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1342 #1349

Merged
merged 3 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace App\Models;

use App\Exceptions\ModelDBException;
use App\Facades\AccessControl;
use App\Models\Extensions\ThrowsConsistentExceptions;
use App\Models\Extensions\UseFixedQueryBuilder;
use App\Models\Extensions\UTCBasedTimes;
use Carbon\Exceptions\InvalidFormatException;
use DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable;
use DarkGhostHunter\Larapass\WebAuthnAuthentication;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -31,13 +34,16 @@
* @property Collection<BaseAlbumImpl> $albums
* @property DatabaseNotificationCollection|DatabaseNotification[] $notifications
* @property Collection<BaseAlbumImpl> $shared
* @property Collection<Photo> $photos
*/
class User extends Authenticatable implements WebAuthnAuthenticatable
{
use Notifiable;
use WebAuthnAuthentication;
use UTCBasedTimes;
use ThrowsConsistentExceptions;
use ThrowsConsistentExceptions {
delete as parentDelete;
}
use UseFixedQueryBuilder;

/**
Expand Down Expand Up @@ -77,6 +83,16 @@ public function albums(): HasMany
return $this->hasMany('App\Models\BaseAlbumImpl', 'owner_id', 'id');
}

/**
* Return the photos owned by the user.
*
* @return HasMany
*/
public function photos(): HasMany
{
return $this->hasMany('App\Models\Photo', 'owner_id', 'id');
}

/**
* Return the albums shared to the user.
*
Expand Down Expand Up @@ -108,4 +124,38 @@ public function name(): string
{
return ($this->id == 0) ? 'Admin' : $this->username;
}

/**
* Deletes a user from the DB and re-assigns ownership of albums and photos
* to the currently authenticated user.
*
* For efficiency reasons the methods performs a mass-update without
* hydrating the actual models.
*
* @return bool always true
*
* @throws ModelDBException
* @throws InvalidFormatException
*/
public function delete(): bool
{
$now = Carbon::now();
$newOwnerID = AccessControl::id();

/** @var HasMany[] $ownershipRelations */
$ownershipRelations = [$this->photos(), $this->albums()];

foreach ($ownershipRelations as $relation) {
// We must also update the `updated_at` column of the related
// models in case clients have cached these models.
$relation->update([
$relation->getForeignKeyName() => $newOwnerID,
$relation->getRelated()->getUpdatedAtColumn() => $relation->getRelated()->fromDateTime($now),
]);
}

$this->shared()->delete();

return $this->parentDelete();
}
}
22 changes: 14 additions & 8 deletions database/migrations/2020_12_12_203153_migrate_admin_user.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
<?php

use App\Exceptions\ModelDBException;
use App\Models\Configs;
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class MigrateAdminUser extends Migration
{
/**
* Run the migrations.
*
* @return void
*
* @throws ModelDBException
*/
public function up()
public function up(): void
{
$user = new User();
$user->username = Configs::get_value('username', '');
$user->password = Configs::get_value('password', '');
$user->save();

// user will have a id which is NOT 0.
// we want this user to have an ID of 0 as it is the ADMIN ID.
// User will have an ID which is NOT 0.
// We want this user to have an ID of 0 as it is the ADMIN ID.
$user->id = 0;
$user->save();
}
Expand All @@ -28,14 +33,15 @@ public function up()
* Reverse the migrations.
*
* @return void
*
* @throws InvalidArgumentException
*/
public function down()
public function down(): void
{
if (Schema::hasTable('users')) {
$user = User::find(0);
if ($user != null) {
$user->delete();
}
DB::table('users')
->where('id', '=', 0)
->delete();
}
}
}