This commit is contained in:
James Cole
2019-08-22 17:06:43 +02:00
parent 10737d10a5
commit 2a4051fe92
5 changed files with 175 additions and 59 deletions

View File

@@ -86,10 +86,10 @@ class UserRepository implements UserRepositoryInterface
* @param User $user
* @param string $newEmail
*
* @see updateEmail
*
* @return bool
* @throws \Exception
* @see updateEmail
*
*/
public function changeEmail(User $user, string $newEmail): bool
{
@@ -245,14 +245,14 @@ class UserRepository implements UserRepositoryInterface
$return = [];
// two factor:
$return['has_2fa'] = $user->mfa_secret !== null;
$return['is_admin'] = $this->hasRole($user, 'owner');
$return['blocked'] = 1 === (int)$user->blocked;
$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();
$return['has_2fa'] = $user->mfa_secret !== null;
$return['is_admin'] = $this->hasRole($user, 'owner');
$return['blocked'] = 1 === (int)$user->blocked;
$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();
$return['attachments_size'] = $user->attachments()->sum('size');
$return['bills'] = $user->bills()->count();
$return['categories'] = $user->categories()->count();
@@ -291,6 +291,28 @@ class UserRepository implements UserRepositoryInterface
return false;
}
/**
* Remove any role the user has.
*
* @param User $user
*/
public function removeRole(User $user): void
{
$user->roles()->sync([]);
}
/**
* Set MFA code.
*
* @param User $user
* @param string|null $code
*/
public function setMFACode(User $user, ?string $code): void
{
$user->mfa_secret = $code;
$user->save();
}
/**
* @param array $data
*
@@ -335,9 +357,17 @@ class UserRepository implements UserRepositoryInterface
*/
public function update(User $user, array $data): User
{
$this->updateEmail($user, $data['email']);
$user->blocked = $data['blocked'] ?? false;
$user->blocked_code = $data['blocked_code'] ?? null;
$this->updateEmail($user, $data['email'] ?? '');
if (isset($data['blocked']) && is_bool($data['blocked'])) {
$user->blocked = $data['blocked'];
}
if (isset($data['blocked_code']) && '' !== $data['blocked_code'] && is_string($data['blocked_code'])) {
$user->blocked_code = $data['blocked_code'];
}
if (isset($data['role']) && '' === $data['role']) {
$this->removeRole($user);
}
$user->save();
return $user;
@@ -350,12 +380,15 @@ class UserRepository implements UserRepositoryInterface
* @param User $user
* @param string $newEmail
*
* @return bool
* @see changeEmail
*
* @return bool
*/
public function updateEmail(User $user, string $newEmail): bool
{
if ('' === $newEmail) {
return true;
}
$oldEmail = $user->email;
// save old email as pref
@@ -367,16 +400,4 @@ class UserRepository implements UserRepositoryInterface
return true;
}
/**
* Set MFA code.
*
* @param User $user
* @param string|null $code
*/
public function setMFACode(User $user, ?string $code): void
{
$user->mfa_secret = $code;
$user->save();
}
}