2016-03-12 14:18:28 +01:00
|
|
|
<?php
|
2022-12-29 19:42:26 +01:00
|
|
|
|
2016-03-12 14:18:28 +01:00
|
|
|
/**
|
|
|
|
* UserRepository.php
|
2020-02-16 14:00:57 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-03-12 14:18:28 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 08:40:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-03-12 14:18:28 +01:00
|
|
|
*/
|
2017-03-24 15:01:53 +01:00
|
|
|
declare(strict_types=1);
|
2016-05-20 12:41:23 +02:00
|
|
|
|
2016-03-12 14:18:28 +01:00
|
|
|
namespace FireflyIII\Repositories\User;
|
|
|
|
|
2021-03-12 06:20:01 +01:00
|
|
|
use Exception;
|
2021-09-18 10:20:19 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-10-15 07:11:53 +02:00
|
|
|
use FireflyIII\Models\BudgetLimit;
|
2023-01-29 07:00:26 +01:00
|
|
|
use FireflyIII\Models\GroupMembership;
|
2022-10-01 12:21:42 +02:00
|
|
|
use FireflyIII\Models\InvitedUser;
|
2016-03-12 14:18:28 +01:00
|
|
|
use FireflyIII\Models\Role;
|
2021-10-26 06:20:33 +02:00
|
|
|
use FireflyIII\Models\UserGroup;
|
2016-03-12 14:18:28 +01:00
|
|
|
use FireflyIII\User;
|
2023-02-22 19:54:19 +01:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2018-07-22 18:50:27 +02:00
|
|
|
use Illuminate\Database\QueryException;
|
2016-04-03 07:07:17 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2023-04-01 07:04:42 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-07-31 06:19:48 +02:00
|
|
|
use Str;
|
2016-03-12 14:18:28 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class UserRepository.
|
2018-07-25 19:43:02 +02:00
|
|
|
*
|
2016-03-12 14:18:28 +01:00
|
|
|
*/
|
|
|
|
class UserRepository implements UserRepositoryInterface
|
|
|
|
{
|
2017-09-26 08:52:16 +02:00
|
|
|
/**
|
2017-09-26 09:15:21 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param string $newEmail
|
2017-09-26 08:52:16 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2021-03-12 06:20:01 +01:00
|
|
|
* @throws Exception
|
2019-08-22 17:06:43 +02:00
|
|
|
* @see updateEmail
|
|
|
|
*
|
2017-09-26 08:52:16 +02:00
|
|
|
*/
|
|
|
|
public function changeEmail(User $user, string $newEmail): bool
|
|
|
|
{
|
|
|
|
$oldEmail = $user->email;
|
|
|
|
|
|
|
|
// save old email as pref
|
2018-07-15 09:27:38 +02:00
|
|
|
app('preferences')->setForUser($user, 'previous_email_latest', $oldEmail);
|
2022-12-29 19:42:26 +01:00
|
|
|
app('preferences')->setForUser($user, 'previous_email_'.date('Y-m-d-H-i-s'), $oldEmail);
|
2017-09-26 08:52:16 +02:00
|
|
|
|
|
|
|
// set undo and confirm token:
|
2018-07-22 18:50:27 +02:00
|
|
|
app('preferences')->setForUser($user, 'email_change_undo_token', bin2hex(random_bytes(16)));
|
|
|
|
app('preferences')->setForUser($user, 'email_change_confirm_token', bin2hex(random_bytes(16)));
|
2017-09-26 08:52:16 +02:00
|
|
|
// update user
|
|
|
|
|
|
|
|
$user->email = $newEmail;
|
2021-04-06 18:48:02 +02:00
|
|
|
$user->blocked = true;
|
2017-09-26 08:52:16 +02:00
|
|
|
$user->blocked_code = 'email_changed';
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-30 13:47:23 +01:00
|
|
|
/**
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param string $password
|
2016-12-30 13:47:23 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function changePassword(User $user, string $password): bool
|
|
|
|
{
|
|
|
|
$user->password = bcrypt($password);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-24 15:01:53 +01:00
|
|
|
/**
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param bool $isBlocked
|
|
|
|
* @param string $code
|
2017-03-24 15:01:53 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function changeStatus(User $user, bool $isBlocked, string $code): bool
|
|
|
|
{
|
|
|
|
// change blocked status and code:
|
|
|
|
$user->blocked = $isBlocked;
|
|
|
|
$user->blocked_code = $code;
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-21 18:06:57 +01:00
|
|
|
/**
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param string $name
|
|
|
|
* @param string $displayName
|
|
|
|
* @param string $description
|
2018-01-21 18:06:57 +01:00
|
|
|
*
|
|
|
|
* @return Role
|
|
|
|
*/
|
|
|
|
public function createRole(string $name, string $displayName, string $description): Role
|
|
|
|
{
|
|
|
|
return Role::create(['name' => $name, 'display_name' => $displayName, 'description' => $description]);
|
|
|
|
}
|
|
|
|
|
2023-03-09 06:33:23 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function deleteInvite(InvitedUser $invite): void
|
|
|
|
{
|
|
|
|
Log::debug(sprintf('Deleting invite #%d', $invite->id));
|
|
|
|
$invite->delete();
|
|
|
|
}
|
|
|
|
|
2016-10-20 19:10:43 +02:00
|
|
|
/**
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param User $user
|
2016-12-12 15:24:47 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2021-03-12 06:20:01 +01:00
|
|
|
* @throws Exception
|
2016-12-12 15:24:47 +01:00
|
|
|
*/
|
|
|
|
public function destroy(User $user): bool
|
|
|
|
{
|
|
|
|
Log::debug(sprintf('Calling delete() on user %d', $user->id));
|
2021-10-26 06:20:33 +02:00
|
|
|
|
|
|
|
$user->groupMemberships()->delete();
|
2016-12-12 15:24:47 +01:00
|
|
|
$user->delete();
|
2021-10-26 06:20:33 +02:00
|
|
|
$this->deleteEmptyGroups();
|
2016-12-12 15:24:47 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-29 14:59:58 +02:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function deleteEmptyGroups(): void
|
|
|
|
{
|
|
|
|
$groups = UserGroup::get();
|
|
|
|
/** @var UserGroup $group */
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
$count = $group->groupMemberships()->count();
|
|
|
|
if (0 === $count) {
|
|
|
|
Log::info(sprintf('Deleted empty group #%d ("%s")', $group->id, $group->title));
|
|
|
|
$group->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 12:21:42 +02:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function count(): int
|
|
|
|
{
|
|
|
|
return $this->all()->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function all(): Collection
|
|
|
|
{
|
|
|
|
return User::orderBy('id', 'DESC')->get(['users.*']);
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:44:33 +02:00
|
|
|
/**
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param string $email
|
2018-03-30 16:44:33 +02:00
|
|
|
*
|
|
|
|
* @return User|null
|
|
|
|
*/
|
2021-09-18 10:26:12 +02:00
|
|
|
public function findByEmail(string $email): ?User
|
2018-03-30 16:44:33 +02:00
|
|
|
{
|
2021-09-18 10:26:12 +02:00
|
|
|
return User::where('email', $email)->first();
|
2018-03-30 16:44:33 +02:00
|
|
|
}
|
|
|
|
|
2017-12-26 17:33:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the first user in the DB. Generally only works when there is just one.
|
|
|
|
*
|
|
|
|
* @return null|User
|
|
|
|
*/
|
|
|
|
public function first(): ?User
|
|
|
|
{
|
2018-07-24 17:46:34 +02:00
|
|
|
return User::orderBy('id', 'ASC')->first();
|
2017-12-26 17:33:53 +01:00
|
|
|
}
|
|
|
|
|
2018-12-20 22:03:34 +01:00
|
|
|
/**
|
2022-12-29 19:42:26 +01:00
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getInvitedUsers(): Collection
|
|
|
|
{
|
|
|
|
return InvitedUser::with('user')->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
2018-12-20 22:03:34 +01:00
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getRoleByUser(User $user): ?string
|
|
|
|
{
|
2021-04-06 18:48:02 +02:00
|
|
|
/** @var Role|null $role */
|
2018-12-20 22:03:34 +01:00
|
|
|
$role = $user->roles()->first();
|
|
|
|
if (null !== $role) {
|
|
|
|
return $role->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-02-22 18:14:14 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function getRolesInGroup(User $user, int $groupId): array
|
|
|
|
{
|
|
|
|
/** @var 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);
|
|
|
|
}
|
|
|
|
|
2016-10-15 07:11:53 +02:00
|
|
|
/**
|
|
|
|
* Return basic user information.
|
|
|
|
*
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param User $user
|
2016-10-15 07:11:53 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getUserData(User $user): array
|
|
|
|
{
|
|
|
|
$return = [];
|
|
|
|
|
|
|
|
// two factor:
|
2019-08-22 17:06:43 +02:00
|
|
|
$return['has_2fa'] = $user->mfa_secret !== null;
|
|
|
|
$return['is_admin'] = $this->hasRole($user, 'owner');
|
2022-12-29 19:42:26 +01:00
|
|
|
$return['blocked'] = 1 === (int)$user->blocked;
|
2019-08-22 17:06:43 +02:00
|
|
|
$return['blocked_code'] = $user->blocked_code;
|
|
|
|
$return['accounts'] = $user->accounts()->count();
|
|
|
|
$return['journals'] = $user->transactionJournals()->count();
|
|
|
|
$return['transactions'] = $user->transactions()->count();
|
|
|
|
$return['attachments'] = $user->attachments()->count();
|
2016-10-15 07:11:53 +02:00
|
|
|
$return['attachments_size'] = $user->attachments()->sum('size');
|
|
|
|
$return['bills'] = $user->bills()->count();
|
|
|
|
$return['categories'] = $user->categories()->count();
|
|
|
|
$return['budgets'] = $user->budgets()->count();
|
2016-11-28 20:38:03 +01:00
|
|
|
$return['budgets_with_limits'] = BudgetLimit::distinct()
|
2016-12-04 18:02:19 +01:00
|
|
|
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
|
|
|
->where('amount', '>', 0)
|
|
|
|
->whereNull('budgets.deleted_at')
|
2021-04-06 18:48:02 +02:00
|
|
|
->where('budgets.user_id', $user->id)
|
2021-04-07 05:55:51 +02:00
|
|
|
->count('budget_limits.budget_id');
|
2016-10-15 07:11:53 +02:00
|
|
|
$return['rule_groups'] = $user->ruleGroups()->count();
|
|
|
|
$return['rules'] = $user->rules()->count();
|
|
|
|
$return['tags'] = $user->tags()->count();
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2017-03-19 17:54:21 +01:00
|
|
|
|
|
|
|
/**
|
2023-02-22 19:54:19 +01:00
|
|
|
* @param User|Authenticatable|null $user
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param string $role
|
2017-03-19 17:54:21 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2023-03-09 06:33:23 +01:00
|
|
|
public function hasRole(User|Authenticatable|null $user, string $role): bool
|
|
|
|
{
|
|
|
|
if (null === $user) {
|
2023-02-22 19:54:19 +01:00
|
|
|
return false;
|
|
|
|
}
|
2023-03-09 06:33:23 +01:00
|
|
|
/** @var Role $userRole */
|
|
|
|
foreach ($user->roles as $userRole) {
|
|
|
|
if ($userRole->name === $role) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-26 09:15:21 +02:00
|
|
|
|
2022-10-01 12:21:42 +02:00
|
|
|
/**
|
2023-03-09 06:33:23 +01:00
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2023-02-22 19:54:19 +01:00
|
|
|
public function inviteUser(User|Authenticatable|null $user, string $email): InvitedUser
|
2022-10-01 12:21:42 +02:00
|
|
|
{
|
2023-02-11 07:36:45 +01:00
|
|
|
$now = today(config('app.timezone'));
|
2022-10-01 12:21:42 +02:00
|
|
|
$now->addDays(2);
|
2022-10-30 14:24:37 +01:00
|
|
|
$invitee = new InvitedUser();
|
2022-10-01 12:21:42 +02:00
|
|
|
$invitee->user()->associate($user);
|
|
|
|
$invitee->invite_code = Str::random(64);
|
|
|
|
$invitee->email = $email;
|
|
|
|
$invitee->redeemed = false;
|
|
|
|
$invitee->expires = $now;
|
|
|
|
$invitee->save();
|
|
|
|
|
|
|
|
return $invitee;
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:42:26 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function redeemCode(string $code): void
|
|
|
|
{
|
|
|
|
$obj = InvitedUser::where('invite_code', $code)->where('redeemed', 0)->first();
|
|
|
|
if ($obj) {
|
|
|
|
$obj->redeemed = true;
|
|
|
|
$obj->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 17:06:43 +02:00
|
|
|
/**
|
|
|
|
* Set MFA code.
|
|
|
|
*
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param string|null $code
|
2019-08-22 17:06:43 +02:00
|
|
|
*/
|
|
|
|
public function setMFACode(User $user, ?string $code): void
|
|
|
|
{
|
|
|
|
$user->mfa_secret = $code;
|
|
|
|
$user->save();
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:33:53 +01:00
|
|
|
/**
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param array $data
|
2017-12-26 17:33:53 +01:00
|
|
|
*
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function store(array $data): User
|
|
|
|
{
|
2018-12-03 07:18:05 +01:00
|
|
|
$user = User::create(
|
2017-12-26 17:33:53 +01:00
|
|
|
[
|
2018-03-03 08:12:18 +01:00
|
|
|
'blocked' => $data['blocked'] ?? false,
|
|
|
|
'blocked_code' => $data['blocked_code'] ?? null,
|
|
|
|
'email' => $data['email'],
|
2020-07-31 06:19:48 +02:00
|
|
|
'password' => Str::random(24),
|
2017-12-26 17:33:53 +01:00
|
|
|
]
|
|
|
|
);
|
2018-12-03 07:18:05 +01:00
|
|
|
$role = $data['role'] ?? '';
|
|
|
|
if ('' !== $role) {
|
|
|
|
$this->attachRole($user, $role);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
2017-12-26 17:33:53 +01:00
|
|
|
}
|
|
|
|
|
2022-03-29 14:59:58 +02:00
|
|
|
/**
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param string $role
|
2022-03-29 14:59:58 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function attachRole(User $user, string $role): bool
|
|
|
|
{
|
|
|
|
$roleObject = Role::where('name', $role)->first();
|
|
|
|
if (null === $roleObject) {
|
|
|
|
Log::error(sprintf('Could not find role "%s" in attachRole()', $role));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$user->roles()->attach($roleObject);
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
// don't care
|
|
|
|
Log::error(sprintf('Query exception when giving user a role: %s', $e->getMessage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-17 14:06:14 +01:00
|
|
|
/**
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param User $user
|
2017-12-17 14:06:14 +01:00
|
|
|
*/
|
|
|
|
public function unblockUser(User $user): void
|
|
|
|
{
|
2021-04-07 05:55:51 +02:00
|
|
|
$user->blocked = false;
|
2017-12-17 14:06:14 +01:00
|
|
|
$user->blocked_code = '';
|
|
|
|
$user->save();
|
|
|
|
}
|
|
|
|
|
2018-03-03 08:12:18 +01:00
|
|
|
/**
|
|
|
|
* Update user info.
|
|
|
|
*
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param array $data
|
2018-03-03 08:12:18 +01:00
|
|
|
*
|
|
|
|
* @return User
|
2021-09-18 10:21:29 +02:00
|
|
|
* @throws FireflyException
|
2018-03-03 08:12:18 +01:00
|
|
|
*/
|
|
|
|
public function update(User $user, array $data): User
|
|
|
|
{
|
2019-08-22 17:06:43 +02:00
|
|
|
$this->updateEmail($user, $data['email'] ?? '');
|
2021-04-07 05:55:51 +02:00
|
|
|
if (array_key_exists('blocked', $data) && is_bool($data['blocked'])) {
|
2019-08-22 17:06:43 +02:00
|
|
|
$user->blocked = $data['blocked'];
|
|
|
|
}
|
2021-04-07 05:55:51 +02:00
|
|
|
if (array_key_exists('blocked_code', $data) && '' !== $data['blocked_code'] && is_string($data['blocked_code'])) {
|
2019-08-22 17:06:43 +02:00
|
|
|
$user->blocked_code = $data['blocked_code'];
|
|
|
|
}
|
2021-04-07 05:55:51 +02:00
|
|
|
if (array_key_exists('role', $data) && '' === $data['role']) {
|
2020-03-23 17:54:49 +01:00
|
|
|
$this->removeRole($user, 'owner');
|
|
|
|
$this->removeRole($user, 'demo');
|
2019-08-22 17:06:43 +02:00
|
|
|
}
|
|
|
|
|
2018-03-03 08:12:18 +01:00
|
|
|
$user->save();
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2017-09-26 09:15:21 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param string $newEmail
|
2017-09-26 09:15:21 +02:00
|
|
|
*
|
2019-08-22 17:06:43 +02:00
|
|
|
* @return bool
|
2021-09-18 10:20:19 +02:00
|
|
|
* @throws FireflyException
|
2017-09-26 09:15:21 +02:00
|
|
|
* @see changeEmail
|
|
|
|
*/
|
|
|
|
public function updateEmail(User $user, string $newEmail): bool
|
|
|
|
{
|
2019-08-22 17:06:43 +02:00
|
|
|
if ('' === $newEmail) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-26 09:15:21 +02:00
|
|
|
$oldEmail = $user->email;
|
|
|
|
|
|
|
|
// save old email as pref
|
2018-07-15 09:27:38 +02:00
|
|
|
app('preferences')->setForUser($user, 'admin_previous_email_latest', $oldEmail);
|
2022-12-29 19:42:26 +01:00
|
|
|
app('preferences')->setForUser($user, 'admin_previous_email_'.date('Y-m-d-H-i-s'), $oldEmail);
|
2017-09-26 09:15:21 +02:00
|
|
|
|
|
|
|
$user->email = $newEmail;
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-10-26 06:20:33 +02:00
|
|
|
|
|
|
|
/**
|
2022-03-29 14:59:58 +02:00
|
|
|
* Remove any role the user has.
|
|
|
|
*
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param string $role
|
2021-10-26 06:20:33 +02:00
|
|
|
*/
|
2022-03-29 14:59:58 +02:00
|
|
|
public function removeRole(User $user, string $role): void
|
2021-10-26 06:20:33 +02:00
|
|
|
{
|
2022-03-29 14:59:58 +02:00
|
|
|
$roleObj = $this->getRole($role);
|
|
|
|
if (null === $roleObj) {
|
|
|
|
return;
|
2021-10-26 06:20:33 +02:00
|
|
|
}
|
2022-03-29 14:59:58 +02:00
|
|
|
$user->roles()->detach($roleObj->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-29 19:42:26 +01:00
|
|
|
* @param string $role
|
2022-03-29 14:59:58 +02:00
|
|
|
*
|
|
|
|
* @return Role|null
|
|
|
|
*/
|
|
|
|
public function getRole(string $role): ?Role
|
|
|
|
{
|
|
|
|
return Role::where('name', $role)->first();
|
2021-10-26 06:20:33 +02:00
|
|
|
}
|
2022-10-01 12:21:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function validateInviteCode(string $code): bool
|
|
|
|
{
|
2023-02-11 07:36:45 +01:00
|
|
|
$now = today(config('app.timezone'));
|
2022-10-01 19:06:55 +02:00
|
|
|
$invitee = InvitedUser::where('invite_code', $code)->where('expires', '>', $now->format('Y-m-d H:i:s'))->where('redeemed', 0)->first();
|
2022-10-01 12:21:42 +02:00
|
|
|
return null !== $invitee;
|
|
|
|
}
|
2016-03-14 20:38:23 +01:00
|
|
|
}
|