mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Code cleanup.
This commit is contained in:
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Repositories\User;
|
||||
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use FireflyIII\Models\GroupMembership;
|
||||
@@ -34,11 +33,9 @@ use FireflyIII\User;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Collection;
|
||||
use Str;
|
||||
|
||||
/**
|
||||
* Class UserRepository.
|
||||
*
|
||||
*/
|
||||
class UserRepository implements UserRepositoryInterface
|
||||
{
|
||||
@@ -46,13 +43,9 @@ class UserRepository implements UserRepositoryInterface
|
||||
* This updates the users email address and records some things so it can be confirmed or undone later.
|
||||
* The user is blocked until the change is confirmed.
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $newEmail
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @see updateEmail
|
||||
*
|
||||
*/
|
||||
public function changeEmail(User $user, string $newEmail): bool
|
||||
{
|
||||
@@ -60,7 +53,7 @@ class UserRepository implements UserRepositoryInterface
|
||||
|
||||
// save old email as pref
|
||||
app('preferences')->setForUser($user, 'previous_email_latest', $oldEmail);
|
||||
app('preferences')->setForUser($user, 'previous_email_' . date('Y-m-d-H-i-s'), $oldEmail);
|
||||
app('preferences')->setForUser($user, 'previous_email_'.date('Y-m-d-H-i-s'), $oldEmail);
|
||||
|
||||
// set undo and confirm token:
|
||||
app('preferences')->setForUser($user, 'email_change_undo_token', bin2hex(random_bytes(16)));
|
||||
@@ -75,12 +68,6 @@ class UserRepository implements UserRepositoryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param string $password
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function changePassword(User $user, string $password): bool
|
||||
{
|
||||
$user->password = bcrypt($password);
|
||||
@@ -89,13 +76,6 @@ class UserRepository implements UserRepositoryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param bool $isBlocked
|
||||
* @param string $code
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function changeStatus(User $user, bool $isBlocked, string $code): bool
|
||||
{
|
||||
// change blocked status and code:
|
||||
@@ -106,21 +86,11 @@ class UserRepository implements UserRepositoryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $displayName
|
||||
* @param string $description
|
||||
*
|
||||
* @return Role
|
||||
*/
|
||||
public function createRole(string $name, string $displayName, string $description): Role
|
||||
{
|
||||
return Role::create(['name' => $name, 'display_name' => $displayName, 'description' => $description]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function deleteInvite(InvitedUser $invite): void
|
||||
{
|
||||
app('log')->debug(sprintf('Deleting invite #%d', $invite->id));
|
||||
@@ -128,10 +98,7 @@ class UserRepository implements UserRepositoryInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function destroy(User $user): bool
|
||||
{
|
||||
@@ -144,12 +111,10 @@ class UserRepository implements UserRepositoryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function deleteEmptyGroups(): void
|
||||
{
|
||||
$groups = UserGroup::get();
|
||||
|
||||
/** @var UserGroup $group */
|
||||
foreach ($groups as $group) {
|
||||
$count = $group->groupMemberships()->count();
|
||||
@@ -160,27 +125,16 @@ class UserRepository implements UserRepositoryInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
return $this->all()->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function all(): Collection
|
||||
{
|
||||
return User::orderBy('id', 'DESC')->get(['users.*']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
*
|
||||
* @return User|null
|
||||
*/
|
||||
public function findByEmail(string $email): ?User
|
||||
{
|
||||
return User::where('email', $email)->first();
|
||||
@@ -188,30 +142,20 @@ class UserRepository implements UserRepositoryInterface
|
||||
|
||||
/**
|
||||
* Returns the first user in the DB. Generally only works when there is just one.
|
||||
*
|
||||
* @return null|User
|
||||
*/
|
||||
public function first(): ?User
|
||||
{
|
||||
return User::orderBy('id', 'ASC')->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getInvitedUsers(): Collection
|
||||
{
|
||||
return InvitedUser::with('user')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRoleByUser(User $user): ?string
|
||||
{
|
||||
/** @var Role|null $role */
|
||||
/** @var null|Role $role */
|
||||
$role = $user->roles()->first();
|
||||
if (null !== $role) {
|
||||
return $role->name;
|
||||
@@ -221,31 +165,27 @@ class UserRepository implements UserRepositoryInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getRolesInGroup(User $user, int $groupId): array
|
||||
{
|
||||
/** @var UserGroup|null $group */
|
||||
/** @var null|UserGroup $group */
|
||||
$group = UserGroup::find($groupId);
|
||||
if (null === $group) {
|
||||
throw new FireflyException(sprintf('Could not find group #%d', $groupId));
|
||||
}
|
||||
$memberships = $group->groupMemberships()->where('user_id', $user->id)->get();
|
||||
$roles = [];
|
||||
|
||||
/** @var GroupMembership $membership */
|
||||
foreach ($memberships as $membership) {
|
||||
$role = $membership->userRole;
|
||||
$roles[] = $role->title;
|
||||
}
|
||||
|
||||
return $roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
*
|
||||
* @return User|null
|
||||
*/
|
||||
public function find(int $userId): ?User
|
||||
{
|
||||
return User::find($userId);
|
||||
@@ -253,17 +193,13 @@ class UserRepository implements UserRepositoryInterface
|
||||
|
||||
/**
|
||||
* Return basic user information.
|
||||
*
|
||||
* @param User $user
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUserData(User $user): array
|
||||
{
|
||||
$return = [];
|
||||
|
||||
// two factor:
|
||||
$return['has_2fa'] = $user->mfa_secret !== null;
|
||||
$return['has_2fa'] = null !== $user->mfa_secret;
|
||||
$return['is_admin'] = $this->hasRole($user, 'owner');
|
||||
$return['blocked'] = 1 === (int)$user->blocked;
|
||||
$return['blocked_code'] = $user->blocked_code;
|
||||
@@ -276,11 +212,12 @@ class UserRepository implements UserRepositoryInterface
|
||||
$return['categories'] = $user->categories()->count();
|
||||
$return['budgets'] = $user->budgets()->count();
|
||||
$return['budgets_with_limits'] = BudgetLimit::distinct()
|
||||
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->where('amount', '>', 0)
|
||||
->whereNull('budgets.deleted_at')
|
||||
->where('budgets.user_id', $user->id)
|
||||
->count('budget_limits.budget_id');
|
||||
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->where('amount', '>', 0)
|
||||
->whereNull('budgets.deleted_at')
|
||||
->where('budgets.user_id', $user->id)
|
||||
->count('budget_limits.budget_id')
|
||||
;
|
||||
$return['rule_groups'] = $user->ruleGroups()->count();
|
||||
$return['rules'] = $user->rules()->count();
|
||||
$return['tags'] = $user->tags()->count();
|
||||
@@ -288,13 +225,7 @@ class UserRepository implements UserRepositoryInterface
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User|Authenticatable|null $user
|
||||
* @param string $role
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasRole(User | Authenticatable | null $user, string $role): bool
|
||||
public function hasRole(null|Authenticatable|User $user, string $role): bool
|
||||
{
|
||||
if (null === $user) {
|
||||
return false;
|
||||
@@ -311,16 +242,13 @@ class UserRepository implements UserRepositoryInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function inviteUser(User | Authenticatable | null $user, string $email): InvitedUser
|
||||
public function inviteUser(null|Authenticatable|User $user, string $email): InvitedUser
|
||||
{
|
||||
$now = today(config('app.timezone'));
|
||||
$now->addDays(2);
|
||||
$invitee = new InvitedUser();
|
||||
$invitee->user()->associate($user);
|
||||
$invitee->invite_code = Str::random(64);
|
||||
$invitee->invite_code = \Str::random(64);
|
||||
$invitee->email = $email;
|
||||
$invitee->redeemed = false;
|
||||
$invitee->expires = $now;
|
||||
@@ -329,9 +257,6 @@ class UserRepository implements UserRepositoryInterface
|
||||
return $invitee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function redeemCode(string $code): void
|
||||
{
|
||||
$obj = InvitedUser::where('invite_code', $code)->where('redeemed', 0)->first();
|
||||
@@ -343,9 +268,6 @@ class UserRepository implements UserRepositoryInterface
|
||||
|
||||
/**
|
||||
* Set MFA code.
|
||||
*
|
||||
* @param User $user
|
||||
* @param string|null $code
|
||||
*/
|
||||
public function setMFACode(User $user, ?string $code): void
|
||||
{
|
||||
@@ -353,11 +275,6 @@ class UserRepository implements UserRepositoryInterface
|
||||
$user->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function store(array $data): User
|
||||
{
|
||||
$user = User::create(
|
||||
@@ -365,7 +282,7 @@ class UserRepository implements UserRepositoryInterface
|
||||
'blocked' => $data['blocked'] ?? false,
|
||||
'blocked_code' => $data['blocked_code'] ?? null,
|
||||
'email' => $data['email'],
|
||||
'password' => Str::random(24),
|
||||
'password' => \Str::random(24),
|
||||
]
|
||||
);
|
||||
$role = $data['role'] ?? '';
|
||||
@@ -376,12 +293,6 @@ class UserRepository implements UserRepositoryInterface
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param string $role
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function attachRole(User $user, string $role): bool
|
||||
{
|
||||
$roleObject = Role::where('name', $role)->first();
|
||||
@@ -401,9 +312,6 @@ class UserRepository implements UserRepositoryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function unblockUser(User $user): void
|
||||
{
|
||||
$user->blocked = false;
|
||||
@@ -414,10 +322,6 @@ class UserRepository implements UserRepositoryInterface
|
||||
/**
|
||||
* Update user info.
|
||||
*
|
||||
* @param User $user
|
||||
* @param array $data
|
||||
*
|
||||
* @return User
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function update(User $user, array $data): User
|
||||
@@ -443,11 +347,8 @@ class UserRepository implements UserRepositoryInterface
|
||||
* This updates the users email address. Same as changeEmail just without most logging. This makes sure that the
|
||||
* undo/confirm routine can't catch this one. The user is NOT blocked.
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $newEmail
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*
|
||||
* @see changeEmail
|
||||
*/
|
||||
public function updateEmail(User $user, string $newEmail): bool
|
||||
@@ -459,7 +360,7 @@ class UserRepository implements UserRepositoryInterface
|
||||
|
||||
// save old email as pref
|
||||
app('preferences')->setForUser($user, 'admin_previous_email_latest', $oldEmail);
|
||||
app('preferences')->setForUser($user, 'admin_previous_email_' . date('Y-m-d-H-i-s'), $oldEmail);
|
||||
app('preferences')->setForUser($user, 'admin_previous_email_'.date('Y-m-d-H-i-s'), $oldEmail);
|
||||
|
||||
$user->email = $newEmail;
|
||||
$user->save();
|
||||
@@ -469,9 +370,6 @@ class UserRepository implements UserRepositoryInterface
|
||||
|
||||
/**
|
||||
* Remove any role the user has.
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $role
|
||||
*/
|
||||
public function removeRole(User $user, string $role): void
|
||||
{
|
||||
@@ -482,23 +380,16 @@ class UserRepository implements UserRepositoryInterface
|
||||
$user->roles()->detach($roleObj->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $role
|
||||
*
|
||||
* @return Role|null
|
||||
*/
|
||||
public function getRole(string $role): ?Role
|
||||
{
|
||||
return Role::where('name', $role)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function validateInviteCode(string $code): bool
|
||||
{
|
||||
$now = today(config('app.timezone'));
|
||||
$invitee = InvitedUser::where('invite_code', $code)->where('expires', '>', $now->format('Y-m-d H:i:s'))->where('redeemed', 0)->first();
|
||||
|
||||
return null !== $invitee;
|
||||
}
|
||||
}
|
||||
|
@@ -36,18 +36,11 @@ interface UserRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Returns a collection of all users.
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function all(): Collection;
|
||||
|
||||
/**
|
||||
* Gives a user a role.
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $role
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function attachRole(User $user, string $role): bool;
|
||||
|
||||
@@ -55,182 +48,74 @@ interface UserRepositoryInterface
|
||||
* This updates the users email address and records some things so it can be confirmed or undone later.
|
||||
* The user is blocked until the change is confirmed.
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $newEmail
|
||||
*
|
||||
* @return bool
|
||||
* @see updateEmail
|
||||
*
|
||||
*/
|
||||
public function changeEmail(User $user, string $newEmail): bool;
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param string $password
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function changePassword(User $user, string $password);
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param bool $isBlocked
|
||||
* @param string $code
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function changeStatus(User $user, bool $isBlocked, string $code): bool;
|
||||
|
||||
/**
|
||||
* Returns a count of all users.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count(): int;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $displayName
|
||||
* @param string $description
|
||||
*
|
||||
* @return Role
|
||||
*/
|
||||
public function createRole(string $name, string $displayName, string $description): Role;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function deleteEmptyGroups(): void;
|
||||
|
||||
/**
|
||||
* @param InvitedUser $invite
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deleteInvite(InvitedUser $invite): void;
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy(User $user): bool;
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
*
|
||||
* @return User|null
|
||||
*/
|
||||
public function find(int $userId): ?User;
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
*
|
||||
* @return User|null
|
||||
*/
|
||||
public function findByEmail(string $email): ?User;
|
||||
|
||||
/**
|
||||
* Returns the first user in the DB. Generally only works when there is just one.
|
||||
*
|
||||
* @return null|User
|
||||
*/
|
||||
public function first(): ?User;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getInvitedUsers(): Collection;
|
||||
|
||||
/**
|
||||
* @param string $role
|
||||
*
|
||||
* @return Role|null
|
||||
*/
|
||||
public function getRole(string $role): ?Role;
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRoleByUser(User $user): ?string;
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param int $groupId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRolesInGroup(User $user, int $groupId): array;
|
||||
|
||||
/**
|
||||
* Return basic user information.
|
||||
*
|
||||
* @param User $user
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUserData(User $user): array;
|
||||
|
||||
/**
|
||||
* @param User|Authenticatable|null $user
|
||||
* @param string $role
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasRole(User | Authenticatable | null $user, string $role): bool;
|
||||
public function hasRole(null|Authenticatable|User $user, string $role): bool;
|
||||
|
||||
/**
|
||||
* @param User|Authenticatable|null $user
|
||||
* @param string $email
|
||||
*
|
||||
* @return InvitedUser
|
||||
*/
|
||||
public function inviteUser(User | Authenticatable | null $user, string $email): InvitedUser;
|
||||
public function inviteUser(null|Authenticatable|User $user, string $email): InvitedUser;
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function redeemCode(string $code): void;
|
||||
|
||||
/**
|
||||
* Remove any role the user has.
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $role
|
||||
*/
|
||||
public function removeRole(User $user, string $role): void;
|
||||
|
||||
/**
|
||||
* Set MFA code.
|
||||
*
|
||||
* @param User $user
|
||||
* @param string|null $code
|
||||
*/
|
||||
public function setMFACode(User $user, ?string $code): void;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function store(array $data): User;
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function unblockUser(User $user): void;
|
||||
|
||||
/**
|
||||
* Update user info.
|
||||
*
|
||||
* @param User $user
|
||||
* @param array $data
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function update(User $user, array $data): User;
|
||||
|
||||
@@ -238,19 +123,9 @@ interface UserRepositoryInterface
|
||||
* This updates the users email address. Same as changeEmail just without most logging. This makes sure that the
|
||||
* undo/confirm routine can't catch this one. The user is NOT blocked.
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $newEmail
|
||||
*
|
||||
* @return bool
|
||||
* @see changeEmail
|
||||
*
|
||||
*/
|
||||
public function updateEmail(User $user, string $newEmail): bool;
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validateInviteCode(string $code): bool;
|
||||
}
|
||||
|
Reference in New Issue
Block a user